Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List.map foldl-version #1116

Open
andre-dietrich opened this issue Jun 4, 2021 · 2 comments
Open

List.map foldl-version #1116

andre-dietrich opened this issue Jun 4, 2021 · 2 comments

Comments

@andre-dietrich
Copy link

andre-dietrich commented Jun 4, 2021

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 ...

@github-actions
Copy link

github-actions bot commented Jun 4, 2021

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.

@andre-dietrich
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant