Say i have a function that is exported from a module, but the modules uses the function many times.
So i wrote an alias, because i'm lazy when i code.
-export([get_toolkit/0]).
get_toolkit() ->
... code ... code ...
... code ... code ...
... code ... code ...
{ok, Thing}.
tk() -> get_toolkit().
Does the compiler optimizes aliases ?
Thanks
I think it will cost you one indirection. I say this because I took this code
-module(testit).
-export([get_toolkit/0, long/0, short/0]).
get_toolkit() ->
_ = lists:seq(1,100),
{ok, thing}.
tk() ->
get_toolkit().
long() ->
get_toolkit(),
{ok, thing2}.
short() ->
tk(),
{ok, thing3}.
and generated the ASM via erlc -S testit.erl which gave me
SNIP
{function, tk, 0, 4}.
{label,3}.
{line,[{location,"testit.erl",8}]}.
{func_info,{atom,testit},{atom,tk},0}.
{label,4}.
{call_only,0,{f,2}}.
{function, long, 0, 6}.
{label,5}.
{line,[{location,"testit.erl",11}]}.
{func_info,{atom,testit},{atom,long},0}.
{label,6}.
{allocate,0,0}.
{line,[{location,"testit.erl",12}]}.
{call,0,{f,2}}.
{move,{literal,{ok,thing2}},{x,0}}.
{deallocate,0}.
return.
{function, short, 0, 8}.
{label,7}.
{line,[{location,"testit.erl",15}]}.
{func_info,{atom,testit},{atom,short},0}.
{label,8}.
{allocate,0,0}.
{line,[{location,"testit.erl",16}]}.
{call,0,{f,4}}.
{move,{literal,{ok,thing3}},{x,0}}.
{deallocate,0}.
return.
the ASM shows that the last function (the one that uses tk/0 ) calls tk/0 ({call, 0, {f, 4}}) which in turn calls get_toolkit/0 ({call, 0, {f,2}}). The function which uses get_toolkit/0 directly calls get_toolkit/0 directly ({call, 0, {f,2}}).
So, I think that there is no optimization applied.
Also, I did some time-tests which seemed to support this hypothesis ;)