summaryrefslogtreecommitdiff
path: root/system/doc/efficiency_guide/functions.xml
blob: f3d6b59e49dc4f7fce52a1175ec131dc1fc7f56c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>2001</year><year>2022</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    </legalnotice>

    <title>Functions</title>
    <prepared>Bjorn Gustavsson</prepared>
    <docno></docno>
    <date>2007-11-22</date>
    <rev></rev>
    <file>functions.xml</file>
  </header>

  <section>
    <title>Pattern Matching</title>
    <p>Pattern matching in function head as well as in <c>case</c> and
    <c>receive</c> clauses are optimized by the compiler. With a few
    exceptions, there is nothing to gain by rearranging clauses.</p>

    <p>One exception is pattern matching of binaries. The compiler
    does not rearrange clauses that match binaries. Placing the
    clause that matches against the empty binary <em>last</em> is usually
    slightly faster than placing it <em>first</em>.</p>

    <p>The following is a rather unnatural example to show another
    exception:</p>

    <p><em>DO NOT</em></p>
    <code type="erl">
atom_map1(one) -> 1;
atom_map1(two) -> 2;
atom_map1(three) -> 3;
atom_map1(Int) when is_integer(Int) -> Int;
atom_map1(four) -> 4;
atom_map1(five) -> 5;
atom_map1(six) -> 6.</code>

     <p>The problem is the clause with the variable <c>Int</c>.
     As a variable can match anything, including the atoms
     <c>four</c>, <c>five</c>, and <c>six</c>, which the following clauses
     also match, the compiler must generate suboptimal code that
     executes as follows:</p>

     <list type="bulleted">
     <item>First, the input value is compared to <c>one</c>, <c>two</c>, and
     <c>three</c> (using a single instruction that does a binary search;
     thus, quite efficient even if there are many values) to select which
     one of the first three clauses to execute (if any).</item>

     <item>If none of the first three clauses match, the fourth clause
     match as a variable always matches.</item>

     <item>If the guard test <c>is_integer(Int)</c> succeeds, the fourth
     clause is executed.</item>

     <item>If the guard test fails, the input value is compared to
     <c>four</c>, <c>five</c>, and <c>six</c>, and the appropriate clause
     is selected. (There is a <c>function_clause</c> exception if none of
     the values matched.)</item>
     </list>

     <p>Rewriting to either:</p>

     <p><em>DO</em></p>
     <code type="erl"><![CDATA[
atom_map2(one) -> 1;
atom_map2(two) -> 2;
atom_map2(three) -> 3;
atom_map2(four) -> 4;
atom_map2(five) -> 5;
atom_map2(six) -> 6;
atom_map2(Int) when is_integer(Int) -> Int.]]></code>

     <p>or:</p>

     <p><em>DO</em></p>
     <code type="erl"><![CDATA[
atom_map3(Int) when is_integer(Int) -> Int;
atom_map3(one) -> 1;
atom_map3(two) -> 2;
atom_map3(three) -> 3;
atom_map3(four) -> 4;
atom_map3(five) -> 5;
atom_map3(six) -> 6.]]></code>

     <p>gives slightly more efficient matching code.</p>

     <p>Another example:</p>

     <p><em>DO NOT</em></p>
     <code type="erl"><![CDATA[
map_pairs1(_Map, [], Ys) ->
    Ys;
map_pairs1(_Map, Xs, [] ) ->
    Xs;
map_pairs1(Map, [X|Xs], [Y|Ys]) ->
    [Map(X, Y)|map_pairs1(Map, Xs, Ys)].]]></code>

     <p>The first argument is <em>not</em> a problem. It is variable, but it
     is a variable in all clauses. The problem is the variable in the second
     argument, <c>Xs</c>, in the middle clause. Because the variable can
     match anything, the compiler is not allowed to rearrange the clauses,
     but must generate code that matches them in the order written.</p>

     <p>If the function is rewritten as follows, the compiler is free to
     rearrange the clauses:</p>

     <p><em>DO</em></p>
     <code type="erl"><![CDATA[
map_pairs2(_Map, [], Ys) ->
    Ys;
map_pairs2(_Map, [_|_]=Xs, [] ) ->
    Xs;
map_pairs2(Map, [X|Xs], [Y|Ys]) ->
    [Map(X, Y)|map_pairs2(Map, Xs, Ys)].]]></code>

    <p>The compiler will generate code similar to this:</p>

    <p><em>DO NOT (already done by the compiler)</em></p>
    <code type="erl"><![CDATA[
explicit_map_pairs(Map, Xs0, Ys0) ->
    case Xs0 of
	[X|Xs] ->
	    case Ys0 of
		[Y|Ys] ->
		    [Map(X, Y)|explicit_map_pairs(Map, Xs, Ys)];
		[] ->
		    Xs0
	    end;
	[] ->
	    Ys0
    end.]]></code>
      
    <p>This is slightly faster for probably the most common case
    that the input lists are not empty or very short.
    (Another advantage is that Dialyzer can deduce a better type
    for the <c>Xs</c> variable.)</p>
  </section>

  <section>
    <title>Function Calls</title>

    <p>This is a rough hierarchy of the performance of the
    different types of function calls:</p>

    <list type="bulleted">
    <item>Calls to local or external functions (<c>foo()</c>, <c>m:foo()</c>)
    are the fastest calls.</item>

    <item>Calling or applying a fun (<c>Fun()</c>, <c>apply(Fun, [])</c>)
    is just a little slower than external calls.</item>

    <item>Applying an exported function (<c>Mod:Name()</c>,
    <c>apply(Mod, Name, [])</c>) where the number of arguments is known
    at compile time is next.</item>

    <item>Applying an exported function (<c>apply(Mod, Name, Args)</c>)
    where the number of arguments is not known at compile time is the
    least efficient.</item>
    </list>

    <section>
       <title>Notes and Implementation Details</title>

       <p>Calling and applying a fun does not involve any hash-table lookup.
       A fun contains an (indirect) pointer to the function that implements
       the fun.</p>

       <p><c>apply/3</c> must look up the code for the function to execute
       in a hash table. It is therefore always slower than a
       direct call or a fun call.</p>

       <p>Caching callback functions into funs may be more efficient
       in the long run than apply calls for frequently-used callbacks.</p>

    </section>
  </section>

  <section>
    <title>Memory Usage in Recursion</title>
    <p>When writing recursive functions, it is preferable to make them
      tail-recursive so that they can execute in constant memory space:</p>

    <p><em>DO</em></p>
    <code type="none">
list_length(List) ->
    list_length(List, 0).

list_length([], AccLen) -> 
    AccLen; % Base case

list_length([_|Tail], AccLen) ->
    list_length(Tail, AccLen + 1). % Tail-recursive</code>

    <p><em>DO NOT</em></p>

    <code type="none">
list_length([]) ->
    0. % Base case
list_length([_ | Tail]) ->
    list_length(Tail) + 1. % Not tail-recursive</code>
  </section>
</chapter>