diff options
author | Jose Valim & Michal Muskala <[email protected]> | 2018-07-25 17:33:30 +0200 |
---|---|---|
committer | José Valim <[email protected]> | 2018-07-25 17:39:44 +0200 |
commit | 6007bf0f822014e0bbd0cef675848d18ae99b69e (patch) | |
tree | 0faaf057a69927bce5974658f1c6e1a56dc923f8 /erts/emulator/beam/erl_nif_api_funcs.h | |
parent | fc12f6935028bf03a46892fc833aaa117d2cbc9e (diff) | |
download | otp-6007bf0f822014e0bbd0cef675848d18ae99b69e.tar.gz otp-6007bf0f822014e0bbd0cef675848d18ae99b69e.tar.bz2 otp-6007bf0f822014e0bbd0cef675848d18ae99b69e.zip |
Do not allocate a new map when the value is the same
This patch optimizes map operations to not allocate new maps
when the key is being replaced by the exact same value in memory.
Imagine this very common idiom:
Map#{key := compute_new_value(Value, Condition)}
where:
compute_new_x(X, true) -> X + 1;
compute_new_x(X, false) -> X;
In many cases, we are not changing the value in `Key`, however
the code prior to this patch would still allocate a new array
for the map values. This optimization changes this.
The cost of optimization is minimum, as in the worst case scenario
it only adds a pointer comparison and boolean check. The major benefit
is reducing the GC pressure by avoiding allocating data.
Next we list the operations we have changed alongside the benchmark
results. The benchmarks basically create a map and perform the same
operations, roughly 20000 times, once replacing the key with the same
value, and another with a different value.
* Map#{Key := Value}
For a map with 4 keys, replacing the fourth key 20000 times went from
718us to 539us.
For a map with 8 keys, replacing the fourth key 20000 times went from
976us to 555us.
* maps:update/3
For a map with 4 keys, replacing the fourth key 20000 times went from
673us to 575us.
For a map with 8 keys, replacing the fourth key 20000 times went from
827us to 585us.
* maps:put/3
For a map with 4 keys, replacing the fourth key 20000 times went from
763us to 553us.
For a map with 8 keys, replacing the fourth key 20000 times went from
788us to 561us.
Note that we have ported some optimizations found in maps:update/3
to maps:put/3 while creating this patch.
Diffstat (limited to 'erts/emulator/beam/erl_nif_api_funcs.h')
0 files changed, 0 insertions, 0 deletions