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

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews