Skip to content

Count node end positions on from the previous one - #706

Merged
frenck merged 2 commits into
mainfrom
frenck/end-positions
Dec 4, 2026
Merged

frenck merged 2 commits into
mainfrom
frenck/end-positions

Conversation

@frenck

@frenck frenck commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Breaking change

None.

Proposed change

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)
  • 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.

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.
@frenck frenck added performance Improving performance, not introducing new features. rust labels Dec 3, 2026
Copilot AI balanced review requested due to automatic review settings December 3, 2026 22:59
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 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.
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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

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.

🟢 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.

Review effort: Balanced

@coderabbitai coderabbitai Bot 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.

Actionable comments

Major 1
  1. Do not discard cursor progress for synthetic empty values.
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

Comment thread src/roundtrip/composer.rs Resolved
@codspeed

codspeed Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

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.

Learn more about system calls

See affected benchmarks

✅ 16 untouched benchmarks


Comparing frenck/end-positions (95b8444) with main (a4cf5fe)

Open in CodSpeed

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.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 23:30

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.

🟢 Approval recommended

No active review issues remain for the change to incremental node-end position counting.

No open issues 🎉

Review effort: Balanced

@frenck
frenck merged commit 2935faf into main Dec 4, 2026
51 checks passed
@frenck
frenck deleted the frenck/end-positions branch December 4, 2026 00:15
@frenck frenck mentioned this pull request Dec 4, 2026
9 of 15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Improving performance, not introducing new features. rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants