[erlang-questions] Small performance hint: avoid abusing module_info(attributes)

Max Lapshin max.lapshin@REDACTED
Thu Sep 28 08:56:44 CEST 2017


Hi.

We have moved from plain text lager logging to our own event system that
has main difference: we have limited amount of possible events that are
compiled in code.

In known_events.erl I have:


-event({stream_started, info, ["stream started"]}).

and then I write in code:


events:stream_started([{media,Name}]).

Later all this is printed (using lager formatting, it is cool).



So we are close to the problem.  My code that was handling such:
 events:stream_started was searching for predefined event via:
 known_events:module_info(attributes).

Suddenly eprof told that I need to look carefully at this place and I wrote
parse_transform that takes all attributes from module AST and compiles them
into code. Also I've generated function  known_events:find(Name) with all
possible clauses.

On single core of my macbook I got 4000 times speedup.  100 000 of fetches
known_events:all()  took about 8 seconds with module_info(attributes) and
about 2 milliseconds with generated and compiled code.


So lesson from here is: try to avoid calling module_info(attributes) in
innner loops. It may be suprisingly expensive.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170928/bd4df1c5/attachment.htm>


More information about the erlang-questions mailing list