[erlang-questions] Strange optimization result

Bob Ippolito <>
Sun Oct 21 11:17:39 CEST 2007


In my experience using tuples for this purpose, it seems like in many
or most cases tuples are instantiated from scratch every function call
even though they're constant in the bytecode. You might just be
assuming some optimization that Erlang doesn't actually do. This is
the sort of thing that the hybrid model might fix, but as far as I
know the only data structures that are on a global heap is large
binaries, not tuples of any kind or size. I'm mostly just guessing
though, because I have not read any of the Erlang VM's source (and
it's great that I haven't had to!).

You may have better luck restructuring your code in such a way that
this can't happen, either by generating a function that is the
equivalent of element(N, Tuple), e.g. a function with 256 clauses, or
by keeping the tuple in the state of a process (maybe several
processes) so that it doesn't get continuously allocated and
deallocated.

-bob

On 10/20/07, Steve Vinoski <> wrote:
>
> On 10/20/07, Anders Nygren <> wrote:
> > Hi
> > I am playing around with Tim Bray's widefinder problem.
> > Actually I am playing with Steve V's latest version
> > see http://steve.vinoski.net/blog/2007/10/18/ok-just-one-more -wf/
> >
> > In it he is using a dict for Boyer-Moore searching, the dict is just
> > a map from char code to shift amount i.e. the keys are 1 - 255
> > and the values are ints.
> > I figured that it would be faster to just have a tuple with the shift
> values.
> > So I changed the code to generate a tuple instead of a dict. And the
> > lookups changed from
> > dict:fetch(C1, Tbl)
> > to
> > element(C1,Tbl)
> >
> > To my big surprise the program now runs slower.
> > On a file with 1M rows approx. 192 M.
> >
> > Steve's original code, using dics, as reported by time
> > real    0m20.444s
> > user    0m37.718s
> > sys     0m0.612s
> >
> > My modified, using a tuple
> > real    0m25.849s
> > user    0m44.203s
> > sys     0m3.332s
> >
> > Interesting to note is the high value for sys in my version.
> > Also the tuple version does not manage to max out the CPU use
> > it peaks at ~85%, while the dict version gets 100% CPU load.
> > The program starts 64 erlang processes on my dual core laptop.
> >
> > My only idea is that this is caused by some pathological CPU cache
> > behaviour. Does anyone have a better explanation?
> >
>
> Hi Anders, I don't know the answer, but I agree that it seems strange. I
> wrote some tests that did simple lookups against dicts and tuples in a loop
> and timed them, and tuples were faster, but when I used them in my Wide
> Finder code, they were slower, just as you saw.
>
> --steve
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list