Skip to content

Implement advance_back_by for str::Chars - #163726

Open
CarloTUM wants to merge 1 commit into
rust-lang:mainfrom
CarloTUM:chars-advance-back-by
Open

CarloTUM wants to merge 1 commit into
rust-lang:mainfrom
CarloTUM:chars-advance-back-by

Conversation

@CarloTUM

@CarloTUM CarloTUM commented Oct 3, 2026

Copy link
Copy Markdown

Chars already has a specialized advance_by (added in 40cf1f9, counts char start bytes in 32 byte chunks), but DoubleEndedIterator for Chars used the default advance_back_by, so chars().nth_back(n), chars().rev().skip(n) / .rev().nth(n) decoded every char one by one.

This mirrors the forward implementation from the back: count non-continuation bytes in 32 byte chunks via as_rchunks, and if the skipped bytes begin in the middle of a char (whose leading byte therefore wasn't counted), keep its continuation bytes in the iterator. The remainder is skipped char by char.

Benchmarks (./x bench library/coretests --test-args chars_advance, corpora::ru::LARGE, x86_64):

bench before after
chars_advance_back_by_0001 0.84–0.88 ns 0.45–0.68 ns
chars_advance_back_by_0010 4.7–4.9 ns 3.4–5.1 ns
chars_advance_back_by_1000 980–1062 ns 106–166 ns
(chars_advance_by_1000, ref) 171 ns 165–177 ns

Tests: test_iterator_advance_back (mirror of the forward test) and test_iterator_advance_back_matches_next_back, which compares advance_back_by(n) / nth_back(n) against repeated next_back() for all n (including past the end) on mixed 1–4 byte strings spanning several chunks, at several start offsets.

r? libs


AI disclosure: This change was assisted by GitHub Copilot and Claude. I reviewed and tested it myself.

`Chars` already has a specialized `advance_by` that counts char start
bytes in 32 byte chunks instead of decoding every char. The reverse
direction fell back to the default implementation, so
`chars().nth_back(n)`, `chars().rev().skip(n)` and friends decoded each
char one by one.

Mirror the forward implementation from the back. When the skipped bytes
start in the middle of a char, its leading byte was not counted, so its
continuation bytes are kept in the iterator.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 3, 2026
@rustbot

rustbot commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @Darksonn (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • libs expanded to 12 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey

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

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants