Pattern matching vs. ETS lookups
Serge Aleynikov
serge@REDACTED
Wed Dec 31 14:40:15 CET 2003
In implementing an application dealing with some binary protocol I ran
into a question that I'd like to ask Erlang community.
A binary protocol involves parsing a header which determines the
function to be called, and a body of variable length. The main question
is if the number of functions is large > 250, is it better (i.e. faster,
more compact, more efficient?) to implement header/body parsing using
hard-coded patterns:
% Input types: <<FunctionID/8, Body/binary>>
f1(<<1, Body/binary>>) -> ...;
f1(<<2, Body/binary>>) -> ...;
f1(<<3, Body/binary>>) -> ...;
...
f1(<<255, Body/binary>>) -> ... .
or to have an ETS table tha would store tuples in the form:
{FunctionID, BodyParsingFunction}
where the ETS table (Table) would initially be populated with a function
list, and later do:
f(<<FunctionID/8, Body/binary>>, Table) ->
BodyParsingFunction =
ets:lookup_element(Table, FunctionID, 2),
BodyParsingFunction(Body).
In the first case if the f1(<<255, ...>>) function is called, would the
bytecode interpreter have to evaluate 255 overloaded function patterns
before it can successfully invoke the last function in the list?
Thanks.
Serge
More information about the erlang-questions
mailing list