static variables?
Yariv Sadan
yarivvv@REDACTED
Tue Aug 22 16:32:32 CEST 2006
First, thanks for everybody's answers. They have been very useful.
Parameterized modules look very promising. I may very well use them.
>
> I interpret your question as "is there a way of defining a
> constant complex value such as a list, without wasting time
> on rebuilding the structure each time it is accessed?".
Yes, that's what I meant :)
>
> Well, ideally, the compiler should construct this constant and
> place it in memory once and for all, so that each access to
> the fields() function just returns a pointer to that memory.
> Sadly, the Beam compiler is not doing this yet, because of
> memory management implementation issues. If you HiPE-compile
> your program, however, you will get this effect. (And even
> the Beam compiler does some optimizations when it comes to
> constructing constant strings.)
Thanks for the tip. I have no objections to solving the problem by
turning on the hipe option :)
>
> My advice would be: don't worry too much about the efficiency,
> but for starters consider using a list of atoms instead of a
> list of strings. It is easy to convert atoms to strings when
> necessary, and comparing atoms for equivalence is much cheaper.
> For example:
>
> fields() ->
> [name, age].
>
> This has quite low cost. If you need much larger constant
> structures (hundreds of bytes) that are accessed often, then
> start considering passing them around in a separate variable,
> or in a field of a state record that you pass around.
>
Although I don't mind passing variables around in general, this isn't
really an option for me at the moment. I will stick to atoms, though.
Thanks.
I know the example I showed was very basic, but I was actually looking
for a generic way of avoiding potentially very inefficient code. If my
example had a thousand fields, the repeated construction cost of the
list would have started seriously slowing things down (assuming the
compiler didn't optimize it).
Thanks again. I will probably go with parameterized modules, or just
rely on hipe.
Best,
Yariv
More information about the erlang-questions
mailing list