Skip to content

Optimize String code unit search with V128#3816

Open
Yu-zh wants to merge 1 commit into
mainfrom
Yu-zh/string-contains-code-unit
Open

Optimize String code unit search with V128#3816
Yu-zh wants to merge 1 commit into
mainfrom
Yu-zh/string-contains-code-unit

Conversation

@Yu-zh

@Yu-zh Yu-zh commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add builtin V128 helpers for loading and comparing eight UTF-16 code units
  • optimize String::contains_code_unit and StringView::contains_code_unit with eight-wide scans on native and wasm targets
  • retain the scalar implementation for other targets and for short/tail ranges
  • add SIMD block, view-boundary, surrogate-code-unit, and benchmark coverage

Why

Code-unit searches previously inspected one UTF-16 code unit at a time. The new intrinsic allows native and wasm backends to scan eight code units per iteration while preserving the existing behavior and public API.

Impact

Long missing or late-match searches are substantially faster on SIMD-enabled targets. String views continue to honor their exact backing-string bounds, and non-native/non-wasm targets keep the scalar path. Generated package interfaces are unchanged.

Benchmark

Isolated native ARM64 release runs comparing the scalar baseline with this implementation (lower is better):

Case Scalar baseline V128 Speedup
String, absent, n=4 7.84 ns 6.50 ns 1.21×
String, absent, n=8 8.81 ns 6.49 ns 1.36×
String, present at 0, n=8 5.77 ns 5.69 ns 1.01×
String, absent, n=256 82.26 ns 16.10 ns 5.11×
offset StringView, absent, n=256 86.38 ns 17.47 ns 4.94×

The SIMD path improves full scans substantially while keeping the immediate-match case effectively unchanged.

Validation

  • moon fmt
  • moon info (no .mbti changes)
  • moon check --target all -p builtin
  • moon test --target all -p builtin
    • wasm: 2,886 passed
    • wasm-gc: 2,886 passed
    • JavaScript: 2,874 passed
    • native: 2,844 passed
  • moon test: 6,716 passed

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 5169

Coverage decreased (-0.1%) to 91.111%

Details

  • Coverage decreased (-0.1%) from the base build.
  • Patch coverage: 24 uncovered changes across 2 files (5 of 29 lines covered, 17.24%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
builtin/simd.mbt 17 0 0.0%
builtin/string_methods.mbt 12 5 41.67%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 17291
Covered Lines: 15754
Line Coverage: 91.11%
Coverage Strength: 190188.93 hits per line

💛 - Coveralls

@Yu-zh
Yu-zh marked this pull request as ready for review July 16, 2026 06:56
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.

2 participants