Skip to content
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

Use UnordMap and UnordSet for id collections (DefIdMap, LocalDefIdMap, etc) #106977

Merged
merged 4 commits into from
Jan 21, 2023

Conversation

michaelwoerister
Copy link
Member

@michaelwoerister michaelwoerister commented Jan 17, 2023

This PR changes the rustc_data_structures::define_id_collections! macro to use UnordMap and UnordSet instead of FxHashMap and FxHashSet. This should account for a large portion of hash-maps being used in places where they can cause trouble.

The changes required are moderate but non-zero:

  • In some places the collections are extracted into sorted vecs.
  • There are a few instances where for-loops have been changed to extends.

Let's see what the performance impact is. With a bit more refactoring, we might be able to get rid of some of the additional sorting -- but the change set is already big enough. Unless there's a performance impact, I'd like to do further changes in subsequent PRs.

Performance does not seem to be negatively affected (perf-run here).

Part of MCP 533.

r? @ghost

@rustbot rustbot added 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 Jan 17, 2023
@michaelwoerister
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 Jan 17, 2023
@bors
Copy link
Contributor

bors commented Jan 17, 2023

⌛ Trying commit 5185c3f658c566a8a55ccb27b335169cdedc7ad9 with merge de6228c8f8104f4bed1e7fb6b1874f3531c6aa31...

@bors
Copy link
Contributor

bors commented Jan 17, 2023

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

1 similar comment
@bors
Copy link
Contributor

bors commented Jan 17, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (de6228c8f8104f4bed1e7fb6b1874f3531c6aa31): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.2%, 1.5%] 4
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-0.4% [-1.2%, -0.2%] 16
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Max RSS (memory usage)

Results

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
Improvements ✅
(secondary)
-4.4% [-5.6%, -3.1%] 5
All ❌✅ (primary) - - 0

Cycles

Results

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
Improvements ✅
(secondary)
-2.4% [-2.9%, -2.0%] 5
All ❌✅ (primary) - - 0

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 17, 2023
@michaelwoerister
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 Jan 18, 2023
@bors
Copy link
Contributor

bors commented Jan 18, 2023

⌛ Trying commit 285e2c1be2918f18fcec3386f91ab8cebe3d19dc with merge 587656503f974defd16cf67c0f3894dcc9d7b565...

@michaelwoerister michaelwoerister removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 18, 2023
@bors
Copy link
Contributor

bors commented Jan 18, 2023

☀️ Try build successful - checks-actions
Build commit: 587656503f974defd16cf67c0f3894dcc9d7b565 (587656503f974defd16cf67c0f3894dcc9d7b565)

@michaelwoerister
Copy link
Member Author

@rust-timer build 587656503f974defd16cf67c0f3894dcc9d7b565

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (587656503f974defd16cf67c0f3894dcc9d7b565): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.1%, 0.1%] 1
Regressions ❌
(secondary)
1.7% [1.7%, 1.7%] 1
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 3
Improvements ✅
(secondary)
-0.5% [-1.2%, -0.2%] 20
All ❌✅ (primary) -0.4% [-0.5%, 0.1%] 4

Max RSS (memory usage)

Results

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)
3.7% [3.3%, 4.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.5% [-5.9%, -2.3%] 4
All ❌✅ (primary) - - 0

Cycles

Results

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.5% [5.2%, 5.7%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) - - 0

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 19, 2023
@michaelwoerister michaelwoerister marked this pull request as ready for review January 19, 2023 14:57
@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@michaelwoerister michaelwoerister changed the title (WIP) Use UnordMap and UnordSet for id collections (DefIdMap, LocalDefIdMap, etc) Use UnordMap and UnordSet for id collections (DefIdMap, LocalDefIdMap, etc) Jan 19, 2023
@michaelwoerister
Copy link
Member Author

Performance looks OK now, I think.

r? compiler (see MCP 533 for background information)

Comment on lines 445 to 451
let mut items: Vec<(&K, &V)> = self.inner.iter().collect();
if cache_sort_key {
items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
} else {
items.sort_unstable_by_key(|(k, _)| k.to_stable_hash_key(hcx));
}
items.into_iter().map(|(_, v)| v)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a recurring pattern, maybe give it a helper?

It should at least have documentation what cache_sort_key does

@oli-obk
Copy link
Contributor

oli-obk commented Jan 19, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jan 19, 2023

📌 Commit f219771 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 19, 2023
@michaelwoerister
Copy link
Member Author

Thanks for the review, @oli-obk!

@bors
Copy link
Contributor

bors commented Jan 21, 2023

⌛ Testing commit f219771 with merge 005fc0f...

@bors
Copy link
Contributor

bors commented Jan 21, 2023

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 005fc0f to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 21, 2023
@bors bors merged commit 005fc0f into rust-lang:master Jan 21, 2023
@rustbot rustbot added this to the 1.68.0 milestone Jan 21, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (005fc0f): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

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

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.3%] 2
Regressions ❌
(secondary)
0.6% [0.4%, 0.8%] 7
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 3
Improvements ✅
(secondary)
-0.3% [-0.7%, -0.2%] 20
All ❌✅ (primary) -0.2% [-0.5%, 0.3%] 5

Max RSS (memory usage)

Results

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.5% [1.2%, 5.3%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-6.6%, -2.6%] 3
All ❌✅ (primary) - - 0

Cycles

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

compiler-errors added a commit to compiler-errors/rust that referenced this pull request Jan 22, 2023
rustdoc: Use `DefId(Map,Set)` instead of `FxHash(Map,Set)`

Not all uses are converted, a few cases iterating through maps/sets and requiring nontrivial changes are kept.
cc rust-lang#106977
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 22, 2023
rustdoc: Use `DefId(Map,Set)` instead of `FxHash(Map,Set)`

Not all uses are converted, a few cases iterating through maps/sets and requiring nontrivial changes are kept.
cc rust-lang#106977
@rylev
Copy link
Member

rylev commented Jan 25, 2023

@michaelwoerister perf got slightly worse from the original perf run, but it's still enough of a wash that I don't think it needs investigating.

@rustbot label +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jan 25, 2023
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jan 27, 2023
Use UnordMap and UnordSet for id collections (DefIdMap, LocalDefIdMap, etc)

This PR changes the `rustc_data_structures::define_id_collections!` macro to use `UnordMap` and `UnordSet` instead of `FxHashMap` and `FxHashSet`. This should account for a large portion of hash-maps being used in places where they can cause trouble.

The changes required are moderate but non-zero:
- In some places the collections are extracted into sorted vecs.
- There are a few instances where for-loops have been changed to extends.

~~Let's see what the performance impact is. With a bit more refactoring, we might be able to get rid of some of the additional sorting -- but the change set is already big enough. Unless there's a performance impact, I'd like to do further changes in subsequent PRs.~~

Performance does not seem to be negatively affected ([perf-run here](rust-lang/rust#106977 (comment))).

Part of [MCP 533](rust-lang/compiler-team#533).

r? `@ghost`
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. perf-regression-triaged The performance regression has been triaged. 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.

6 participants