Skip to content

Change TaskDeps to start preallocated with 128 capacity #137563

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
Mar 9, 2025

Conversation

FractalFir
Copy link
Contributor

@FractalFir FractalFir commented Feb 24, 2025

This is a tiny change that makes TaskDeps::read_set start preallocated with capacity for 128 elements.

From local profiling, it looks like TaskDeps::read_set is one of the most-often resized hash-sets in rustc.

@rustbot
Copy link
Collaborator

rustbot commented Feb 24, 2025

r? @Noratrieb

rustbot has assigned @Noratrieb.
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 A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2025
@FractalFir
Copy link
Contributor 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 Feb 24, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 24, 2025
[Perf experiment] Changed TaskDeps to start with preallocated with 1024 capacity

This is a tiny change that makes `TaskDeps::read_set` start preallocated with capacity for 1024 elements(4096 bytes).

Somewhat annoyingly, `HashSet::with_capacity` requires the hasher to implement `RandomState`. Since `FxHash` does not implement `RandomState`, I had to use `HashSet::with_capacity_and_hasher`, which required re-exporting `FxBuildHasher` in `rustc_data_structures`.

From local profiling, it looks like `TaskDeps::read_set`  is one of the most-often resized hash-sets in `rustc`.

Local perf runs indicate this is a small perf improvement(without any regressions). There is also no significant RSS increase(the RSS changes are noisy and cancel themselves out almost entirely).

Still, since the local and CI results can differ, I'll do a CI perf run before this PR can be reviewed.

`@bors` try `@rust-timer` queue
@bors
Copy link
Collaborator

bors commented Feb 24, 2025

⌛ Trying commit 02aa397 with merge 0fd98fe...

@bors
Copy link
Collaborator

bors commented Feb 24, 2025

☀️ Try build successful - checks-actions
Build commit: 0fd98fe (0fd98fe325e1591d05ebd43e902c9273501ab6d3)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0fd98fe): 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)
0.4% [0.2%, 0.6%] 11
Regressions ❌
(secondary)
0.4% [0.3%, 0.4%] 6
Improvements ✅
(primary)
-3.8% [-5.5%, -0.4%] 8
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.5%] 1
All ❌✅ (primary) -1.4% [-5.5%, 0.6%] 19

Max RSS (memory usage)

Results (primary 0.0%, 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)
1.0% [0.4%, 2.8%] 25
Regressions ❌
(secondary)
1.0% [0.4%, 1.9%] 15
Improvements ✅
(primary)
-0.9% [-3.4%, -0.4%] 28
Improvements ✅
(secondary)
-1.0% [-2.5%, -0.4%] 19
All ❌✅ (primary) 0.0% [-3.4%, 2.8%] 53

Cycles

Results (primary -0.3%, secondary 0.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.9% [0.4%, 3.3%] 46
Regressions ❌
(secondary)
1.3% [0.4%, 6.3%] 69
Improvements ✅
(primary)
-1.5% [-4.9%, -0.4%] 45
Improvements ✅
(secondary)
-0.9% [-3.6%, -0.4%] 59
All ❌✅ (primary) -0.3% [-4.9%, 3.3%] 91

Binary size

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

Bootstrap: 770.447s -> 768.842s (-0.21%)
Artifact size: 359.63 MiB -> 359.69 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 24, 2025
@FractalFir
Copy link
Contributor Author

This seems like a perf improvement to me - since the average is down, and the regressions are much smaller than the improvements.

I'll have to look a bit more into the regressions, since, at least at the first glance, some of them seem a bit odd:

Eg. unicode-normalization-0.1.19 opt incr-patched: println sees a regression in the backend, which I don't think this change touches.

@FractalFir
Copy link
Contributor Author

r? @nnethercote Since this is a micro-optimization, I think you are the right person to review this.

What is the general policy for PRs that have both regressions and improvements? Does each regression need to be separately justifed, or do they just need to be outweight by the perf gains?

@rustbot rustbot assigned nnethercote and unassigned Noratrieb Feb 25, 2025
@Mark-Simulacrum
Copy link
Member

What is the distribution of sizes of the set? You can probably add a Drop impl and just eprintln! the len() of the set and then pass that through counts or similar to get that.

IOW, is 1024 the right point? Or perhaps 2048, or some other point on the curve -- I could also imagine wanting to jump up (e.g., most sets are 10 elements but then immediately grow to 2048 elements).

@nnethercote
Copy link
Contributor

I agree with @Mark-Simulacrum, some counts results would be useful. I don't have any sense how many TaskDeps values are created and how big they get.

@@ -1299,7 +1299,7 @@ impl Default for TaskDeps {
#[cfg(debug_assertions)]
node: None,
reads: EdgesVec::new(),
read_set: FxHashSet::default(),
read_set: FxHashSet::with_capacity_and_hasher(1024, FxBuildHasher::default()),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
read_set: FxHashSet::with_capacity_and_hasher(1024, FxBuildHasher::default()),
read_set: FxHashSet::with_capacity_and_hasher(1024, Default::default()),

would work too I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works - thanks for the suggestion.

@FractalFir
Copy link
Contributor Author

I did not know about counts - it will probably help a lot with further profiling!

This is the result of counts:

(  1)   577194 (82.8%, 82.8%): read_set.len():0
(  2)    11768 ( 1.7%, 84.5%): read_set.len():9
(  3)    10303 ( 1.5%, 86.0%): read_set.len():8
(  4)     8670 ( 1.2%, 87.2%): read_set.len():11
(  5)     8655 ( 1.2%, 88.5%): read_set.len():10
(  6)     7165 ( 1.0%, 89.5%): read_set.len():12
(  7)     5010 ( 0.7%, 90.2%): read_set.len():16
(  8)     4740 ( 0.7%, 90.9%): read_set.len():14
(  9)     4511 ( 0.6%, 91.5%): read_set.len():13
( 10)     3949 ( 0.6%, 92.1%): read_set.len():17
( 11)     3493 ( 0.5%, 92.6%): read_set.len():18
( 12)     3347 ( 0.5%, 93.1%): read_set.len():15
( 13)     3281 ( 0.5%, 93.6%): read_set.len():19
( 14)     2563 ( 0.4%, 93.9%): read_set.len():20
( 15)     2138 ( 0.3%, 94.2%): read_set.len():23
( 16)     1932 ( 0.3%, 94.5%): read_set.len():21
( 17)     1767 ( 0.3%, 94.8%): read_set.len():22
( 18)     1736 ( 0.2%, 95.0%): read_set.len():24
( 19)     1480 ( 0.2%, 95.2%): read_set.len():27
( 20)     1445 ( 0.2%, 95.4%): read_set.len():25
( 21)     1412 ( 0.2%, 95.6%): read_set.len():32
( 22)     1272 ( 0.2%, 95.8%): read_set.len():26
( 23)     1233 ( 0.2%, 96.0%): read_set.len():33
( 24)     1198 ( 0.2%, 96.2%): read_set.len():29
( 25)     1121 ( 0.2%, 96.3%): read_set.len():28
( 26)     1088 ( 0.2%, 96.5%): read_set.len():39
( 27)      936 ( 0.1%, 96.6%): read_set.len():31
( 28)      823 ( 0.1%, 96.7%): read_set.len():30
( 29)      814 ( 0.1%, 96.8%): read_set.len():34
( 30)      772 ( 0.1%, 97.0%): read_set.len():35
( 31)      737 ( 0.1%, 97.1%): read_set.len():42
( 32)      680 ( 0.1%, 97.2%): read_set.len():45
( 33)      662 ( 0.1%, 97.3%): read_set.len():37
( 34)      630 ( 0.1%, 97.3%): read_set.len():40
( 35)      606 ( 0.1%, 97.4%): read_set.len():38
( 36)      568 ( 0.1%, 97.5%): read_set.len():47
( 37)      565 ( 0.1%, 97.6%): read_set.len():48
( 38)      560 ( 0.1%, 97.7%): read_set.len():41
( 39)      551 ( 0.1%, 97.8%): read_set.len():44
( 40)      545 ( 0.1%, 97.8%): read_set.len():36
( 41)      472 ( 0.1%, 97.9%): read_set.len():49
( 42)      430 ( 0.1%, 98.0%): read_set.len():46
( 43)      423 ( 0.1%, 98.0%): read_set.len():43
( 44)      421 ( 0.1%, 98.1%): read_set.len():50
( 45)      417 ( 0.1%, 98.1%): read_set.len():51
( 46)      390 ( 0.1%, 98.2%): read_set.len():60
( 47)      325 ( 0.0%, 98.2%): read_set.len():52
( 48)      308 ( 0.0%, 98.3%): read_set.len():56
( 49)      286 ( 0.0%, 98.3%): read_set.len():62
( 50)      278 ( 0.0%, 98.4%): read_set.len():55
( 51)      274 ( 0.0%, 98.4%): read_set.len():59
( 52)      268 ( 0.0%, 98.4%): read_set.len():53
( 53)      268 ( 0.0%, 98.5%): read_set.len():54
( 54)      248 ( 0.0%, 98.5%): read_set.len():57
( 55)      231 ( 0.0%, 98.6%): read_set.len():61
( 56)      220 ( 0.0%, 98.6%): read_set.len():78
( 57)      219 ( 0.0%, 98.6%): read_set.len():64
( 58)      208 ( 0.0%, 98.6%): read_set.len():68
( 59)      191 ( 0.0%, 98.7%): read_set.len():58
( 60)      176 ( 0.0%, 98.7%): read_set.len():65
( 61)      173 ( 0.0%, 98.7%): read_set.len():69
( 62)      168 ( 0.0%, 98.7%): read_set.len():116
( 63)      168 ( 0.0%, 98.8%): read_set.len():74
( 64)      162 ( 0.0%, 98.8%): read_set.len():80
( 65)      159 ( 0.0%, 98.8%): read_set.len():67
( 66)      142 ( 0.0%, 98.8%): read_set.len():63
( 67)      141 ( 0.0%, 98.9%): read_set.len():66
( 68)      141 ( 0.0%, 98.9%): read_set.len():70
( 69)      141 ( 0.0%, 98.9%): read_set.len():72
( 70)      120 ( 0.0%, 98.9%): read_set.len():71
( 71)      117 ( 0.0%, 98.9%): read_set.len():76
( 72)      116 ( 0.0%, 99.0%): read_set.len():75
( 73)      111 ( 0.0%, 99.0%): read_set.len():77
( 74)      107 ( 0.0%, 99.0%): read_set.len():79
( 75)      107 ( 0.0%, 99.0%): read_set.len():81
( 76)      105 ( 0.0%, 99.0%): read_set.len():86
( 77)       99 ( 0.0%, 99.0%): read_set.len():85
( 78)       93 ( 0.0%, 99.0%): read_set.len():112
( 79)       92 ( 0.0%, 99.1%): read_set.len():91

So, it seems like this HashSet is usually either empty, or it has < 128 elements in it. I'll see if 128 elements is better locally.

@FractalFir
Copy link
Contributor Author

FractalFir commented Feb 26, 2025

I have done some more testing, and 128 seems to be a better cap for performance.

Additonally, I have discovered(by printing the caller on each resize) that ~30% of all resizes of HashSets occur within this file.

(  1)   118543 (28.3%, 28.3%): Resizing insert from Location { file: "/home/michal/rust/compiler/rustc_query_system/src/dep_graph/graph.rs", line: 511, col: 40 }
(  2)    84689 (20.2%, 48.6%): Resizing insert from Location { file: "/home/michal/rust/compiler/rustc_data_structures/src/obligation_forest/mod.rs", line: 678, col: 37 }
(  3)    32348 ( 7.7%, 56.3%): Resizing insert from Location { file: "/home/michal/rust/compiler/rustc_data_structures/src/transitive_relation.rs", line: 104, col: 20 }
(  4)    28910 ( 6.9%, 63.2%): Resizing insert from Location { file: "/home/michal/rust/compiler/rustc_type_ir/src/elaborate.rs", line: 97, col: 30 }
(  5)    26681 ( 6.4%, 69.6%): Resizing insert from Location { file: "compiler/rustc_hir_typeck/src/method/probe.rs", line: 852, col: 28 }
(  6)    18466 ( 4.4%, 74.0%): Resizing insert from Location { file: "/home/michal/rust/compiler/rustc_data_structures/src/graph/scc/mod.rs", line: 689, col: 57 }
(  7)    16360 ( 3.9%, 77.9%): Resizing insert from Location { file: "compiler/rustc_resolve/src/late.rs", line: 3918, col: 44 }

@nnethercote
Copy link
Contributor

nnethercote commented Feb 26, 2025

counts is invaluable for this kind of profiling, I use it all the time.

Do you want to do another perf run with the limit set to 128 or even 64?

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 27, 2025
@rustbot

This comment has been minimized.

@FractalFir
Copy link
Contributor 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 Feb 27, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 27, 2025
[Perf experiment] Changed TaskDeps to start with preallocated with 1024 capacity

This is a tiny change that makes `TaskDeps::read_set` start preallocated with capacity for 1024 elements(4096 bytes).

Somewhat annoyingly, `HashSet::with_capacity` requires the hasher to implement `RandomState`. Since `FxHash` does not implement `RandomState`, I had to use `HashSet::with_capacity_and_hasher`, which required re-exporting `FxBuildHasher` in `rustc_data_structures`.

From local profiling, it looks like `TaskDeps::read_set`  is one of the most-often resized hash-sets in `rustc`.

Local perf runs indicate this is a small perf improvement(without any regressions). There is also no significant RSS increase(the RSS changes are noisy and cancel themselves out almost entirely).

Still, since the local and CI results can differ, I'll do a CI perf run before this PR can be reviewed.

`@bors` try `@rust-timer` queue
@bors
Copy link
Collaborator

bors commented Feb 27, 2025

⌛ Trying commit e100fd4 with merge a21e95e...

@bors
Copy link
Collaborator

bors commented Feb 27, 2025

☀️ Try build successful - checks-actions
Build commit: a21e95e (a21e95e41580e2394a6be7b196d7fff5cd833ee1)

@Kobzol
Copy link
Contributor

Kobzol commented Feb 28, 2025

@rust-timer build a21e95e

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a21e95e): 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
Improvements ✅
(primary)
-0.6% [-0.8%, -0.5%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-0.8%, -0.5%] 6

Max RSS (memory usage)

Results (secondary 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)
- - 0
Regressions ❌
(secondary)
2.9% [2.8%, 3.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary -2.1%, secondary 7.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)
- - 0
Regressions ❌
(secondary)
7.7% [7.6%, 7.8%] 2
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.1% [-2.1%, -2.1%] 1

Binary size

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

Bootstrap: 770.531s -> 770.268s (-0.03%)
Artifact size: 361.97 MiB -> 361.95 MiB (-0.01%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Feb 28, 2025
@FractalFir FractalFir changed the title [Perf experiment] Changed TaskDeps to start with preallocated with 1024 capacity [Perf experiment] Changed TaskDeps to start with preallocated with 128 capacity Feb 28, 2025
@FractalFir
Copy link
Contributor Author

I think this is ready to merge - since it is a perf imporvement, and a 1-line change.

@nnethercote
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Feb 28, 2025

📌 Commit e100fd4 has been approved by nnethercote

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Feb 28, 2025

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@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 Feb 28, 2025
@nnethercote nnethercote changed the title [Perf experiment] Changed TaskDeps to start with preallocated with 128 capacity Changed TaskDeps to start with preallocated with 128 capacity Feb 28, 2025
@nnethercote nnethercote changed the title Changed TaskDeps to start with preallocated with 128 capacity Change TaskDeps to start with preallocated with 128 capacity Feb 28, 2025
@nnethercote nnethercote changed the title Change TaskDeps to start with preallocated with 128 capacity Change TaskDeps to start preallocated with 128 capacity Feb 28, 2025
Noratrieb added a commit to Noratrieb/rust that referenced this pull request Mar 6, 2025
…rcote

Change TaskDeps to start preallocated with 128 capacity

This is a tiny change that makes `TaskDeps::read_set` start preallocated with capacity for 128 elements.

From local profiling, it looks like `TaskDeps::read_set`  is one of the most-often resized hash-sets in `rustc`.
@bors
Copy link
Collaborator

bors commented Mar 9, 2025

⌛ Testing commit e100fd4 with merge 4f52199...

@bors
Copy link
Collaborator

bors commented Mar 9, 2025

☀️ Test successful - checks-actions
Approved by: nnethercote
Pushing 4f52199 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 9, 2025
@bors bors merged commit 4f52199 into rust-lang:master Mar 9, 2025
7 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 9, 2025
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

gh pr comment ${HEAD_PR} -F output.log
shell: /usr/bin/bash -e {0}
##[endgroup]
fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
##[error]Process completed with exit code 128.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4f52199): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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
Improvements ✅
(primary)
-0.6% [-0.8%, -0.5%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-0.8%, -0.5%] 6

Max RSS (memory usage)

Results (primary 2.7%, secondary -1.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)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-3.9%, -0.9%] 4
All ❌✅ (primary) 2.7% [2.7%, 2.7%] 1

Cycles

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

Binary size

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

Bootstrap: 765.189s -> 765.417s (0.03%)
Artifact size: 362.04 MiB -> 362.05 MiB (0.00%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) merged-by-bors This PR was explicitly merged by bors. 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.

10 participants