Skip to content

simplify slice::Iter[Mut]::next_chunk implementation - #158879

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
WaffleLapkin:slice_iter_next_chunk_simp
Jul 29, 2026
Merged

simplify slice::Iter[Mut]::next_chunk implementation#158879
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
WaffleLapkin:slice_iter_next_chunk_simp

Conversation

@WaffleLapkin

@WaffleLapkin WaffleLapkin commented Jul 6, 2026

Copy link
Copy Markdown
Member

I verified that this doesn't pessimize codegen using the example from the PR that inroduced the optimization of next_chunk (#149131):

#![feature(iter_next_chunk)]

#[no_mangle]
pub fn simd_sum_slow(arr: &[u32]) -> u32 {
    const STEP_SIZE: usize = 16;

    let mut result = [0; STEP_SIZE];

    let mut iter = arr.iter();

    while let Ok(c) = iter.next_chunk::<STEP_SIZE>() {
        for (&n, r) in c.iter().zip(result.iter_mut()) {
            *r += n;
        }
    }

    result.iter().sum()
}

I compiled this example with

./build/host/stage1/bin/rustc t.rs --emit=asm -O --crate-type=lib

Before and after this change; the only difference is the choice of the jump instruction, which I think shouldn't make any difference:

28,29c28,29
< 	cmpq	$64, %rsi
< 	jae	.LBB0_2
---
> 	cmpq	$60, %rsi
> 	ja	.LBB0_2

r? libs
cc @bend-n

@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 6, 2026
@rust-log-analyzer

This comment has been minimized.

@bend-n

bend-n commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

i dont think theres any optimization problem here in anything but compile time, but i'm not sure as to the purpose of the pr?

@WaffleLapkin

Copy link
Copy Markdown
Member Author

The purpose of the PR is to make the code simpler and easier to read/maintain.

@bend-n

bend-n commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

i feel as though its not exactly simpler, merely smaller.

I verified that this doesn't pessimize codegen using the example from the
PR that inroduced the optimization of `next_chunk` (# 149131):

```rust
#![feature(iter_next_chunk)]

#[no_mangle]
pub fn simd_sum_slow(arr: &[u32]) -> u32 {
    const STEP_SIZE: usize = 16;

    let mut result = [0; STEP_SIZE];

    let mut iter = arr.iter();

    while let Ok(c) = iter.next_chunk::<STEP_SIZE>() {
        for (&n, r) in c.iter().zip(result.iter_mut()) {
            *r += n;
        }
    }

    result.iter().sum()
}
```

I compiled this example with
```shell
./build/host/stage1/bin/rustc t.rs --emit=asm -O --crate-type=lib
```

Before and after this change; the only difference is the choice of the
jump instruction, which I think shouldn't make any difference:

```diff
28,29c28,29
< 	cmpq	$64, %rsi
< 	jae	.LBB0_2
---
> 	cmpq	$60, %rsi
> 	ja	.LBB0_2
```
@WaffleLapkin

Copy link
Copy Markdown
Member Author

I feel like it is simpler? It avoids the whole thing with copying things into a MaybeUninit array and creating array::IntoIter, etc...

@WaffleLapkin
WaffleLapkin force-pushed the slice_iter_next_chunk_simp branch from ac14155 to aec1f2a Compare July 7, 2026 16:35
@JohnTitor

Copy link
Copy Markdown
Member

Let's perf-run just in case:
@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 27, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
simplify `slice::Iter[Mut]::next_chunk` implementation

@JohnTitor JohnTitor 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.

r=me if the result is fine

View changes since this review

@rust-bors

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2277e36 (2277e3696d57fbbfdcbb2f2b84752dcafdd5afb3)
Base parent: 09ee43b (09ee43b2d6055539771bee8ac30a6e56eb4db773)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2277e36): comparison URL.

Overall result: no relevant changes - 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

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary -2.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)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.5% [-6.5%, -6.5%] 1
All ❌✅ (primary) - - 0

Cycles

Results (secondary -2.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
Improvements ✅
(secondary)
-2.2% [-2.4%, -2.1%] 2
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 487.408s -> 488.179s (0.16%)
Artifact size: 387.99 MiB -> 387.99 MiB (0.00%)

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

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit aec1f2a has been approved by JohnTitor

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 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 11 pull requests

Successful merges:

 - #158168 (Added implementation on `set_permissions_nofollow` for all primary platforms)
 - #160055 (Simplify `MaybeRequiresStorage`)
 - #157226 (Partially stabilize `box_vec_non_null`)
 - #158879 (simplify `slice::Iter[Mut]::next_chunk` implementation)
 - #159413 (Enable `#[diagnostic::on_unknown]` during late res)
 - #160091 (Fix rustdoc toolbar height when title is taller than one line)
 - #158615 (fix: don't fire `explicit_outlives_requirements` on `?Sized` type params)
 - #159666 (fix(ld64.lld): route version mismatch warnings to linker_info on macOS)
 - #160032 (rustdoc-json: Make `Stability` compatible with non-self-describing serde formats)
 - #160039 (Add regression test for enum unconstrained parameter )
 - #160049 (Use assert_eq! in splat codegen tests)
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 11 pull requests

Successful merges:

 - #158168 (Added implementation on `set_permissions_nofollow` for all primary platforms)
 - #160055 (Simplify `MaybeRequiresStorage`)
 - #157226 (Partially stabilize `box_vec_non_null`)
 - #158879 (simplify `slice::Iter[Mut]::next_chunk` implementation)
 - #159413 (Enable `#[diagnostic::on_unknown]` during late res)
 - #160091 (Fix rustdoc toolbar height when title is taller than one line)
 - #158615 (fix: don't fire `explicit_outlives_requirements` on `?Sized` type params)
 - #159666 (fix(ld64.lld): route version mismatch warnings to linker_info on macOS)
 - #160032 (rustdoc-json: Make `Stability` compatible with non-self-describing serde formats)
 - #160039 (Add regression test for enum unconstrained parameter )
 - #160049 (Use assert_eq! in splat codegen tests)
@rust-bors
rust-bors Bot merged commit 8c6ef9f into rust-lang:main Jul 29, 2026
14 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 29, 2026
rust-timer added a commit that referenced this pull request Jul 29, 2026
Rollup merge of #158879 - WaffleLapkin:slice_iter_next_chunk_simp, r=JohnTitor

simplify `slice::Iter[Mut]::next_chunk` implementation

I verified that this doesn't pessimize codegen using the example from the PR that inroduced the optimization of `next_chunk` (#149131):

```rust
#![feature(iter_next_chunk)]

#[no_mangle]
pub fn simd_sum_slow(arr: &[u32]) -> u32 {
    const STEP_SIZE: usize = 16;

    let mut result = [0; STEP_SIZE];

    let mut iter = arr.iter();

    while let Ok(c) = iter.next_chunk::<STEP_SIZE>() {
        for (&n, r) in c.iter().zip(result.iter_mut()) {
            *r += n;
        }
    }

    result.iter().sum()
}
```

I compiled this example with
```shell
./build/host/stage1/bin/rustc t.rs --emit=asm -O --crate-type=lib
```

Before and after this change; the only difference is the choice of the jump instruction, which I think shouldn't make any difference:

```diff
28,29c28,29
< 	cmpq	$64, %rsi
< 	jae	.LBB0_2
---
> 	cmpq	$60, %rsi
> 	ja	.LBB0_2
```

r? libs
cc @bend-n
@WaffleLapkin
WaffleLapkin deleted the slice_iter_next_chunk_simp branch July 29, 2026 18:24
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