⚡ Borrow single-quoted scalars instead of copying char by char#224
Conversation
|
Warning Review limit reached
Next review available in: 58 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
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. Comment |
There was a problem hiding this comment.
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_quotedto 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_stringsbenchmark payload paired with the existingstrings_arrayso 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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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 allocatedString, and the result was always returned asCow::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::Borrowedon 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 withpush_strrather 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_stringspayload tobench/bench.py, paired with the existing plainstrings_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
Additional information
Checklist
uv run pytestpasses locally. A pull request cannot be merged unless CI is green.uv run ruff check .anduv run ruff format --check .pass.cargo fmt --checkandcargo clippy --all-targets -- -D warningspass.If the change is user-facing:
docs/is added or updated, anddocs/verify_examples.pystill passes.