Skip to content

Cleanup chunk_by count method and add tests#159389

Open
hkBst wants to merge 1 commit into
rust-lang:mainfrom
hkBst:patch-8
Open

Cleanup chunk_by count method and add tests#159389
hkBst wants to merge 1 commit into
rust-lang:mainfrom
hkBst:patch-8

Conversation

@hkBst

@hkBst hkBst commented Jul 16, 2026

Copy link
Copy Markdown
Member

View all comments

Cleanup chunk_by count method (introduced in #158866) to use array_windows for improved readability.

EDIT: also added tests for chunk_by.

@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 Jul 16, 2026
@rustbot

rustbot commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

r? @Darksonn

rustbot has assigned @Darksonn.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 6 candidates

@Darksonn Darksonn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that this function is missing some tests.

View changes since this review

Comment thread library/core/src/slice/iter.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2026
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the patch-8 branch 2 times, most recently from e4cf709 to f5048ba Compare July 17, 2026 08:58
@hkBst hkBst changed the title Refactor count method in slice iterator Cleanup chunk_by count method and add tests Jul 17, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread library/coretests/tests/slice.rs
Comment thread library/coretests/tests/slice.rs Outdated
Comment thread library/coretests/tests/slice.rs Outdated
Comment thread library/coretests/tests/slice.rs Outdated
Comment thread library/core/src/slice/iter.rs
@hkBst

hkBst commented Jul 17, 2026

Copy link
Copy Markdown
Member Author

@rustbot ready

@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 17, 2026
@obi1kenobi

Copy link
Copy Markdown
Member

Would you mind just verifying that the new implementation is still able to vectorize the count?

There was some sensitivity to that in the initial code. Unfortunately the existing perf suite doesn't measure this call site. My original PR used the omnibor crate to measure and hopefully you'd be able to do the same.

@Darksonn

Copy link
Copy Markdown
Member

Good point, @obi1kenobi

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2026
@hkBst

hkBst commented Jul 25, 2026

Copy link
Copy Markdown
Member Author

All comments addressed, except I'm not able to test vectorization at this time. @obi1kenobi perhaps you would be willing to test that?

@rustbot ready

@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 25, 2026
@obi1kenobi

Copy link
Copy Markdown
Member

On my current x86-64 machine this is a perf regression predominantly for short inputs, both with and without vectorization.

Slice length Native AVX2 slowdown Generic x86-64 slowdown
2 16.4% 17.0%
8 9.3% 11.4%
16 14.9% 8.7%
64 3.6% 3.5%
8 KiB ~0–0.6% ~0.5%

Looking at the assembly, the proposed new code has extra branches and conditional-moves, plus a few extra arithmetic instructions. Here's a flavor of the extra instructions:

+	subq	$2, %rsi
+	cmovbq	%rdi, %rsi       # saturating array_windows length
...
+	incq	%rax             # tail bookkeeping
...
+	incq	%rdi             # add the initial chunk after reduction
+	movq	%rdi, %rax

I can include a full diff of the assembly if you'd like, but it's a bit noisy because registers got renamed.

I don't have my macOS machine handy at the moment (where I originally benchmarked this) but given the generated assembly is more complex, I expect directionally similar results. (For reference, #158866 was a ~2x win on macOS but a 4-5.8x win on this x86-64 machine, largely attributable to the macOS arm cores having more effective ILP in scalar mode than my x86-64 CPU).

I'm not a reviewer or decision-maker on this PR. But personally, I don't really find the new code any easier to read than the prior code. I think the extra tests are great though!

@Darksonn

Copy link
Copy Markdown
Member

Given @obi1kenobi's analysis, I think we should keep the existing code. But I'd be happy to approve adding the tests.

@Darksonn

Copy link
Copy Markdown
Member

Also, did you even change the code since this comment? #159389 (comment)

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.

5 participants