Skip to content

[perf] Add explicit Iterator::count impl for ChunkBy.#158866

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
obi1kenobi:pg/iterator-chunk-by
Jul 10, 2026
Merged

[perf] Add explicit Iterator::count impl for ChunkBy.#158866
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
obi1kenobi:pg/iterator-chunk-by

Conversation

@obi1kenobi

@obi1kenobi obi1kenobi commented Jul 6, 2026

Copy link
Copy Markdown
Member

Prior to this PR, .chunk_by(..).count() constructs were using the default Iterator::count implementation for ChunkBy, which was based on Iterator::next().

We can do noticeably better by writing a dedicated implementation, as measured at this real-world call site in omnibor-rs.

A local benchmark on my macOS laptop shows that using omnibor-rs's public API to invoke ArtifactId::sha256() in a tight loop, with appropriate black-boxing, experiences a 5-7% CPU time win with the dedicated implementation. Of course the benchmark is sensitive to the exact length and contents of the file, hence the 5-7% range. But the 95% confidence interval for the amount of improvement in every benchmark I ran is <1% wide, so I am quite confident this is an improvement.

I also extracted the relevant omnibor-rs use of .chunk_by(..).count() into a microbenchmark, which shows a 2.1-2.4x throughput improvement (-51.5-57.7% CPU time) with the new implementation. Disassembly of the baseline vs new implementation shows that the optimizer was able to vectorize the new implementation, while the baseline was not vectorized.

AI disclosure: The optimization opportunity here was discovered as part of a systematic probe for missed optimizations utilizing both traditional and AI tools. The code here was initially prototyped and vetted by AI tools, followed by additional manual work. I secured approval in advance from the reviewer I pinged. I stand behind the quality of the code I'm submitting, and I vouch it's as good or better compared to if I had written every line by my own hand.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 6, 2026
@obi1kenobi

Copy link
Copy Markdown
Member Author

cc @Kobzol for perf run

@lqd

lqd commented Jul 6, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 6, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 6, 2026
[perf] Add explicit `Iterator::count` impl for `ChunkBy`.
@rust-bors

rust-bors Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 546c6e5 (546c6e543ac3ec8f80e058d5494d0d388854d0d4)
Base parent: 3c00c96 (3c00c96d3af4d5b5e101e56cc161a608b21366ee)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (546c6e5): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.7% [0.1%, 1.7%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -0.7%, secondary -5.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.7% [-0.7%, -0.7%] 1
Improvements ✅
(secondary)
-5.2% [-5.2%, -5.2%] 1
All ❌✅ (primary) -0.7% [-0.7%, -0.7%] 1

Cycles

Results (primary 2.9%, secondary -3.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.4%, -2.6%] 2
All ❌✅ (primary) 2.9% [2.9%, 2.9%] 1

Binary size

Results (secondary 0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 491.018s -> 490.286s (-0.15%)
Artifact size: 388.45 MiB -> 388.95 MiB (0.13%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 7, 2026
@obi1kenobi

Copy link
Copy Markdown
Member Author

It's interesting that we see regressions at all. I expected a ~zero impact finding due to chunk_by not being represented in the benchmark suite. I'll try to triage a bit more, but if someone with more experience cares to look more closely I welcome the help!

@Kobzol

Kobzol commented Jul 7, 2026

Copy link
Copy Markdown
Member

Those two benchmarks are essentially just hello world, it might just be noise, or the stdlib becoming a tiny bit larger to load or something.

A change like this most likely won't be caught by our compile benchmark suite (which doesn't mean it's not worth landing).

@joboet

joboet commented Jul 9, 2026

Copy link
Copy Markdown
Member

This looks fine to me, and probably performs better than the default. r=me if you want to...
r? joboet

@Kobzol

Kobzol commented Jul 9, 2026

Copy link
Copy Markdown
Member

@bors delegate

@rust-bors

rust-bors Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

✌️ @obi1kenobi, you can now approve this pull request!

If @Kobzol told you to "r=me" after making some further change, then please make that change and post @bors r=Kobzol.

View changes since this delegation.

@obi1kenobi
obi1kenobi marked this pull request as ready for review July 10, 2026 06:02
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 10, 2026
@obi1kenobi

obi1kenobi commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

I updated the PR description with the results of a benchmark using a real-world library's public API, plus a microbenchmark extracted from that use site. Both agree there's a material win here, and seeing vectorization start happening here is the cherry on top.

@obi1kenobi

Copy link
Copy Markdown
Member Author

@bors r=joboet

@rust-bors

rust-bors Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 22ab4de has been approved by joboet

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 10, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 10, 2026
Rollup of 7 pull requests

Successful merges:

 - #155429 (Support `u128`/`i128` c-variadic arguments)
 - #158899 (Fix `unaligned_volatile_store` by removing `MemFlags::UNALIGNED`)
 - #158640 (Handle nested inline consts in const argument checks)
 - #158866 ([perf] Add explicit `Iterator::count` impl for `ChunkBy`.)
 - #158912 (Introduce new bootstrap config section for PGO configuration)
 - #159040 (Print step stack trace when bootstrap panics)
 - #159056 (Print a loud error if the configured LLDB cannot be found)
@rust-bors
rust-bors Bot merged commit 5d9e984 into rust-lang:main Jul 10, 2026
14 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 10, 2026
rust-timer added a commit that referenced this pull request Jul 10, 2026
Rollup merge of #158866 - obi1kenobi:pg/iterator-chunk-by, r=joboet

[perf] Add explicit `Iterator::count` impl for `ChunkBy`.

Prior to this PR, `.chunk_by(..).count()` constructs were using the default `Iterator::count` implementation for `ChunkBy`, which was based on `Iterator::next()`.

We can do noticeably better by writing a dedicated implementation, as measured [at this real-world call site in `omnibor-rs`](https://github.com/omnibor/omnibor-rs/blob/116c5c2281219dece9da4eddc7d2e2c47e4c81c4/omnibor/src/gitoid/internal.rs#L186).

A local benchmark on my macOS laptop shows that using `omnibor-rs`'s public API to invoke `ArtifactId::sha256()` in a tight loop, with appropriate black-boxing, experiences a 5-7% CPU time win with the dedicated implementation. Of course the benchmark is sensitive to the exact length and contents of the file, hence the 5-7% range. But the 95% confidence interval for the amount of improvement in every benchmark I ran is <1% wide, so I am quite confident this is an improvement.

I also extracted the relevant `omnibor-rs` use of `.chunk_by(..).count()` into a microbenchmark, which shows a 2.1-2.4x throughput improvement (-51.5-57.7% CPU time) with the new implementation. Disassembly of the baseline vs new implementation shows that the optimizer was able to vectorize the new implementation, while the baseline was not vectorized.

**AI disclosure:** The optimization opportunity here was discovered as part of a systematic probe for missed optimizations utilizing both traditional and AI tools. The code here was initially prototyped and vetted by AI tools, followed by additional manual work. I secured approval in advance from the reviewer I pinged. I stand behind the quality of the code I'm submitting, and I vouch it's as good or better compared to if I had written every line by my own hand.
@joboet

joboet commented Jul 11, 2026

Copy link
Copy Markdown
Member

Hey Predrag 😄! Could you make sure to include the LLM-disclosure in the description of draft PRs as well? I don't think it would have changed anything about my approval of this PR, but it feels a bit nicer not to be surprised by such a note when revisiting things. Thanks, and no worries!

@obi1kenobi
obi1kenobi deleted the pg/iterator-chunk-by branch July 12, 2026 04:29
@obi1kenobi

Copy link
Copy Markdown
Member Author

Apologies for the unexpected surprise! To be honest I didn't expect anyone to review a draft PR with no description 😅

I'll try to indicate going forward whether a PR is in a state I consider reviewable or just for measurement purposes only. As you know for perf work it's often useful to know whether a change is perf-positive at all before polishing it, and as you see my AI note includes a strong endorsement of the code quality which wouldn't necessarily apply on a perf-measurement-only PR. I might have to come up with a different AI note for such situations. I'll try to figure out something!

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants