Performance
Counting calls with cprof
Measure before you change anything. The old profiler module of the books is gone; the runtime ships cprof for call counts, eprof for time per function, fprof for a full call graph, and observer for a window with all of them. cprof is the cheapest and the one to reach for first, because a function that runs a million times is usually the problem.
The module below has the same job written twice, once with plain recursion and once with an accumulator and a reverse, so the profile has something to count. Save it as perf.erl.
-module(perf).
-export([double_naive/1, double_fast/1]).
double_naive([]) ->
[];
double_naive([H | T]) ->
[H * 2 | double_naive(T)].
double_fast(List) ->
lists:reverse(double(List, [])).
double([], Acc) ->
Acc;
double([H | T], Acc) ->
double(T, [H * 2 | Acc]).
erlc perf.erl
erl
1> _ = cprof:start(), ok.
ok
2> Input = lists:duplicate(1000, 1), perf:double_fast(Input) =:= perf:double_naive(Input).
true
3> _ = cprof:pause(), ok.
ok
4> {_Module, Total, Counts} = cprof:analyse(perf), {Total, proplists:get_value({perf, double, 2}, Counts)}.
{2003,1001}
Two thousand calls in total, and one thousand of them in the accumulator loop: one per element plus the final empty list. The counts are exact, which is the point of cprof; the times that eprof prints are not, so read cprof for the what and eprof only for the where.
Memory and sharing
A list cell is two words of memory, one for the head and one for the tail pointer, and a small integer fits in a word. That is why a list of a million integers costs about sixteen megabytes, and why a binary of the same size costs one megabyte: binaries above 64 bytes live outside the heap and are shared by reference. erts_debug:size counts a shared term once, flat_size counts it every time.
erl
1> L = [1, 2, 3], erts_debug:size(L).
6
2> erts_debug:size([L, L]).
10
3> erts_debug:flat_size([L, L]).
16
The pair [L, L] costs two cells plus the shared list once, so 10 words, while flat_size charges the list twice and answers 16. Erlang never copies a term it does not have to copy, and that is why passing a big data structure to a process in the same node is cheap.
Habits that keep code fast
Most slowness in Erlang comes from a handful of shapes, and all of them are easy to avoid once you know them. The rules below are worth more than any micro benchmark.
Build a list from the front and reverse it once, as double_fast/1 does, instead of appending to the end of a growing list. Build output as an iolist and let the port flatten it in one pass: io:format and gen_tcp:send both accept a deep list, and iolist_to_binary is the cheap way to collapse it. Keep the O(n) functions out of a loop: length/1, lists:append/2 and ++ with a long left side each walk the list they are given.
Use maps for a record with a stable key set, a map or an ETS table for shared state, and binaries for text that crosses a process or a socket. Match on the shape of the data instead of testing it with a chain of functions, and let the guards of lesson 17 keep the hot clause early.
Runtime flags
The VM takes its own flags, and the default is right for most programs. +S sets the number of schedulers, +A the async thread pool for file and DNS work, +P the maximum number of processes and +Q the maximum number of ports. A node that does a lot of file IO benefits from a larger +A; a node with more processes than the default limit needs a larger +P. Change one flag at a time, and only after a measurement shows the limit.
The same numbers are readable at run time: erlang:system_info(process_count), erlang:system_info(run_queue) for the work waiting for a scheduler, and erlang:statistics(reductions) for the total work done. observer:start() shows all of them live, together with the process list and the memory per process, and that window is usually the fastest way to see what a running node is doing.
Next: Modern Erlang tour
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which tool counts how often each function is called?
Answer
cprofcprof counts calls per module, function and arity without measuring time.
-
What does
cprof:pause/0do?Answer
Stops counting
The counters stop, so
cprof:analyse/1reads a stable snapshot. -
What does the answer of
cprof:analyse/1contain?Answer
A module, a total and a list of
{MFA, Count}pairsThe counts are exact per function, and the total is the sum over the module.
-
Which profiler builds a full call graph with times?
Answer
fproffprof traces every call and writes a profile with call counts and times.
-
How much memory does one list cell take?
Answer
Two words
A cell holds the head and the tail pointer, so two words on a 64-bit VM.
-
Where does a binary larger than 64 bytes live?
Answer
Outside the heap, shared by reference
Large binaries are reference counted and passed by reference, not copied.
-
What does
erts_debug:size/1count for a shared term?Answer
The term once
size follows sharing, while flat_size charges the term at every reference.
-
Why does
double_fast/1reverse the accumulator?Answer
Because the accumulator is built backwards
Prepending is O(1), so the accumulator comes out reversed and one reverse fixes the order.
-
Why are iolists preferred for output?
Answer
They are flattened once by the writer
Building a deep list is cheap, and the port or
iolist_to_binaryflattens it in one pass. -
What does the
+Pflag of the VM set?Answer
The maximum number of processes
+Praises the process limit;+S,+Aand+Qare the other three.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev