-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Optimize lists:flatmap/2 for filtering #8749
Conversation
CT Test ResultsNo tests were run for this PR. This is either because the build failed, or the PR is based on a branch without GH actions tests configured. Results for commit 29bb2b4 To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a worthwhile optimization.
I only have one minor comment; please amend the commit and force-push when you do the correction.
5858183
to
29bb2b4
Compare
BIF call overhead is considerable compared to consing or doing nothing at all. |
I did some measurements using your version of
According to these measurements, your version is faster if all lists have 0 or 1 elements. As soon as most lists have more than one element, you will lose time by having those extra clauses. |
Ah, I get it now 💡 The performance gain is most pronounced when the given |
@bjorng you beat me to it XD |
@bjorng that said... doesn't that mean that with the changes in this PR, |
Yes, it does. I think it's worth it because using |
In case there is an interest, I'm proposing this optimization we added to elixir (elixir-lang/elixir#13793), since the implementation and the expected improvement are the same. Feel free to close if you feel it is unnecessary.
This could especially generate important speedups (1.5~3x) for cases where
flatmap
is used as afiltermap
, like this or this, but should benefit any case which returns some amount of empty lists or size-1 lists.