Skip to content

perf: Use a fast path for exact equality of immediates in select_val and is_eq_exact - #2398

Open
alii wants to merge 1 commit into
atomvm:release-0.7from
alii:perf/eq-exact-fast-path
Open

alii wants to merge 1 commit into
atomvm:release-0.7from
alii:perf/eq-exact-fast-path

Conversation

@alii

@alii alii commented Sep 17, 2026 •

Copy link
Copy Markdown

This is one third of #2388

is_eq_exact, is_not_eq_exact and select_val would always call term_compare, which for two atoms looks up both names in the atom table and memcmps them.

Two immediates with the same bits are always equal and two immediates with different bits are never equal. We can decide inline and only fall back to term_compare when a side is boxed or a list.

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

@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.

AMP review:

PR Review: exact-equality fast path

Commit reviewed: f424c95a97c5a0a74615af4907753141006e93d4
Verdict: Approve — no actionable correctness or regression findings.

Findings

No findings.

The fast path preserves exact-equality semantics:

  • Bit-identical terms were already treated as equal by the first branch of term_compare.
  • Distinct immediate terms are canonical and therefore cannot be exactly equal.
  • Lists, boxed values, and mixed representations still fall back to term_compare(..., TermCompareExact, ...).
  • Comparisons that may allocate still retain the existing TermCompareMemoryAllocFail handling in all three opcode implementations.
  • OP_SELECT_VAL continues decoding every value/label pair after finding a match, so the instruction pointer remains positioned correctly before the jump.

Oracle independently reviewed the commit's term-tag invariants, structural-comparison fallback, and OOM paths and likewise reported no actionable regressions or test gaps.

Suggested diffs

None. No small fix or follow-up suggestion is warranted by the reviewed code.

Verification

  • git diff HEAD^..HEAD --check — passed.
  • cmake --build build --target AtomVM -j2 — passed with only the pre-existing duplicate-library linker warning.
  • ./build/tests/test-erlang — passed (exit status 0).
  • ./build/tests/test-term — passed (exit status 0; this is a stale local artifact and is not part of the current CMake target set, so it is supplementary only).

The claimed performance improvement was not independently benchmarked as part of this correctness review.

@petermm

petermm commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

we want this based on release-0.7 right?

@alii

alii commented Sep 17, 2026

Copy link
Copy Markdown
Author

Sounds good let me do that!

@alii
alii force-pushed the perf/eq-exact-fast-path branch from f424c95 to 578b066 Compare September 17, 2026 09:25
@alii
alii changed the base branch from main to release-0.7 September 17, 2026 09:25

@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.

Just an important thing here and it can be merged: git commit --amend --signoff so it can pass DCO CI check.
Also there is a non-blocking issue: CHANGELOG entry might be improved in terms of clarity and order.

Comment thread CHANGELOG.md
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)
- Exact-equality opcodes (`is_eq_exact`, `is_not_eq_exact`, `select_val`) decide two immediates
without `term_compare`; atom `case` clauses no longer compare atom names through the atom table

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, so it is clearer to the end user. Something like: "Optimize atom comparison in ...". Let's also append this at the end of 0.7.0-beta.0 ### Changed section, so we keep this change in chronological order.

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)
- Exact-equality opcodes (`is_eq_exact`, `is_not_eq_exact`, `select_val`) decide two immediates

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.

This change is invisible to users and doesn't need to be mentioned here (except as faster comparison maybe).

Comment thread src/libAtomVM/term.h
*equal = false;
return true;
}
return false;

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.

Boxed vs immediate is also fast to compare.

I went through something similar here:

https://github.com/pguyot/AtomVM/blob/19a8b7738aa4ccc654b316d52f1796e182489058/src/libAtomVM/term.h#L2682

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