You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Count node end positions on from the previous one - #706
Loading a document with OPT_ROUND_TRIP (and OPT_ANNOTATED, includes, schema validation and upgrade(), which compose the same tree) was quadratic on one long line, like minified JSON in a flow collection. For every node, Composer::end_position turned its end offset into a line and column by counting characters from the start of the line. On a single 689 KB line holding a 100,000 item flow sequence, a round-trip load took 1,456 ms. It now takes 56 ms.
How it works:
The composer keeps the last position it worked out (EndCursor in src/roundtrip/composer.rs) and counts on from there. Nodes finish in the order their ends appear in the source (children before their parent, and an empty node takes the position of the token after it), so the next end is nearly always on the same line and not before it.
A step back on the same line counts from whichever is closer, the cursor or the line start, and an end on another line falls back to the old binary search. No call costs more than it did before.
The parser, Event and loads are untouched. Putting end positions on the events was the other option: it costs 8 bytes per event against the 80 byte limit and scanner work that loads pays for and never reads.
Positions are unchanged. A Rust test checks every node end against an independent count from the start of the input, over a leading BOM, CRLF, a lone CR, mixed line breaks, multibyte characters, tabs, flow and block collections, empty keys and values, aliases, block scalars, several documents and a long line. A second test asks for every character boundary in forward, reversed and scattered order. Across the local real-world corpus and the YAML test suite, all 9,155,737 node ends in 26,564 files match the old computation.
In tests/robustness/test_scaling.py, the two strict xfail long-line cases now pass and moved to the linear cases at larger sizes (the old small runs took 1 to 3 ms, too short to time). New: a long line full of empty keys and values (the shape that could step back), and annotated long flow sequence and mapping, which the file left out before because the quadratic and the annotated work drowned each other.
Instructions in the extension (callgrind):
Load
main
this PR
change
loads, config sized document
221.79M
221.79M
none
loads, large block document
371.06M
371.10M
noise
round-trip, config sized document
315.3M
305.8M
-3.0%
round-trip, large block document
546.9M
521.2M
-4.7%
round-trip, 10,000 pair flow mapping on one line
856.6M
41.8M
-95%
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)
The round-trip composer turned every node's end offset into a line and
column by counting characters from the start of its line. On one long
line, like minified JSON in a flow collection, that adds up to the
square of the line length.
Nodes finish in the order their ends appear, so the composer now keeps
the last position it worked out and counts on from there. A step back
on the same line counts from whichever is closer, the cursor or the
line start, and another line falls back to the old search. Positions
are unchanged.
No actionable comments were generated in the recent review.
Walkthrough
ComposeState now tracks the last end-position offset, line, and column. It reuses this cursor for offsets on the same line and uses binary-search line lookup for offsets on other lines. Tests compare calculated positions with an independent counter and add scaling coverage for long flow sequences and mappings.
Possibly related PRs
frenck/YAMLRocks#696: This PR changes how node end positions are derived during the comment walk. PR #706 changes end-position calculation in the round-trip composer.
Merge Risk: Minimal · up to 95b84
This change makes node end-position calculation on very long single-line documents roughly linear instead of quadratic, without altering the positions produced. Tests compare results against an independent counter, and scaling tests cover long flow collections. No unresolved defect was found, so the merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "Count node end positions on from the previous one" matches the main change: the composer now counts end positions forward from the last computed cursor instead of recounting from line start…
Description check
The description is directly related to the changeset. It explains the quadratic end-position computation in Composer::end_position, the new EndCursor that counts from the previous position, the fallba…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "Count node end positions on from the previous one" matches the main change: the composer now counts end positions forward from the last computed cursor instead of recounting from line start. The summary and objectives both describe this cursor-based counting as the primary change. The title is short, specific, and a reviewer scanning history would understand the primary change. Minor wording is awkward, but the meaning is clear.
Full details: Description check
Explanation
The description is directly related to the changeset. It explains the quadratic end-position computation in Composer::end_position, the new EndCursor that counts from the previous position, the fallback behaviour for other lines, the Rust and Python test changes in tests/robustness/test_scaling.py, and benchmark tables. The template sections are filled in with substantive content.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 4c99b050-21c9-4324-acd3-a316e50c5013
Commits
Reviewing files that changed from the base of the PR and between 8c649d5 and 95b8444.
Files selected for processing: 1
src/roundtrip/composer.rs
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
🟢 Approval recommended
No active review issues are recorded for the end-position tracking optimization.
No open issues 🎉
What changed in this PR
Optimizes node end-position tracking in YAMLRocks’ shared composer, addressing the long-line performance bottleneck left after parser consolidation without changing source positions.
Changes:
Reuses the previous end position to avoid repeated line-prefix scans.
Adds position checks across Unicode, line endings, and traversal orders.
Promotes long-line scaling tests from expected failures and adds annotated-load coverage.
File
Description
tests/robustness/test_scaling.py
Expands linear-scaling coverage for long flow collections and empty entries.
src/roundtrip/composer.rs
Adds cached end-position tracking and independent correctness tests.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 7daa0353-258d-4596-a6aa-38e44abdb355
Commits
Reviewing files that changed from the base of the PR and between a4cf5fe and 8c649d5.
Files selected for processing: 2
src/roundtrip/composer.rs
tests/robustness/test_scaling.py
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.
An empty node can sit at an earlier offset than the node before it.
Answering it moved the cursor back, so the next node would count the
line again from there. Keep the cursor at the furthest position
instead; the earlier query still gets its own answer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
performanceImproving performance, not introducing new features.rust
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change
None.
Proposed change
Loading a document with
OPT_ROUND_TRIP(andOPT_ANNOTATED, includes, schema validation andupgrade(), which compose the same tree) was quadratic on one long line, like minified JSON in a flow collection. For every node,Composer::end_positionturned its end offset into a line and column by counting characters from the start of the line. On a single 689 KB line holding a 100,000 item flow sequence, a round-trip load took 1,456 ms. It now takes 56 ms.How it works:
EndCursorinsrc/roundtrip/composer.rs) and counts on from there. Nodes finish in the order their ends appear in the source (children before their parent, and an empty node takes the position of the token after it), so the next end is nearly always on the same line and not before it.Eventandloadsare untouched. Putting end positions on the events was the other option: it costs 8 bytes per event against the 80 byte limit and scanner work thatloadspays for and never reads.Positions are unchanged. A Rust test checks every node end against an independent count from the start of the input, over a leading BOM, CRLF, a lone CR, mixed line breaks, multibyte characters, tabs, flow and block collections, empty keys and values, aliases, block scalars, several documents and a long line. A second test asks for every character boundary in forward, reversed and scattered order. Across the local real-world corpus and the YAML test suite, all 9,155,737 node ends in 26,564 files match the old computation.
In
tests/robustness/test_scaling.py, the two strict xfail long-line cases now pass and moved to the linear cases at larger sizes (the old small runs took 1 to 3 ms, too short to time). New: a long line full of empty keys and values (the shape that could step back), and annotated long flow sequence and mapping, which the file left out before because the quadratic and the annotated work drowned each other.Instructions in the extension (callgrind):
loads, config sized documentloads, large block documentType 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.