Skip to content

perf: Implement maps:find/2 and maps:get/3 without using exceptions - #2397

Open
alii wants to merge 1 commit into
atomvm:release-0.7from
alii:perf/maps-find-no-exceptions
Open

alii wants to merge 1 commit into
atomvm:release-0.7from
alii:perf/maps-find-no-exceptions

Conversation

@alii

@alii alii commented Sep 17, 2026 •

Copy link
Copy Markdown

This is one third of #2388

Today, maps:find/2 and maps:get/3 caught catch error:{badkey, _} and so every time you looked up a missing key, an exception was raised which generated stacktraces.

Instead we can use erlang:is_map_key/2 and erlang:map_get/2, keeping the {badmap, Map} error for non-map arguments (as OTP does).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@alii
alii force-pushed the perf/maps-find-no-exceptions branch from 286a0d5 to 124d88b Compare September 17, 2026 09:25
@alii
alii changed the base branch from main to release-0.7 September 17, 2026 09:25

@petermm petermm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: 286a0d56 — avoid exceptions in maps:find/2 and maps:get/3

Verdict

Approve with one non-blocking test suggestion.

I found no correctness, compatibility, or maintainability defects in the production change. The new implementations preserve the OTP behavior for hits, misses, and non-map inputs while avoiding the exception path on missing keys. Oracle independently reviewed the commit and reached the same conclusion.

Findings

[Suggestion] Add direct coverage for every rewritten maps:get/3 branch

Location: tests/libs/estdlib/test_maps.erl:82-90

The existing tests exercise maps:get/3 only for a missing key. The hit path and the new explicit non-map clause are not directly tested: the assertions currently at lines 88-89 call maps:get/2, not maps:get/3. maps:find/2 covers all three branches, but its generic bad-map helper does not verify that the invalid argument is retained in the exception reason.

This is not evidence of a production bug—the implementation matches OTP and the suite passes—but focused assertions would protect the exact behavior this commit changes.

diff --git a/tests/libs/estdlib/test_maps.erl b/tests/libs/estdlib/test_maps.erl
--- a/tests/libs/estdlib/test_maps.erl
+++ b/tests/libs/estdlib/test_maps.erl
@@ -84,9 +84,11 @@ test_get() ->
     ok = check_bad_map(fun() -> maps:get(bar, id(not_a_map)) end),
     ok = check_bad_key(fun() -> maps:get(bar, id(#{foo => bar})) end, bar),
 
     ?ASSERT_MATCH(maps:get(gnu, id(#{foo => bar}), gnat), gnat),
+    ?ASSERT_MATCH(maps:get(foo, id(#{foo => bar}), gnat), bar),
+    ?ASSERT_ERROR(maps:get(gnu, id({hello}), gnat), {badmap, {hello}}),
     ?ASSERT_ERROR(maps:get({hello}, id(#{foo => bar})), {badkey, {hello}}),
     ?ASSERT_ERROR(maps:get(gnu, id({hello})), {badmap, {hello}}),
     ok.
@@ -223,6 +225,7 @@ test_find() ->
     ?ASSERT_MATCH(maps:find(b, #{a => 1, b => 2, c => 3}), {ok, 2}),
     ?ASSERT_MATCH(maps:find(c, #{a => 1, b => 2, c => 3}), {ok, 3}),
     ?ASSERT_MATCH(maps:find(foo, #{a => 1, b => 2, c => 3}), error),
     ok = check_bad_map(fun() -> maps:find(foo, id(not_a_map)) end),
+    ?ASSERT_ERROR(maps:find(foo, id({hello})), {badmap, {hello}}),
     ok.

Review notes

  • erlang:is_map_key/2 and erlang:map_get/2 use the same exact-key lookup semantics in AtomVM.
  • Maps are immutable, so the membership check followed by retrieval cannot race with a mutation.
  • The successful path now performs two lookups, but that is the intended tradeoff for eliminating exception construction on misses; it is not a correctness issue.
  • OTP 29 confirms both functions raise error:{badmap, Map} for non-map arguments.

Verification

  • git diff HEAD^ HEAD --check — passed.
  • cmake --build build --target test_estdlib -j2 — passed.
  • build/src/AtomVM build/tests/libs/estdlib/test_estdlib.avm — passed; test_maps returned ok and the suite returned ok.

@alii
alii force-pushed the perf/maps-find-no-exceptions branch from 124d88b to e66b254 Compare September 17, 2026 17:29

@bettio bettio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for reporting one last issue: would you mind doing: git commit --amend --signoff so it passes DCO check?
By the way, there is a minor non-blocking change that can be done to CHANGELOG.md

Comment thread CHANGELOG.md
calling it with a port now raises `badarg` (previous versions accepted any id-carrying
term, so it could be used to read port information; there is no `erlang:port_info/2`
in AtomVM yet to migrate such code to)
- `maps:find/2` and `maps:get/3` no longer raise and catch an exception on a missing key

@bettio bettio Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reframe this as an optimization. Also let's append this at the end of the ### Changed 0.7.0-beta.0 section. We keep them in some sort of chronological order.

Comment thread libs/estdlib/src/maps.erl
Default
end.
get(Key, Map, Default) when is_map(Map) ->
case erlang:is_map_key(Key, Map) of

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're doing two lookups here.

OTP pattern only does one:

get(Key, Map, Default) when is_map(Map) ->
    case Map of
        #{Key := Value} -> Value;
        #{} -> Default
    end;

@pguyot

pguyot commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

Also AtomVM properly raises badmap before this change.

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

Successfully merging this pull request may close these issues.

4 participants