compiler: Optimize map comprehensions to use maps:from_keys/2#10646
compiler: Optimize map comprehensions to use maps:from_keys/2#10646bjorng merged 2 commits intoerlang:masterfrom
Conversation
|
Please rebase on the latest master. |
e1b1ee1 to
999b155
Compare
CT Test Results 2 files 335 suites 9m 6s ⏱️ Results for commit 7a16eba. ♻️ This comment has been updated with latest results. 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 |
|
Sorry about that, should be correctly rebased on master now |
e6b5dd1 to
f0ea609
Compare
|
Multiple test suite modules fail to compile ( |
22e36c9 to
a1b733a
Compare
|
This should be now resolved, I successfully run the whole compile test suite |
Map comprehensions with constant values that don't depend on generator
variables are now compiled to use maps:from_keys/2 instead of
maps:from_list/1. This avoids creating intermediate {Key, Value} tuples.
Before: #{K => Value || K <- List} -> maps:from_list([{K, Value} || K <-
List])
After: #{K => Value || K <- List} -> maps:from_keys([K || K <- List],
Value)
The optimization applies when the value expression:
- Is safe (cannot fail at runtime)
- Has no pre-expressions (is a simple value)
- Does not reference any generator-bound variables
- For multi-valued comprehensions all values are the same
Examples that are optimized:
- #{K => 42 || K <- List} % literal
- #{K => X || K <- List} % outer variable
- #{K => {X, Y} || K <- List} % tuple of outer vars
- #{K => X, {x,K} => X || K <- List} % all values the same
Examples that are NOT optimized:
- #{K => V || {K, V} <- List} % value from generator
- #{K => K * 2 || K <- List} % value depends on key
- #{K => f(X) || K <- List} % function call (not safe)
- #{K => a, {x,K} => b || K <- List} % different values
a1b733a to
7a16eba
Compare
|
I've force-pushed a rebased and slightly updated version of this branch. Here is what I did:
I've added this PR along with an updated primary bootstrap to our daily builds. |
|
Amazing, thank you for fixing this up |
|
Thanks for your pull request. |
Map comprehensions with constant values that don't depend on generator
variables are now compiled to use
maps:from_keys/2instead ofmaps:from_list/1. This avoids creating intermediate{Key, Value}tuples.Before:
#{K => Value || K <- List}->maps:from_list([{K, Value} || K <- List])After:
#{K => Value || K <- List}->maps:from_keys([K || K <- List], Value)The optimization applies when the value expression:
Examples that are optimized:
#{K => 42 || K <- List}% literal#{K => X || K <- List}% outer variable#{K => {X, Y} || K <- List}% tuple of outer vars#{K => X, {x,K} => X || K <- List}% all values the sameExamples that are NOT optimized:
#{K => V || {K, V} <- List}% value from generator#{K => K * 2 || K <- List}% value depends on key#{K => f(X) || K <- List}% function call (not safe)#{K => a, {x,K} => b || K <- List}% different values