erlang的内联编译

今天再[url=http://www.cnblogs.com/me-sa/archive/2012/01/09/erlang0029.html]erlang inline 编译[/url]中读到erlang的函数也可以内联编译。
但是内联功能从来不会默认打开的。如果想使用的话,必须显示的在代码里使用-compile()属性。
当再代码里加上-compile(inline).时,编译器会根据自己的规则来决定哪个函数被定义成内联函数,其中有一个因素就是-compile({inline_size, Size}).,默认的size是24,这个size作为一个权重来参与决定某个函数是否被编译成内联的函数。
我们也可以使用
-compile({inline, [test/0, start/1]}).
或者
-compile({inline, [{test, 0}, {start, 1}]}).
来显示的指定哪个函数被编译成内联的函数。
我们还可以使用
-compile(inline_list_funcs).
来显示将下列的函数内联到该模块
[quote]
* lists:all/2
* lists:any/2
* lists:foreach/2
* lists:map/2
* lists:flatmap/2
* lists:filter/2
* lists:foldl/3
* lists:foldr/3
* lists:mapfoldl/3
* lists:mapfoldr/3
[/quote]
另外我们也可以同时指定这些选项来编译模块,例如:
-compile([inline, {inline_size, 100}, inline_list_funcs, {inline, [show/0]}]).

下面我们使用一个例子来看一下是否使用内联编译的区别:
[list]
[*]不是用内联编译
erlang文件

-module(test).

-export([test/0, show/0]).

test() ->
show(),
show(),
show(),
show().

show() ->
io:format("this is ~p~n", ["test"]).

生成的汇编代码

{module, test}. %% version = 0

{exports, [{module_info,0},{module_info,1},{show,0},{test,0}]}.

{attributes, []}.

{labels, 9}.


{function, test, 0, 2}.
{label,1}.
{line,[{location,"test.erl",5}]}.
{func_info,{atom,test},{atom,test},0}.
{label,2}.
{allocate,0,0}.
{line,[{location,"test.erl",6}]}.
{call,0,{f,4}}.
{line,[{location,"test.erl",7}]}.
{call,0,{f,4}}.
{line,[{location,"test.erl",8}]}.
{call,0,{f,4}}.
{call_last,0,{f,4},0}.


{function, show, 0, 4}.
{label,3}.
{line,[{location,"test.erl",11}]}.
{func_info,{atom,test},{atom,show},0}.
{label,4}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",12}]}.
{call_ext_only,2,{extfunc,io,format,2}}.


{function, module_info, 0, 6}.
{label,5}.
{line,[]}.
{func_info,{atom,test},{atom,module_info},0}.
{label,6}.
{move,{atom,test},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.


{function, module_info, 1, 8}.
{label,7}.
{line,[]}.
{func_info,{atom,test},{atom,module_info},1}.
{label,8}.
{move,{x,0},{x,1}}.
{move,{atom,test},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.

[*]使用内联编译
erlang代码

-module(test).

-export([test/0, show/0]).

-compile([inline, {inline_size, 100}, inline_list_funcs, {inline, [show/0]}]).

test() ->
show(),
show(),
show(),
show().

show() ->
io:format("this is ~p~n", ["test"]).

生成的汇编代码

{module, test}. %% version = 0

{exports, [{module_info,0},{module_info,1},{show,0},{test,0}]}.

{attributes, []}.

{labels, 9}.


{function, test, 0, 2}.
{label,1}.
{line,[{location,"test.erl",9}]}.
{func_info,{atom,test},{atom,test},0}.
{label,2}.
{allocate,0,0}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",16}]}.
{call_ext,2,{extfunc,io,format,2}}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",16}]}.
{call_ext,2,{extfunc,io,format,2}}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",16}]}.
{call_ext,2,{extfunc,io,format,2}}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",16}]}.
{call_ext_last,2,{extfunc,io,format,2},0}.


{function, show, 0, 4}.
{label,3}.
{line,[{location,"test.erl",15}]}.
{func_info,{atom,test},{atom,show},0}.
{label,4}.
{move,{literal,["test"]},{x,1}}.
{move,{literal,"this is ~p~n"},{x,0}}.
{line,[{location,"test.erl",16}]}.
{call_ext_only,2,{extfunc,io,format,2}}.


{function, module_info, 0, 6}.
{label,5}.
{line,[]}.
{func_info,{atom,test},{atom,module_info},0}.
{label,6}.
{move,{atom,test},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.


{function, module_info, 1, 8}.
{label,7}.
{line,[]}.
{func_info,{atom,test},{atom,module_info},1}.
{label,8}.
{move,{x,0},{x,1}}.
{move,{atom,test},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.

[/list]
其中生成汇编代码的命令是:
erlc -S test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值