Skip to content

⚡ Borrow single-quoted scalars instead of copying char by char#224

Merged
frenck merged 1 commit into
mainfrom
frenck/zero-copy-single-quoted-scalars
Jul 5, 2026
Merged

⚡ Borrow single-quoted scalars instead of copying char by char#224
frenck merged 1 commit into
mainfrom
frenck/zero-copy-single-quoted-scalars

Conversation

@frenck

@frenck frenck commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Breaking change

None. Pure internal optimization; parse results and round-trip output are byte-for-byte identical.

Proposed change

The single-quoted scalar scanner walked the content character by character: every byte was decoded to a char, pushed into a freshly allocated String, and the result was always returned as Cow::Owned, even for the overwhelmingly common case of a scalar with no '' escape and no line fold. It also checked for a ---/... document marker on every character, when a marker can only ever begin at column 0.

This rewrites it to mirror the plain-scalar scanner: it tracks byte offsets and returns Cow::Borrowed on the input slice between the quotes when the scalar is clean (no escape, no fold), so the common case allocates nothing. An owned buffer is materialized lazily only when a '' escape or a line fold forces a transformation, and each clean run is then bulk-copied with push_str rather than a char at a time. The document-marker check moves out of the per-character loop to run once, after a fold lands on a continuation line.

Measured on a quoted-scalar-heavy load with callgrind (deterministic instruction counts): ~27% fewer instructions (1.572B to 1.148B). Plain scalars already scanned this way; this brings single-quoted scalars up to parity. Behavior is unchanged: the full suite, the YAML compliance suite, all Rust unit tests, and 1.14M round-trip fuzz runs pass with no differences.

Also adds a quoted_strings payload to bench/bench.py, paired with the existing plain strings_array, so the quoted-scalar path is benchmarked alongside the plain one and this cannot silently regress. (A double-quoted equivalent will follow as a separate change.)

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to:
  • Link to a separate documentation pull request:

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 15:24
@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

Warning

Review limit reached

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

Next review available in: 58 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: d08ffc9a-7099-4ce3-83f8-ef53952ea689

📥 Commits

Reviewing files that changed from the base of the PR and between 7ada98c and 73a7401.

📒 Files selected for processing (2)
  • bench/bench.py
  • src/scanner/scalar.rs

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/zero-copy-single-quoted-scalars (73a7401) with main (7ada98c)

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

This PR is a pure internal optimization of the single-quoted scalar scanner in src/scanner/scalar.rs. Previously the scanner decoded and pushed every byte into a freshly allocated String and always returned Cow::Owned, and it checked for a document marker on every character. The rewrite mirrors the existing plain-scalar scanner: it tracks byte offsets and returns Cow::Borrowed for the common clean case (no '' escape, no line fold), materializing an owned buffer only when a transformation is required, and moves the document-marker check out of the per-character loop to run once after a fold. Parse results and round-trip output are byte-for-byte identical (~27% fewer instructions on a quoted-heavy load).

Changes:

  • Rewrote scan_single_quoted to borrow the input slice between quotes with zero allocation in the common case, materializing an owned buffer lazily on escape/fold.
  • Moved the ---/... document-marker check to run once after a fold (column 0 only) instead of on every character.
  • Added a quoted_strings benchmark payload paired with the existing strings_array so the quoted-scalar path is measured alongside the plain one.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/scanner/scalar.rs Rewrites scan_single_quoted to use the lazy-owned Cow borrow pattern and relocates the document-marker check out of the per-char loop.
bench/bench.py Adds a QUOTED_STRINGS payload to benchmark the single-quoted scalar path.

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

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.51%. Comparing base (7ada98c) to head (73a7401).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #224   +/-   ##
=======================================
  Coverage   92.50%   92.51%           
=======================================
  Files          41       41           
  Lines       12708    12720   +12     
=======================================
+ Hits        11756    11768   +12     
  Misses        952      952           

☔ 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 9d666c2 into main Jul 5, 2026
74 of 75 checks passed
@frenck frenck deleted the frenck/zero-copy-single-quoted-scalars branch July 5, 2026 15:41
@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