elm/core

List.map foldl-version

andre-dietrich opened this issue · 2 comments

Hi, i just did some experiments with List.map and a foldl version of it, which seems to be faster (on average 25%), for different sizes and also when multiple mapping operations are applied subsequently within a pipe.

map f =
    List.foldl (\x acc -> (::) (f x) acc) []
        >> List.reverse

See the benchmark

ezgif com-gif-maker

Maybe this is an optimization option, next to a native Kernel-function ...

Thanks for reporting this! To set expectations:

  • Issues are reviewed in batches, so it can take some time to get a response.
  • Ask questions a community forum. You will get an answer quicker that way!
  • If you experience something similar, open a new issue. We like duplicates.

Finally, please be patient with the core team. They are trying their best with limited resources.

Only for curiosity, I added a local List.map1 function that does the same as List.map but it uses a Kernel function ... The patches look like this:

-- ~/.elm/0.19.1/packages/elm/core/1.0.5/src/List.elm

...

map1 : (a -> result) -> List a -> List result
map1 =
  Elm.Kernel.List.map1

...
// ~/elm/0.19.1/packages/elm/core/1.0.5/src/Elm/Kernel/List.js

...

var _List_map1 = F2(function(f, xs)
{
	for (var arr = []; xs.b ; xs = xs.b) // WHILE_CONSES
	{
		arr.push(f(xs.a));
	}
	return _List_fromArray(arr);
});

...

And boom, on Chromium I got a performance boost up to 250%, seems to be constant, tested for list lengths from 5 to 5000 ... On Firefox, the results seem to be similar, however it is dropping from 250% to 60% on lists with a 5000 elements...

screenshot_10

screenshot_1000