Age | Commit message (Collapse) | Author |
|
When matching on a literal map, the map is placed into the general
scratch register first. This is fine in isolation, but when the
key to be matched was in a Y register it would also be placed in
the scratch register, overwriting the map and crashing the
emulator.
|
|
This complements the `map_get/2` guard BIF introduced in #1784.
Rationale.
`map_get/2` allows accessing map fields in guards, but it might be
problematic in more complex guard expressions, for example:
foo(X) when map_get(a, X) =:= 1 or is_list(X) -> ...
The `is_list/1` part of the guard could never succeed since the
`map_get/2` guard would fail the whole guard expression. In this
situation, this could be solved by using `;` instead of `or` to separate
the guards, but it is not possible in every case.
To solve this situation, this PR proposes a `is_map_key/2` guard that
allows to check if a map has key inside a guard before trying to access
that key. When combined with `is_map/1` this allows to construct a
purely boolean guard expression testing a value of a key in a map.
Implementation.
Given the use case motivating the introduction of this function, the PR
contains compiler optimisations that produce optimial code for the
following guard expression:
foo(X) when is_map(X) and is_map_key(a, X) and map_get(a, X) =:= 1 -> ok;
foo(_) -> error.
Given all three tests share the failure label, the `is_map_key/2` and
`is_map/2` tests are optimised away.
As with `map_get/2` the `is_map_key/2` BIF is allowed in match specs.
|
|
Rationale
Today all compound data types except for maps can be deconstructed in guards.
For tuples we have `element/2` and for lists `hd/1` and `tl/1`. Maps are
completely opaque to guards. This means matching on maps can't be
abstracted into macros, which is often done with repetitive guards. It
also means that maps have to be always selected whole from ETS tables,
even when only one field would be enough, which creates a potential
efficiency issue.
This PR introduces an `erlang:map_get/2` guard-safe function that allows
extracting a map field in guard. An alternative to this function would be
to introduce the syntax for extracting a value from a map that was planned
in the original EEP: `Map#{Key}`.
Even outside of guards, since this function is a guard-BIF it is more
efficient than using `maps:get/2` (since it does not need to set up the
stack), and more convenient from pattern matching on the map (compare:
`#{key := Value} = Map, Value` to `map_get(key, Map)`).
Performance considerations
A common concern against adding this function is the notion that "guards
have to be fast" and ideally execute in constant time. While there are
some counterexamples (`length/1`), what is more important is the fact
that adding those functions does not change in any way the time
complexity of pattern matching - it's already possible to match on map
fields today directly in patterns - adding this ability to guards will
niether slow down or speed up the execution, it will only make certain
programs more convenient to write.
This first version is very naive and does not perform any optimizations.
|
|
This function is no longer needed as maps:iterator has
now been implemented.
|
|
This iterator implementation fetches multiple elements to
iterate over in one call to erts_internal:maps_next instead
of one at a time. This means that the memory usage will go
up for the iterator as we are buffering elements, but the
usage is still bounded.
In this implementation the max memory usage is 1000 words.
Using this approach makes the iterator as fast as using
maps:to_list, so maps:iterator/2 has been removed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* bjorn/deprecate-random/OTP-12502:
Deprecate the 'random' module
Eliminate mentions of 'random' in documentation
mnesia tests: Replace 'random' with 'rand'
percept tests: Replace 'random' with 'rand'
system tests: Replace 'random' with 'rand'
common_test tests: Replace 'random' with 'rand'
gs: Remove the contribs directory
wx: Replace 'random' with 'rand'
stdlib tests: Replace 'random' with 'rand'
kernel test: Replace 'random' with 'rand'
debugger tests: Replace 'random' with 'rand'
compiler tests: Replace 'random' with 'rand'
Emulator test suite: Replace use of 'random' with 'rand'
Use 'rand' instead of the obsolete 'random' module
compile: Eliminate use of the obsolete 'random' module
|
|
|
|
* sverk/dist-ctrl-msg-overflow-master:
erts: Tweak hashmap heap size estimation
erts: Fix bug for remote control message containing fat maps
erts: Add test for remote exit signal with fat map
erts: Fix bug in heap_factory_undo for FACTORY_HEAP_FRAGS mode
|
|
to support other terms, not just maps
|
|
|
|
* sverk/rare-gc-map-overflow/OTP-13146:
erts: Fix rare case of faulty heap fragment deallocation
|
|
after major GC.
Can only be caused by distributed messages containing large maps.
Bad map hashing will increase the risk.
|
|
|
|
* sverk/map-merge-trap:
erts: Expand test map_SUITE:t_bif_merge_and_check
|
|
with merge of randomized maps.
|
|
|
|
Reported-by: Rory Byrne
|
|
* rickard/time-improvement/OTP-11997:
Allow execution of estone suite on pre OTP-18 systems
Add parallel time monotonicity test-case
Replace usage of erlang:now() in line-tracing
Replace erlang:now() usage in emulator suite
Replace erlang:now() usage in system suite
Misc time improvements
|
|
|
|
According to EEP-43 for maps, a 'badmap' exception should be
generated when an attempt is made to update non-map term such as:
<<>>#{a=>42}
That was not implemented in the OTP 17.
José Valim suggested that we should take the opportunity to
improve the errors coming from map operations:
http://erlang.org/pipermail/erlang-questions/2015-February/083588.html
This commit implement better errors from map operations similar
to his suggestion.
When a map update operation (Map#{...}) or a BIF that expects a map
is given a non-map term, the exception will be:
{badmap,Term}
This kind of exception is similar to the {badfun,Term} exception
from operations that expect a fun.
When a map operation requires a key that is not present in a map,
the following exception will be raised:
{badkey,Key}
José Valim suggested that the exception should be
{badkey,Key,Map}. We decided not to do that because the map
could potentially be huge and cause problems if the error
propagated through links to other processes.
For BIFs, it could be argued that the exceptions could be simply
'badmap' and 'badkey', because the bad map and bad key can be found in
the argument list for the BIF in the stack backtrace. However, for the
map update operation (Map#{...}), the bad map or bad key will not be
included in the stack backtrace, so that information must be included
in the exception reason itself. For consistency, the BIFs should raise
the same exceptions as update operation.
If more than one key is missing, it is undefined which of
keys that will be reported in the {badkey,Key} exception.
|
|
In the update loop for big maps, the E variable is restored for
each turn of the loop. It only needs to be restored if a garbage
collection has been performed.
Also add a new test case that attempts to force several garbage
collections while updating a map, to help us find bugs with
incorrect restoration of the E variable after a garbage collection.
|
|
The map instructions require that the keys in the instructions
are sorted (for flatmaps). But that is an implementation detail
that should not exposed outside of the BEAM virtual machine.
Therefore, make the sorting of the keys the responsibility of
the loader and not the compiler.
Also note that the sort order for maps with numeric keys or keys
with numeric components has changed in OTP 18. That means that
code compiled for OTP 17 that operated on maps with map keys
might not work in OTP 18 without the sorting in the loader
(although it is unlikely to be an issue in practice).
|
|
The has_map_fields instruction was not tested at all by
erts/map_SUITE.erl
|
|
To be sure that the compiler and BEAM virtual machine correctly
handles literals maps, we must test it.
|
|
* egil/maps-test-coverage:
erts: Remove code that was commented out
erts: Cover maps:values/1 for large maps
erts: Test maps:from_list/1 shrinking
|
|
|
|
Repeated keys will shrink map to a flatmap if the number of pairs drops below the limit.
|
|
Reported-by: Jesper Louis Andersen
|
|
* egil/fix-maps-tmp-heap:
erts: Test deep Maps updates
erts: Use halfword secure tmp heap
erts: Remove unused tmp heap in make_internal_hash
Conflicts:
erts/emulator/test/map_SUITE.erl
|
|
* egil/fix-maps-from_list-size:
erts: Strengthen Maps merge tests
erts: Try to test deep Maps collision
erts: Fix size bug in maps:from_list/1 BIF
|
|
|
|
|
|
Ensure maps:size/1 is correct.
|
|
|
|
* egil/fix-make_internal_hash-float:
erts: Add tests for internal_hash
erts: Fix make_internal_hash for 0.0 vs -0.0
|
|
|
|
|
|
* sverk/hamt-term2bin-bug/OTP-12585:
erts: Optimize hashmap_get
erts: Remove HAMT_SUBTAG_NODE_ARRAY
erts: Fix bug in binary_to_term for hamt when yielding
erts: Rename to flatmap_from_validated_list
|
|
Conflicts:
erts/emulator/beam/erl_map.c
erts/emulator/test/map_SUITE.erl
|
|
This will also fix a bug in term_to_binary
treating full nodes as tuples and emiting LIST_EXT for leafs.
|
|
Comparison of exact terms could cause faulty term tests.
This was caused by a faulty (too small) internal type.
Symptom:
-1 = erts_internal:cmp_term(2147483648,0). %% wrong
Correct:
1 = erts_internal:cmp_term(2147483648,0).
Reported-by: Jesper Louis Andersen
|
|
|
|
|
|
|