[erlang-questions] Optimization for the ++ operator

Björn Gustavsson bgustavsson@REDACTED
Fri Jun 1 10:28:12 CEST 2012


On Thu, May 31, 2012 at 1:55 PM, José Valim <jose.valim@REDACTED> wrote:
> I thought Erlang would optimize the ++ operator when the left side is known
> at compile time.
>
> For example, if the compiler sees the following outside of a function
> signature:
>
>     "Foo" ++ Bar
>
> It could rewrite it as:
>
>     [$F, $o, $o | assert_list(Bar)]
>

The '++' operator does not verify that the second argument is a list.
Therefore, the compiler will rewrite the first expression to simply:

[$F,$o,$o|Bar]

> However, I ran some benchmarks and it seems the optimization does not happen
> (on R15B).
> With a local dummy implementation of assert_list(Bar), I got that the first
> format is 50% slower than the second one.

Have you made sure that you run each test in a newly spawned process?
Do you run the test for long enough time?

If you have set up your benchmark environment properly, and your second
example is still faster, I can only assume that the code sits better in cache.

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list