Skip to content

perf(encode_sink): make Rope::len constant time - #782

Open
fallintoplace wants to merge 1 commit into
anthropics:mainfrom
fallintoplace:perf/rope-constant-time-len-20261019
Open

fallintoplace wants to merge 1 commit into
anthropics:mainfrom
fallintoplace:perf/rope-constant-time-len-20261019

Conversation

@fallintoplace

Copy link
Copy Markdown
Contributor

What

  • Make Rope::len() constant time by tracking finalized segment bytes.

Why

  • Length checks currently scan every output segment.

Implementation

  • Keep a running total for finalized segments and include the pending tail.
  • Add Criterion cases for length checks, append-and-check, and write costs.

Benchmark

  • Criterion, two runs: len() at 1/64/1,024 segments: 1.31/10.89/130.77 ns -> 0.92/0.94/0.92 ns.
  • Append then check: 16 segments 320-353 ns -> 292 ns; 128 segments 2.81-2.89 us -> 1.77-1.81 us; 1,024 segments 79.8-86.7 us -> 12.3-13.2 us.
  • Write checks (120 samples): small writes 404.6 -> 403.5 ns; shared segments 523.5 -> 528.7 ns (~1%).

@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant