Pattern matching vs. ETS lookups

Serge Aleynikov serge@REDACTED
Mon Feb 16 19:26:29 CET 2004

Could anyone shed some light on which one of the two approaches is 
better (i.e. faster, more compact, more efficient) - function 
overloading & use of pattern matching or ets lookups?

A binary protocol uses the following convention:
<<FunID_MSB/8, FunID_LSB/8, Body/binary>>

{FunID_MSB, FunID_LSB} determine the FunctionID to be called, that 
accepts a Body/binary.

The main question is if the number of functions is large (~ 65536), is 
it better to implement header/body parsing using hard-coded patterns:

% Input types: <<FunctionID/8, Body/binary>>
f1(<<0,   1,   Body/binary>>) -> ...;
f1(<<0,   2,   Body/binary>>) -> ...;
f1(<<255, 255, Body/binary>>) -> ... .

or to have an ETS table that 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(<<FunID_MSB/8, FunID_LSB/8, Body/binary>>, Table) ->
    BodyParsingFunction =
      ets:lookup_element(Table, {FunID_MSB, FunID_LSB}, 2),



More information about the erlang-questions mailing list