1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2001</year><year>2010</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
24
<title>Functions</title>
25
<prepared>Bjorn Gustavsson</prepared>
27
<date>2007-11-22</date>
29
<file>functions.xml</file>
33
<title>Pattern matching</title>
34
<p>Pattern matching in function head and in <c>case</c> and <c>receive</c>
35
clauses are optimized by the compiler. With a few exceptions, there is nothing
36
to gain by rearranging clauses.</p>
38
<p>One exception is pattern matching of binaries. The compiler
39
will not rearrange clauses that match binaries. Placing the
40
clause that matches against the empty binary <em>last</em> will usually
41
be slightly faster than placing it <em>first</em>.</p>
43
<p>Here is a rather contrived example to show another exception:</p>
45
<p><em>DO NOT</em></p>
49
atom_map1(three) -> 3;
50
atom_map1(Int) when is_integer(Int) -> Int;
53
atom_map1(six) -> 6.</code>
55
<p>The problem is the clause with the variable <c>Int</c>.
56
Since a variable can match anything, including the atoms
57
<c>four</c>, <c>five</c>, and <c>six</c> that the following clauses
58
also will match, the compiler must generate sub-optimal code that will
59
execute as follows:</p>
61
<p>First the input value is compared to <c>one</c>, <c>two</c>, and
62
<c>three</c> (using a single instruction that does a binary search;
63
thus, quite efficient even if there are many values) to select which
64
one of the first three clauses to execute (if any).</p>
66
<p>If none of the first three clauses matched, the fourth clause
67
will match since a variable always matches. If the guard test
68
<c>is_integer(Int)</c> succeeds, the fourth clause will be
71
<p>If the guard test failed, the input value is compared to
72
<c>four</c>, <c>five</c>, and <c>six</c>, and the appropriate clause
73
is selected. (There will be a <c>function_clause</c> exception if
74
none of the values matched.)</p>
76
<p>Rewriting to either</p>
79
<code type="erl"><![CDATA[
82
atom_map2(three) -> 3;
86
atom_map2(Int) when is_integer(Int) -> Int.]]></code>
91
<code type="erl"><![CDATA[
92
atom_map3(Int) when is_integer(Int) -> Int;
95
atom_map3(three) -> 3;
98
atom_map3(six) -> 6.]]></code>
100
<p>will give slightly more efficient matching code.</p>
102
<p>Here is a less contrived example:</p>
104
<p><em>DO NOT</em></p>
105
<code type="erl"><![CDATA[
106
map_pairs1(_Map, [], Ys) ->
108
map_pairs1(_Map, Xs, [] ) ->
110
map_pairs1(Map, [X|Xs], [Y|Ys]) ->
111
[Map(X, Y)|map_pairs1(Map, Xs, Ys)].]]></code>
113
<p>The first argument is <em>not</em> a problem. It is variable, but it
114
is a variable in all clauses. The problem is the variable in the second
115
argument, <c>Xs</c>, in the middle clause. Because the variable can
116
match anything, the compiler is not allowed to rearrange the clauses,
117
but must generate code that matches them in the order written.</p>
119
<p>If the function is rewritten like this</p>
122
<code type="erl"><![CDATA[
123
map_pairs2(_Map, [], Ys) ->
125
map_pairs2(_Map, [_|_]=Xs, [] ) ->
127
map_pairs2(Map, [X|Xs], [Y|Ys]) ->
128
[Map(X, Y)|map_pairs2(Map, Xs, Ys)].]]></code>
130
<p>the compiler is free rearrange the clauses. It will generate code
133
<p><em>DO NOT (already done by the compiler)</em></p>
134
<code type="erl"><![CDATA[
135
explicit_map_pairs(Map, Xs0, Ys0) ->
140
[Map(X, Y)|explicit_map_pairs(Map, Xs, Ys)];
148
<p>which should be slightly faster for presumably the most common case
149
that the input lists are not empty or very short.
150
(Another advantage is that Dialyzer is able to deduce a better type
151
for the variable <c>Xs</c>.)</p>
155
<title>Function Calls </title>
157
<p>Here is an intentionally rough guide to the relative costs of
158
different kinds of calls. It is based on benchmark figures run on
161
<list type="bulleted">
162
<item>Calls to local or external functions (<c>foo()</c>, <c>m:foo()</c>)
163
are the fastest kind of calls.</item>
164
<item>Calling or applying a fun (<c>Fun()</c>, <c>apply(Fun, [])</c>)
165
is about <em>three times</em> as expensive as calling a local function.</item>
166
<item>Applying an exported function (<c>Mod:Name()</c>,
167
<c>apply(Mod, Name, [])</c>) is about twice as expensive as calling a fun,
168
or about <em>six times</em> as expensive as calling a local function.</item>
172
<title>Notes and implementation details</title>
174
<p>Calling and applying a fun does not involve any hash-table lookup.
175
A fun contains an (indirect) pointer to the function that implements
178
<warning><p><em>Tuples are not fun(s)</em>.
179
A "tuple fun", <c>{Module,Function}</c>, is not a fun.
180
The cost for calling a "tuple fun" is similar to that
181
of <c>apply/3</c> or worse. Using "tuple funs" is <em>strongly discouraged</em>,
182
as they may not be supported in a future release,
183
and because there exists a superior alternative since the R10B
184
release, namely the <c>fun Module:Function/Arity</c> syntax.</p></warning>
186
<p><c>apply/3</c> must look up the code for the function to execute
187
in a hash table. Therefore, it will always be slower than a
188
direct call or a fun call.</p>
190
<p>It no longer matters (from a performance point of view)
191
whether you write</p>
194
Module:Function(Arg1, Arg2)</code>
199
apply(Module, Function, [Arg1,Arg2])</code>
201
<p>(The compiler internally rewrites the latter code into the former.)</p>
203
<p>The following code</p>
206
apply(Module, Function, Arguments)</code>
208
<p>is slightly slower because the shape of the list of arguments
209
is not known at compile time.</p>
214
<title>Memory usage in recursion</title>
215
<p>When writing recursive functions it is preferable to make them
216
tail-recursive so that they can execute in constant memory space.</p>
220
list_length(List, 0).
222
list_length([], AccLen) ->
225
list_length([_|Tail], AccLen) ->
226
list_length(Tail, AccLen + 1). % Tail-recursive</code>
227
<p><em>DO NOT</em></p>
231
list_length([_ | Tail]) ->
232
list_length(Tail) + 1. % Not tail-recursive</code>