joangavelan

joangavelan

Reducing incremental compilation times in Phoenix/Ash project

So I’m building a couple of projects with Elixir/Ash/Phoenix/Inertia/React and I’m being EXTREMELY productive with this stack — I mean it, VERY productive — making great progress on both projects at the same time.

Although, I’ve run into a roadblock that’s been discouraging me and I fear it will get worse. One of the projects has started to grow, and every little change I make in a file is taking quite a while to recompile (incremental compilation). It’s slowing down my productivity a lot. It started at 2–3s and now it’s up to 8–10s on every change.

I’m almost certain this is due to my lack of experience in the language and that I might be doing something wrong with the way I’m architecting/writing my code. I’ve done some research and it seems related to compile/runtime dependencies and the like. I’d really appreciate actionable advice or links to resources that can help me fix this. Will this keep getting worse as my project grows, or can it be kept low?

As a side note, I had Claude analyze my repo and it found 13 circular dependency cycles. I’m sure some of you will resonate with this and can guide me on identifying, measuring, and fixing the problem — and avoiding it in the future.

Thanks in advance.

Most Liked

sodapopcan

sodapopcan

You can use xref to help better understand how your modules are connected. There can be a bit of a learning curve but some useful commands are:

mix xref graph --format cycles --label compile-connected

and

mix xref graph --label cycles --sink lib/path/to/file/that/causes/many/recompilations.ex

Cycles aren’t 100% avoidable. For example in has_many and belongs_to relationships, A is going to refer to B and B is going to refer to A. But you should avoid calling functions in this scenario, ie, avoid this:

defmodule A do
  def a, do: B.b()
end

defmodule B do
  def b, do: A.a()
end

This is a bit of a deep topic and there are a lot of threads about this on this forum (search for “transitive dependencies”) as well as some blog posts. Here’s a good one. But it’s very hard to give specific actionable advice without seeing some code (though it would require seeing a lot of code which is of course probably not possible and not something I’d really wanna do, lol).

Some brief tips, though:

  • Avoid defining macros in files that change often.
  • Avoid having modules that you sometimes use in some modules, and sometimes just call random functions (or import) in other modules. Split those up.
  • Avoid calling functions in a module’s body. For example, if you have @foo Foo.bar(), change that to def foo, do: Foo.bar(). This can be ok, but these are brief tips so it’s easier to say Just Don’t Do It.
  • Sometimes you want to avoid even referring to a module in another module’s body (though I believe this isn’t as much of a problem anymore)
  • Along the lines of avoiding cycles: don’t (ever) call any Web functions from your domain modules/resources modules.

That’s all I got. If you have more questions I’m happy to try and answer. I’m not a huge expert here though have helped squash compiletime deps in a few projects and enjoy the topic.

joangavelan

joangavelan

Thank you everyone for your contributions! My mistake was writing inline action hooks within my Ash resources. Moving them into their own modules fixed the issue. I was even experiencing slow compilation times when changing code in my Phoenix controllers, because they referenced Ash domains, which in turn pointed to resources that referenced other modules in the inline hook implementations—creating the so-called transitive dependencies. Breaking those action hooks into separate modules completely solved the problem. Also worth mentioning @zachdaniel for his support in the Ash Discord community. Thanks again!

arcanemachine

arcanemachine

I can’t help you with the Ash side of things, but you can use this tool to get a more concrete view of your dependency graph if that a concern:

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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 43806 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement