Skip to content

Support byte ranges in binary patterns - #11473

Open
josevalim wants to merge 2 commits into
erlang:masterfrom
josevalim:jv-binary-matches-byte-ranges
Open

Support byte ranges in binary patterns#11473
josevalim wants to merge 2 commits into
erlang:masterfrom
josevalim:jv-binary-matches-byte-ranges

Conversation

@josevalim

@josevalim josevalim commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Allow non-empty lists of inclusive byte-range tuples
in binary:compile_pattern/1 and the binary match, matches,
split, and replace operations. Those ranges are compiled
into a new pattern which uses SIMD for up to 16 normalized
entries.

ASCII validation throughput on arm64 macOS (million calls/s):

                   SIMD   Unicode   SWAR   Guard
Valid ASCII       14.40      2.74   2.74    0.41
Invalid last      13.78      3.00   2.74    0.41
Invalid first     42.30    148.81  91.83  104.38

SIMD is the one added by this pull request, Unicode is
unicode:bin_is_7bit/1. Guard is the equivalent guard
implementation and a SWAR optimized guard. The native
unicode:bin_is_7bit/1 can be removed in a future commit.

As shown in the benchmarks above, the new byte ranges are
clear winners for validating data, as most times you are
expecting to fully traverse binaries without matches.
For parsing, byte ranges can be faster than guards, but may
be beaten by SWAR on certain payloads.

Implementation notes

The first commit is actually #11468. Without that commit,
the SWAR usage in json is much faster than this implementation
due to the cost of compiling the pattern. With that commit,
they are in the same order of magnitude, and one edges the
other depending on how frequent the matches are (with SIMD
being up to 3x faster when there are no matches). This pull
request will be rebased if and when that one is merged.

Also note this functionality could be added by adding new
functions, such as find_byte and find_bytes, but I thought
adding new compile patterns is the best approach as we can
reuse all infrastructure for match/matches/replace/split.

@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

CT Test Results

     5 files     545 suites   1h 54m 15s ⏱️
 4 552 tests  4 438 ✅ 113 💤 1 ❌
10 506 runs  10 367 ✅ 138 💤 1 ❌

For more details on these failures, see this check.

Results for commit 8555f4a.

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

Allow non-empty lists of inclusive byte-range tuples
in binary:compile_pattern/1 and the binary match, matches,
split, and replace operations. Normalize overlapping
ranges and compile them to the new br pattern type.

Use SIMD for up to 16 normalized entries, with equality
checks for singletons and one-sided comparisons for ranges
touching 0 or 255. Use the 256-byte membership table for
tails, unsupported SIMD targets, and larger range sets.

ASCII validation throughput on arm64 macOS (million calls/s):

                       SIMD   Unicode   SWAR   Guard
    Valid ASCII       14.40      2.74   2.74    0.41
    Invalid last      13.78      3.00   2.74    0.41
    Invalid first     42.30    148.81  91.83  104.38

SIMD is the one added by this pull request, Unicode stands
for the current unicode:bin_is_7bit/1. Guard is the equivalent
guard imlpementation and the SWAR one is an optimized version
of that. The native unicode:bin_is_7bit/1 can be removed in
a future commit.

As shown in the benchmarks above, the new byte ranges are
clear winners for validating data, as most times you are
expecting to fully traverse binaries without matches.
For parsing, byte ranges can be faster than guards, but may
be beaten by SWAR on certain payloads.
@josevalim
josevalim force-pushed the jv-binary-matches-byte-ranges branch from 4bc3603 to 8555f4a Compare August 12, 2026 11:44
@jhogberg jhogberg added the team:VM Assigned to OTP team VM label Aug 17, 2026
@jhogberg jhogberg self-assigned this Aug 17, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants