Skip to content

⚡ Find the single-quoted scalar end with SIMD memchr#228

Merged
frenck merged 2 commits into
mainfrom
frenck/memchr-single-quoted
Jul 5, 2026
Merged

⚡ Find the single-quoted scalar end with SIMD memchr#228
frenck merged 2 commits into
mainfrom
frenck/memchr-single-quoted

Conversation

@frenck

@frenck frenck commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Breaking change

None. Pure internal optimization; parse results are byte-for-byte identical.

Proposed change

Follow-up to the quoted-scalar work (#224, #225, #226). The single-quoted content scan introduced in #226 walked the run one byte at a time, testing each byte against the closing quote, the line breaks, and the non-ASCII range.

This replaces that loop with a SIMD memchr3 search for the next stop byte (', \n, \r), so a long scalar (a description, an embedded script, a base64 blob) is skipped in a handful of vector loads instead of a comparison per byte. Column tracking stays exact: column is per character, so it advances by the run's byte length when the run is pure ASCII (the common case, checked with a SIMD is_ascii) and by an exact character count only when the run carries multi-byte content. memchr was already a transitive dependency (via regex), so promoting it to a direct dependency adds no build cost.

Single-quoted has exactly three stop bytes, a natural fit for memchr3; double-quoted has four (it also stops at \), so it keeps its byte loop.

Measured with callgrind on a multi-document single-quoted corpus: ~3% fewer instructions overall (its scalars are mostly short). The win scales with scalar length: on a stream of long single-quoted strings the scan is several times faster, since the per-byte loop is replaced by vector search. Behavior is unchanged: the full suite, the YAML compliance suite, all Rust unit tests, and round-trip plus parse fuzzing pass with no differences.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

Checklist

  • I have read the AI Policy, and this pull request was not created by an autonomous agent.
  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and uv run pytest passes locally. A pull request cannot be merged unless CI is green.
  • uv run ruff check . and uv run ruff format --check . pass.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.
  • Round-trip fidelity is preserved: an unmodified document still re-emits byte-for-byte.
  • No commented-out or dead code is left in the pull request.

If the change is user-facing:

  • Documentation under docs/ is added or updated, and docs/verify_examples.py still passes.

Copilot AI review requested due to automatic review settings July 5, 2026 17:43
@frenck frenck added the performance Improving performance, not introducing new features. label Jul 5, 2026
@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@frenck, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 50 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 339a93d3-a487-4fbe-bcd6-01d2da0f9c3f

📥 Commits

Reviewing files that changed from the base of the PR and between ab21d8a and e900f33.

📒 Files selected for processing (1)
  • src/scanner/reader.rs
📝 Walkthrough

Walkthrough

Changes

A memchr = "2" dependency was added to Cargo.toml. The Reader::take_single_quoted_run function in src/scanner/reader.rs was reimplemented to use memchr::memchr3 to locate the next single quote, newline, or carriage return, replacing the previous byte-by-byte scan that also stopped on non-ASCII bytes. Column tracking now uses byte length for ASCII runs and UTF-8 character counting for non-ASCII runs, with pos advanced to the computed stop index.

Sequence Diagram(s)

Not applicable; the change is a scanning algorithm modification without cross-component interaction changes.

Related Issues: None referenced in the provided information.

Related PRs: None referenced in the provided information.

Suggested labels: performance, dependencies, scanner

Suggested reviewers: frenck


Poem
A rabbit twitched its whiskers keen,
found memchr's speed, both swift and clean,
through quotes and lines it darts ahead,
counting chars where UTF-8 led,
one hop, one scan, the scalar's read.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the SIMD memchr optimization for single-quoted scalar scanning.
Description check ✅ Passed The description matches the changeset and accurately explains the performance-focused scanner update.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jul 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing frenck/memchr-single-quoted (e900f33) with main (582cf73)

Open in CodSpeed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes the YAML scanner’s single-quoted scalar fast path by replacing the per-byte stop-byte loop with a SIMD-accelerated memchr3 search, improving throughput on long single-quoted scalars while preserving identical parse results.

Changes:

  • Replace the single-quoted scalar run scan with memchr::memchr3 and adjust column tracking to handle non-ASCII runs accurately.
  • Promote memchr to a direct Rust dependency (already present transitively) and record it in the lockfile.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/scanner/reader.rs Reworks take_single_quoted_run to locate stop bytes via memchr3 and updates column advancement logic accordingly.
Cargo.toml Adds memchr = "2" as a direct dependency with rationale comments.
Cargo.lock Records memchr as a direct dependency of the crate.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/scanner/reader.rs
@codecov-commenter

codecov-commenter commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.52%. Comparing base (4fd21ad) to head (e900f33).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
- Coverage   92.55%   92.52%   -0.03%     
==========================================
  Files          41       41              
  Lines       12782    12837      +55     
==========================================
+ Hits        11830    11878      +48     
- Misses        952      959       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frenck frenck merged commit da0d3e2 into main Jul 5, 2026
77 checks passed
@frenck frenck deleted the frenck/memchr-single-quoted branch July 5, 2026 18:14
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

performance Improving performance, not introducing new features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants