Skip to content

perf(core): scan for bytes with memchr - #860

Merged
Brooooooklyn merged 1 commit into
mainfrom
perf/memchr-scans
Oct 18, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
perf/memchr-scans

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Byte scans in the resolve and load hooks now use the memchr crate wherever it is faster. memchr was already a dependency, and decode_url_path, has_malformed_escape and the code-frame helpers already used it.

What changed

Before After Sites
contains('#'), contains('?'), contains('\\'), find(':') memchr TsconfigSource::discover, is_bare_specifier, file_url_to_path, resolver_request, decode_path_string (Windows)
find(['?', '#']), find(['/', '\\']) memchr2 url_path, url_to_path / unc_path_to_url (Windows)
contains(['\t', '\n', '\r']) memchr3 create_resolve, url_to_path (Windows)
contains(['%', '\\', '?', '#']) memchr3 + memchr the path-reference check in create_resolve
windows(3).any(…) for %2F / %5C memchr_iter(b'%') has_encoded_separator
split(b'\n').any(…) memchr_iter(b'\n') frame_too_large
rfind('\n') memrchr line_column
iter().position(|b| b == b'%') memchr decode_path_string, has_forbidden_host_escape (Windows)

A char set decodes one character at a time. memchr compares a vector register's worth of bytes at a time.

Measurements

Release build on Apple Silicon, with typical specifiers, paths and URLs as input. Each figure is the time per call.

Scan Before After
contains('#') 5.89 ns 2.90 ns
contains('\\') on a path 7.12 ns 2.66 ns
find(':') 4.19 ns 2.79 ns
three-byte set 6.50 ns 2.78 ns
four-byte set 14.29 ns 4.70 ns
find(['?', '#']) on a URL 33.04 ns 1.88 ns
%2F / %5C window scan 20.41 ns 3.32 ns

Left unchanged

  • Substring searches, such as contains("/node_modules/"). str::contains(&str) is about as fast as a static memmem::Finder (4.12 ns against 4.43 ns), and a one-shot memmem::find is slower (22.79 ns).
  • Byte-class scans, such as bytes().any(|b| b <= b' ') and the percent-encode check. memchr can only search for fixed byte values, not a class.

Tests

  • New unit test frame_too_large_measures_each_line. It covers the first, middle and last line at exactly the length limit and one byte over, with and without a trailing newline.
  • has_encoded_separator's existing test already covers a %2 at the end of the input.
  • cargo test --all-features, cargo clippy --all-targets --all-features -- -D warnings, vp run test (also with OXC_LOG=debug) and vp run lint all pass.
  • The windows_file_url.rs changes compile only on Windows, so they rely on CI.

πŸ€– Generated with Claude Code

Single-byte searches and two- or three-byte sets in the resolve and load
hooks now go through memchr, memchr2, memchr3 and memrchr instead of
str::contains / str::find with a char or a char set. A char set decodes
every character; memchr compares a vector register's worth of bytes at a
time. The %2F / %5C check and the per-line frame-size check walk memchr
hits instead of every byte.

Substring searches (`/node_modules/`) and byte-class scans are unchanged:
str::contains(&str) is already as fast as a memmem::Finder, and memchr
cannot express a class.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-10-17T04:58:35.588008Z 3e833fa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@Brooooooklyn
Brooooooklyn merged commit c4256da into main Oct 18, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the perf/memchr-scans branch October 18, 2026 04:10
Brooooooklyn added a commit that referenced this pull request Oct 20, 2026
Follow-up to #860. An audit of every byte and string scan left in
`src/`, keeping only the changes a benchmark shows are faster.

## Changed

| Where | Before | After | Speed (aarch64, M-series) |
|---|---|---|---|
| `TryAsStr::try_as_str`: every CommonJS source the load hook checks, up
to twice per file | `std::str::from_utf8` | `simdutf8` (`compat` where
the error text is shown) | 2.0x–3.3x on ASCII 2 KB–200 KB, 6.7x on a 300
KB non-ASCII file |
| `encode_file_url_path`: "does this path need `%XX`?", on every
resolved file URL | `bytes().any(encoded)` |
`any_byte(file_url_encoded)` | 5x–5.8x on 34–117 byte paths |
| `is_bare_specifier`: "any byte ≀ space?" | `bytes().any(..)` |
`any_byte(..)` | 2.5x–12x on 18–96 byte specifiers |

`any_byte` checks 16 bytes at a time with no early exit inside a chunk,
so LLVM emits vector compares (NEON, SSE2). The encode set is rewritten
as ranges and compares joined by `|`: the old `matches!` compiles to a
bit test per byte, which does not vectorize (that form measured 0.8x,
slower than `any`).

## Looked at, left as is

| Where | Why |
|---|---|
| `contains("/node_modules/")` (5 places) | `memmem` with a static
`Finder` measured 0.5x–1.3x against `str::contains` on 34–117 byte
paths: no win |
| `windows_file_url.rs` host and segment checks | Windows-only, short
UNC hosts and segments, rare |
| line and column counting for error frames | only on a syntax error |
| the remaining `replace` / `split` with one char | already guarded by a
`memchr` check, or run once at startup |

## Tests

- `file_url_encoded_is_the_path_to_file_url_set`: the compare form
matches the old `matches!` set for all 256 bytes.
- `any_byte_is_iterator_any`: same answer as `Iterator::any` for lengths
0–40, with the hit at every position.
- `cargo test --all-features` (49), clippy with warnings denied, and `vp
run test` (272 passed, 7 skipped) all pass.

The numbers come from a standalone micro-benchmark (release, LTO,
`codegen-units = 1`) on Apple Silicon. They are not measured on x86_64.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant