Optimize some ++ situations

Björn Gustavsson bjorn@REDACTED
Wed Oct 28 07:02:56 CET 2020


On Tue, Oct 27, 2020 at 5:09 PM Christofer Tornkvist (ctornkvi)
<ctornkvi@REDACTED> wrote:
>
> would it be a stupid suggestion to let add a parameter of list type
> to all built-in and library functions returning a list.

No, but it is unlikely that we would consider implementing such an
open-ended suggestion.

It would be better to show real world use cases for the list functions
that would truly gain from that optimization.

> If this was the case, the complier can optimize code like;
>
> erlang:atom_to_list(Atom) ++ MyTail
>
> into
>
> erlang:atom_to_list(Atom, MyTail)
>

In many cases you can build an iolist instead:

[atom_to_list(Atom),MyTail]

and flatten it later or send it to one of the many functions or BIFs
that accept iolists.

> lists:map(Fun, List) ++ MyTail
>
> into
>
> lists:map(Fun, List, MyTail)
>

The compiler will optimize it if you use a list comprehension instead
of lists:map/2:

[Fun(E) || E <- List] ++ MyTail

> Or is the compiler able to see this optimization anyhow?

No.

/Björn

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


More information about the erlang-questions mailing list