Skip to content

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Aug 21, 2025

This pull request optimizes both construction and matching of little-endian integer segments. See the commit messages for details.

bjorng added 3 commits August 21, 2025 07:38
Break out code for binary construction and matching that do not
depend on the architecture (x86_64 or AArch64).
In Erlang/OTP 26 (in erlang#6031), the JIT learned to optimize binary
construction such as:

    <<A:16/big, B:32/big, C:16/big>>

The optimization is done on the native-code level, but the idea behind
it can be illustrated in Erlang by rewriting the construction as
follows:

    Acc0 = A,
    Acc1 = (Acc0 bsl 32) bor B,
    Acc = (Acc1 bsl 16) bor C,
    <<Acc:64/big>>

When done in native code, the values of the segments is accumulated
into a CPU register, which is then written to memory. This is faster
than writing each segment to memory one at a time, especially if the
sizes are not byte-sized as in the following example:

    <<A:6, B:6, C:6, D:6>>

This commit introduces a similar optimization for little-endian
integer segments. Example:

    <<A:16/little, B:32/little, C:16/little>>

This expression can be rewritten as follows:

    Acc0 = C,
    Acc1 = (Acc0 bsl 32) bor B,
    Acc = (Acc1 bsl 16) bor A,
    <<Acc:64/little>>

Note that this rewriting is only safe if all segments except the last
one are byte-sized.
Multiple little-endians segments were handled one at a time. It turns
out that it is safe to match out multiple little-endian segments at
once, or even any mix of big and little endian segments at once.
@bjorng bjorng self-assigned this Aug 21, 2025
@bjorng bjorng added team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI labels Aug 21, 2025
@bjorng bjorng requested review from sverker and lucioleKi August 21, 2025 05:58
Copy link
Contributor

github-actions bot commented Aug 21, 2025

CT Test Results

    3 files    142 suites   49m 46s ⏱️
1 649 tests 1 592 ✅ 57 💤 0 ❌
2 372 runs  2 295 ✅ 77 💤 0 ❌

Results for commit 24e5b6c.

♻️ 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

@bjorng bjorng changed the title Bjorn/erts/bs little endian/otp 19747 Optimize binary syntax for little-endian segments Aug 21, 2025
@bjorng bjorng requested a review from jhogberg August 25, 2025 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant