tomekowal

tomekowal

Should defdelegate and import be compile time dependencies?

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?

Most Liked

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

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!

Where Next?

Popular in Discussions Top

AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20142 166
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement