Re: Optimize some ++ situations

Björn Gustavsson <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CA+yh78SRJmW5yfW9m3OFhOG2+Nf=8xr8Np_Ho7xgfE7Fq3n1mQ@mail.gmail.com>
On Tue, Oct 27, 2020 at 5:09 PM Christofer Tornkvist (ctornkvi)
<[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.