Regarding my “performance cost” phrasing, I was specifically referring to the distinction of turning off compile-time Plug initialization in Plug.Builder
and derivatives like Plug.Router
or Phoenix.Router
, which forces it to perform the way it would under MIX_ENV=dev
and is comparatively very expensive as those calculations are re-performed on every request.
:init_mode
- the environment to initialize the plug’s options, one of :compile or :runtime. Defaults [to]:compile
.
https://hexdocs.pm/plug/Plug.Builder.html#module-options
Each
Plug.Router
has a plug pipeline, defined byPlug.Builder
, …
https://hexdocs.pm/plug/Plug.Router.html
Anything that needs to vary after building your compiled artifact, such as in a Mix/Distillery release based on an environment variable, has to be deferred to your Plug’s call
and looked up on every request, so minimizing that effort is key when this becomes necessary for your goals. Something like persistent_term might be a good option if it’s a very hot path or something more complicated than a trivial Application.get_env
call. I’d love to see this older but well-researched post updated for modern versions of Erlang/Elixir with some of the new tools like persistent_term
included.
Finally, here’s some past discussions here on our forum for additional context: