-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Improve resolver speed again #14665
Open
x-hgg-x
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
x-hgg-x:resolver-perf-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−52
Open
Improve resolver speed again #14665
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::collections::HashSet; | ||
use std::hash::{Hash, Hasher}; | ||
use std::num::NonZeroU64; | ||
use std::sync::{Mutex, OnceLock}; | ||
|
||
use crate::core::SourceId; | ||
use crate::util::interning::InternedString; | ||
|
||
static ACTIVATION_KEY_CACHE: OnceLock<Mutex<HashSet<&'static ActivationKeyInner>>> = | ||
OnceLock::new(); | ||
|
||
type ActivationKeyInner = (InternedString, SourceId, SemverCompatibility); | ||
|
||
/// The activated version of a crate is based on the name, source, and semver compatibility. | ||
#[derive(Clone, Copy, Eq)] | ||
pub struct ActivationKey { | ||
inner: &'static ActivationKeyInner, | ||
} | ||
|
||
impl From<ActivationKeyInner> for ActivationKey { | ||
fn from(inner: ActivationKeyInner) -> Self { | ||
let mut cache = ACTIVATION_KEY_CACHE | ||
.get_or_init(|| Default::default()) | ||
.lock() | ||
.unwrap(); | ||
let inner = cache.get(&inner).cloned().unwrap_or_else(|| { | ||
let inner = Box::leak(Box::new(inner)); | ||
cache.insert(inner); | ||
inner | ||
}); | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl ActivationKey { | ||
/// This function is used for the `Eq` and `Hash` impls to implement a "no hash" hashable value. | ||
/// This is possible since all `ActivationKey` are already interned in a `HashSet`. | ||
fn key(&self) -> u64 { | ||
std::ptr::from_ref(self.inner) as u64 | ||
} | ||
} | ||
|
||
impl PartialEq for ActivationKey { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.key() == other.key() | ||
} | ||
} | ||
|
||
impl nohash_hasher::IsEnabled for ActivationKey {} | ||
|
||
impl Hash for ActivationKey { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
state.write_u64(self.key()); | ||
} | ||
} | ||
|
||
/// A type that represents when cargo treats two versions as compatible. | ||
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the same. | ||
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)] | ||
pub enum SemverCompatibility { | ||
Major(NonZeroU64), | ||
Minor(NonZeroU64), | ||
Patch(u64), | ||
} | ||
|
||
impl From<&semver::Version> for SemverCompatibility { | ||
fn from(ver: &semver::Version) -> Self { | ||
if let Some(m) = NonZeroU64::new(ver.major) { | ||
return SemverCompatibility::Major(m); | ||
} | ||
if let Some(m) = NonZeroU64::new(ver.minor) { | ||
return SemverCompatibility::Minor(m); | ||
} | ||
SemverCompatibility::Patch(ver.patch) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: This PR does a number of different things, and I'm trying to understand which ones are critical to the performance win. I tried switching this one
nohash_hasher::BuildNoHashHasher
back torustc_hash::FxBuildHasher
, and the Solana only benchmark I was running yesterday runs in 162.53min (or 164.63min there seems to be a surprising amount of run to run variability).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it seems that
rustc-hash
is already very optimized when hashing a singleu64
:https://github.com/rust-lang/rustc-hash/blob/master/src/lib.rs#L99.
In this case the performance increase may only be due to fact that we don't hash the full
ActivationKeyInner
but only its address.