今天再[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文件
生成的汇编代码
[*]使用内联编译
erlang代码
生成的汇编代码
[/list]
其中生成汇编代码的命令是:
erlc -S test
但是内联功能从来不会默认打开的。如果想使用的话,必须显示的在代码里使用-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