Skip to content

Rework collect_and_apply to not rely on size hint for optimization #141652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025

Conversation

compiler-errors
Copy link
Member

I saw that we have quite a few collect_and_apply calls for N=3-7 (N=7 corresponding to cumulative 99% of nalgebra's calls). Didn't perf locally, but also this is super low-pri, so let's see what rust-timer says.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 27, 2025
@compiler-errors
Copy link
Member Author

@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 May 27, 2025
@@ -402,6 +402,51 @@ impl<T, R> CollectAndApply<T, R> for T {
assert!(iter.next().is_none());
f(&[t0, t1])
}
(3, Some(3)) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

this could be a macro

bors added a commit that referenced this pull request May 27, 2025
[PERF] More collect and apply

I saw that we have quite a few `collect_and_apply` calls for N=3-7 (N=7 corresponding to cumulative 99% of nalgebra's calls). Didn't perf locally, but also this is super low-pri, so let's see what rust-timer says.
@bors
Copy link
Collaborator

bors commented May 27, 2025

⌛ Trying commit 618a3b0 with merge 23b0844...

@bors
Copy link
Collaborator

bors commented May 27, 2025

☀️ Try build successful - checks-actions
Build commit: 23b0844 (23b0844997f3003800cd6bf24365e11d353a5709)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (23b0844): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
3.0% [3.0%, 3.0%] 1
Regressions ❌
(secondary)
0.6% [0.1%, 1.3%] 27
Improvements ✅
(primary)
-0.2% [-0.2%, -0.1%] 6
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.1%] 19
All ❌✅ (primary) 0.3% [-0.2%, 3.0%] 7

Max RSS (memory usage)

Results (primary 1.7%, secondary 4.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
6.1% [1.2%, 9.8%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-2.5%, -1.2%] 3
All ❌✅ (primary) 1.7% [1.7%, 1.7%] 1

Cycles

Results (primary 2.7%, secondary 2.7%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
2.7% [1.3%, 3.1%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [2.7%, 2.7%] 1

Binary size

Results (primary 1.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

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

Bootstrap: 780.158s -> 784.918s (0.61%)
Artifact size: 366.33 MiB -> 368.72 MiB (0.65%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels May 28, 2025
@compiler-errors compiler-errors force-pushed the more-collect-and-apply branch from 618a3b0 to 8180ed0 Compare May 31, 2025 10:08
@compiler-errors
Copy link
Member Author

@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 May 31, 2025
@bors
Copy link
Collaborator

bors commented May 31, 2025

⌛ Trying commit 8180ed0 with merge 4997545...

bors added a commit that referenced this pull request May 31, 2025
[PERF] More collect and apply

I saw that we have quite a few `collect_and_apply` calls for N=3-7 (N=7 corresponding to cumulative 99% of nalgebra's calls). Didn't perf locally, but also this is super low-pri, so let's see what rust-timer says.
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

};

let Some(t2) = iter.next() else {
return f(&[t1]);
Copy link
Member Author

Choose a reason for hiding this comment

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

lol

@bors
Copy link
Collaborator

bors commented May 31, 2025

💔 Test failed - checks-actions

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 31, 2025
@compiler-errors compiler-errors force-pushed the more-collect-and-apply branch from 8180ed0 to e311c75 Compare May 31, 2025 10:34
@compiler-errors
Copy link
Member Author

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 1, 2025

⌛ Trying commit e311c75 with merge e582a3d

rust-bors bot added a commit that referenced this pull request Jun 1, 2025
[PERF] More collect and apply

I saw that we have quite a few `collect_and_apply` calls for N=3-7 (N=7 corresponding to cumulative 99% of nalgebra's calls). Didn't perf locally, but also this is super low-pri, so let's see what rust-timer says.
@rust-bors
Copy link

rust-bors bot commented Jun 1, 2025

☀️ Try build successful (CI)
Build commit: e582a3d (e582a3daad87838574e3805e1708c269b5684437)

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 2, 2025

☀️ Try build successful (CI)
Build commit: c80db40 (c80db4002515430c35018ea3d5c3b3b782f54dd8)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c80db40): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 6
Improvements ✅
(primary)
-0.3% [-0.4%, -0.2%] 5
Improvements ✅
(secondary)
-0.6% [-1.3%, -0.4%] 48
All ❌✅ (primary) -0.3% [-0.4%, -0.2%] 5

Max RSS (memory usage)

Results (primary -1.6%, secondary 0.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.9% [0.6%, 10.0%] 12
Improvements ✅
(primary)
-1.6% [-1.6%, -1.6%] 1
Improvements ✅
(secondary)
-2.5% [-5.3%, -0.4%] 5
All ❌✅ (primary) -1.6% [-1.6%, -1.6%] 1

Cycles

Results (secondary 0.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.0% [0.4%, 2.1%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-5.0%, -0.8%] 4
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 743.462s -> 749.816s (0.85%)
Artifact size: 372.27 MiB -> 371.77 MiB (-0.13%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Jun 2, 2025
@compiler-errors compiler-errors marked this pull request as ready for review June 3, 2025 14:31
@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2025

r? @jieyouxu

rustbot has assigned @jieyouxu.
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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 3, 2025
@compiler-errors
Copy link
Member Author

r? lqd (or other perf ppl)

Seems like a slight improvement, I assume this helps for iterators like .filter() and .flat_map() where they end up knocking out the size hint.

Maybe there's more work to be done here? idk.

@rustbot rustbot assigned lqd and unassigned jieyouxu Jun 3, 2025
@compiler-errors compiler-errors changed the title [PERF] More collect and apply Rework collect_and_apply to not rely on size hint for optimization Jun 3, 2025
@jieyouxu jieyouxu removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 3, 2025
@lqd
Copy link
Member

lqd commented Jun 3, 2025

Yeah, LGTM: a slight improvement, not too complicated, and the gains outweigh the tiny regressions on the secondary stress test. The code may look strange to people stumbling upon it (but it's not unexpected ofc) so I'd like to retain some of the prior comment that we're micro-optimizing for the most common lengths in super hot code.

r=me with that comment (and the commit message fixed :3)

@compiler-errors compiler-errors force-pushed the more-collect-and-apply branch from 42aacf4 to 0479e74 Compare June 3, 2025 17:29
@compiler-errors
Copy link
Member Author

@bors r=lqd

@bors
Copy link
Collaborator

bors commented Jun 3, 2025

📌 Commit 0479e74 has been approved by lqd

It is now in the queue for this repository.

@bors bors 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 Jun 3, 2025
@bors
Copy link
Collaborator

bors commented Jun 4, 2025

⌛ Testing commit 0479e74 with merge d9a7393...

@bors
Copy link
Collaborator

bors commented Jun 4, 2025

☀️ Test successful - checks-actions
Approved by: lqd
Pushing d9a7393 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 4, 2025
@bors bors merged commit d9a7393 into rust-lang:master Jun 4, 2025
11 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 4, 2025
Copy link
Contributor

github-actions bot commented Jun 4, 2025

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 792fc2b (parent) -> d9a7393 (this PR)

Test differences

No test diffs found

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard d9a739363878a06633eb912e44f02a3c5d6ba08c --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-apple-1: 6807.6s -> 8404.7s (23.5%)
  2. dist-x86_64-apple: 9323.0s -> 7523.3s (-19.3%)
  3. aarch64-apple: 4704.8s -> 5402.0s (14.8%)
  4. dist-aarch64-apple: 6738.7s -> 6165.7s (-8.5%)
  5. mingw-check-2: 2019.3s -> 1863.3s (-7.7%)
  6. dist-armhf-linux: 4613.8s -> 4970.0s (7.7%)
  7. dist-apple-various: 6012.1s -> 6434.9s (7.0%)
  8. x86_64-gnu-llvm-19-3: 6398.1s -> 6815.8s (6.5%)
  9. mingw-check-1: 1899.7s -> 1779.8s (-6.3%)
  10. x86_64-gnu-llvm-19-2: 5520.3s -> 5817.3s (5.4%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d9a7393): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.4%] 6
Improvements ✅
(primary)
-0.2% [-0.4%, -0.1%] 9
Improvements ✅
(secondary)
-0.5% [-1.0%, -0.2%] 49
All ❌✅ (primary) -0.2% [-0.4%, -0.1%] 9

Max RSS (memory usage)

Results (secondary 3.3%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.6% [4.9%, 6.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.2% [-1.2%, -1.2%] 1
All ❌✅ (primary) - - 0

Cycles

Results (secondary 1.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

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

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 745.447s -> 748.459s (0.40%)
Artifact size: 372.35 MiB -> 371.85 MiB (-0.13%)

@rustbot rustbot added the perf-regression Performance regression. label Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants