[erlang-questions] HiPE and large number of string cases

Peter Wang pwa@REDACTED
Mon Jul 2 04:22:36 CEST 2007


Hi,

I've been writing an Erlang backend for the Mercury compiler.  I've come
across a problem compiling a particular generated function with HiPE.
The function simply matches a variable against a string and returns a
corresponding atom, like this:

    f(X) ->
	case X of
	    "option1" -> atom1;
	    "option2" -> atom2;
	    ...
	    _ -> fail
	end.

There are over 500 cases.  Compiling it to bytecode takes a reasonable
amount of time (~2 secs), but compiling it to native code is unbearably
slow (over five minutes).  Breaking it into smaller functions does help,
but it's still not exactly fast (~1 minute).  (These times are at
optimisation level o1.)

I've found a workaround, which is to convert the string to an atom at
runtime and match on atom instead, but I'd rather not do that.  What
else can I do?

Thanks.
Peter




More information about the erlang-questions mailing list