MatUrbanski

MatUrbanski

Hello everyone. I’m pretty new to Elixir. I have question about alias and import usage. Does using import makes impact on app performance because of importing all of the code into module? When should I use import and when alias? I didn’t find any good answer for this question.

Thanks in advance.

Showing Posts 1 to 10

KronicDeth

KronicDeth

import does not “import” the code into the module’s .beam. All calls are resolved at compile time and both alias qualified calls and imported calls count as remote calls. It’s mostly a convenience for the human reader of your code.

Kurisu

Kurisu

After reading this about upgrading to Phoenix 1.4:

A Routes alias has been added to app_web.ex for view and controller blocks in favor over the previously imported AppWeb.Router.Helpers .

The new Routes alias makes it clearer where page_path/page_url and friends exist and removes compile-time dependencies across your web stack.

Should we consider that there is at least a compile-time performance matter between alias and import ?

I found this topic because I was wondering if I will import back the router helpers in my phoenix project as before 1.4 version…

LostKobrakai

LostKobrakai

There absolutely is a cost to using import, because it needs to require the selected module as part of its functionality. require does cause a compile time dependency, which means both modules can no longer be compiled in parallel, but need to be compiled one after the other. The upside is that you can check at compile time if certain functions are actually defined in the external module, what you can’t do otherwise. Having a compile time dependency on a module means the current module needs to be recompiled if the module it depends on changes. So for the router helpers it at least means with each route change the compiler will need to at least recompile all the view modules.

11
Post #3
Kurisu

Kurisu

Thank you for this clarification. ^^

christhekeele

christhekeele

Important to emphasize: this only introduces perf penalties during compilation. By default that is only once each time you deploy to production, and every time you change files in a dev environment.

Your original question asks if this affects app performance, which for most app production deployment techniques means only runtime perf, not compile time perf. In this case there is no impact.

Edit: meant to reply to @Kurisu, sorry @LostKobrakai

hauleth

hauleth

I am not so sure. IIRC imported functions aren’t remotely called, which mean that if you overwrite imported module in the runtime then the module will still use old implementations. alias will make calls remote, so there will be negligible performance hit, but it will provide more flexibility.

christhekeele

christhekeele

That’s a good point, I forget that local vs remote calls have slightly different hot-code-reloading characteristics, and import vs alias determines if they are local or remote.

I’d be curious to see what the performance diff between the two is, even though it’s totally negligible to every application I can conceive of. I did some digging around the core erlang docs and BEAM internals book and there are slightly different instruction sets for them, but I couldn’t divine what the end cost to the caller would be.

At any rate, it’s the lowest level of perf difference possible above the bytecode level, so whichever produces the most maintainable code for one’s situation is to be preferred for sure!

gregvaughn

gregvaughn

Is that true? I always thought import just did a compile time check for the functions’ existence, but still generated the remote call AST.

NobbZ

NobbZ

import and alias do not change if a function call is remote or local…

alias Foo.Bar
Bar.baz()
import Foo.Bar
baz()

Will both result in BEAM bytecode roughly equivalent to the following and therefore be remote calls:

Foo.Bar.baz()
josevalim

josevalim

Creator of Elixir

This is correct. Both imported and aliased calls are remote calls.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New
adr
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities. Sup...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews