tomekowal

tomekowal

I was looking today at slow compile times in my app, and I concluded, that it is because of defdelegates and imports

Instead of editing the app, I started wondering if those two macros must be compile-time dependencies.

In a generic defmacro case, we create a compile-time dependency.

In a case like A -> (compile) -> B -> (runtime) -> C,
if C changes, we need to recompile A.
This recompilation happens because changing C may change return values of B, and A might use them during its compilation.

That means all changes of runtime dependencies of B trigger recompilation of A, and that is unavoidable in a general case.

I believe it is not the case with defdelegate. We know how the macro works, and that it doesn’t care about the return value. It cares about the function name, arity, and its docs, so we could potentially avoid recompiling delegating module.

So in case like A -> (defdelegate) -> B -> (runtime) -> C,
if C changes, we don’t have to recompile A. We need to recompile A only if B changes.

I believe imports behave similarly. Importing module doesn’t care about return values of imported functions, so it could potentially skip the compilation if runtime dependencies of importing module change.

So in case like A -> (import) -> B -> (runtime) -> C,
if C changes we could potentially skip compilation of C.

For the curious people out there, I’d like to present why those two little macros cause quite a lot of recompilation.

We use Phoenix contexts, and very often the structure ends up like this:

context.ex (a bunch of defdelegates to use_case modules)
context
| - use_case1.ex (some defdelegates to helpers)
| - use_case1
|   `- use_case1_helper.ex
` - use_case2.ex 
...

One of the contexts is central to the app, and almost all other contexts use it. In the web layer view_helper.ex calls context.ex to calculate some info for displaying some entities. Different views import some functions from view_helper.ex because specifying the entire module name in templates <%= ViewHelper.function(...) %> seems strange.

In the end, changing use_case1_helper.ex triggers recompilation of context.ex which triggers recompilation of almost all views. In my relatively small app, it is 49 files.

I understand I could change imports to aliases, but maybe changing how the imports and defdelegates work could benefit the broader community.

Was there a discussion about it somewhere? I know that Phoenix.Router changed imports to aliases to solve a similar issue, so there might be something hard I am missing here.

On the other hand, in Elixir 1.6, structs stopped being a compile-time dependency and trigger recompilation only when the struct changes. Would it be possible to apply a similar trick to imports and defdelegates?

Showing Posts 1 to 2

wojtekmach

wojtekmach

Hex Core Team

Would it be possible to apply a similar trick to imports and defdelegates?

Yes! Here’s a patch for defdelegate: Don't add compile-time dependency on defdelegate by wojtekmach · Pull Request #10093 · elixir-lang/elixir · GitHub.

Imports are slightly more complicated to handle as we really do need to add the compile dependency so that when the imported function is ever removed, we need to recompile (and fail.)

10
Post #1
tomekowal

tomekowal OP

Nice! Defdelegates were more important to me anyway. I like them for maintaining clean APIs. I can easily get rid of imports and use aliases :slight_smile:
Thank you!

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
AstonJ
This might be a bit disturbing for some but it’s happening - computers running on living human neurons. They’ve made them smart enough t...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews