[erlang-questions] overhead of lists:dropwhile/2

Björn Gustavsson bjorn@REDACTED
Sun Apr 26 14:34:42 CEST 2015


On Sun, Apr 26, 2015 at 12:14 PM, Tuncer Ayaz <tuncer.ayaz@REDACTED> wrote:
> On Sun, Apr 26, 2015 at 11:21 AM, Björn Gustavsson <bjorn@REDACTED> wrote:
[...]
>> drops many items from the list, that cost will not be noticeable.
>> But it will be noticeable if no or few elements are dropped from the
>> list. For example, in beam_utils, there is almost never any label to
>> drop (i.e. the fun would only be called once for each call to
>> lists:dropwhile/2). Similarly, in beam_jump it is also fairly common
>> that nothing is dropped.
>
> Would it be as fast if you had extracted the predicate into a local
> fun and referenced it as "fun my_pred/1" in the dropwhile/2 call?

No. That would not make any difference. A fun object still
has to be created on the heap in the current implementation of
funs. It would be possible to treat funs with an empty
environment (which includes all funs that are simple references
to local functions) as constants, but it is not how they are
currently implemented.

/Björn

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



More information about the erlang-questions mailing list