-
Notifications
You must be signed in to change notification settings - Fork 556
Open
Labels
A-incr-compArea: incremental compilationArea: incremental compilationC-enhancementCategory: enhancementCategory: enhancementE-help-wantedCall for participation: extra help is wantedCall for participation: extra help is wantedE-mediumDifficulty: might require some prior knowledge or code readingDifficulty: might require some prior knowledge or code readingE-needs-writeupCall for participation: discussion can be written up without much research requiredCall for participation: discussion can be written up without much research requiredT-compilerRelevant to compiler teamRelevant to compiler team
Description
So rustc is full of these impl_stable_hash_for
. What are these for? I originally thought that would be for FxHashMap
, but that seems to be wrong (I still need to derive(Hash)
to use FxHashMap
). So now I am just confused. It would be great if the guide could explain that.
@eddyb said "incremental" but that on its own does not explain much of anything -- why is Hash
not good enough? Why do I need a tcx
to compute a "stable hash"?
Metadata
Metadata
Assignees
Labels
A-incr-compArea: incremental compilationArea: incremental compilationC-enhancementCategory: enhancementCategory: enhancementE-help-wantedCall for participation: extra help is wantedCall for participation: extra help is wantedE-mediumDifficulty: might require some prior knowledge or code readingDifficulty: might require some prior knowledge or code readingE-needs-writeupCall for participation: discussion can be written up without much research requiredCall for participation: discussion can be written up without much research requiredT-compilerRelevant to compiler teamRelevant to compiler team
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
RalfJung commentedon Sep 20, 2018
I was told that @michaelwoerister knows all about this? :D
eddyb commentedon Sep 20, 2018
Our
Hash
impls hash pointers, IDs, etc. - they're designed for efficiency, not stability.Stability here is across compilations, and it means the hash depends on semantic data, not transient representation.
tcx
is needed to e.g. convert an ID into its "stable" representation / get a cached hash.RalfJung commentedon Sep 20, 2018
What kind of "ID" are you referring to?
RalfJung commentedon Sep 20, 2018
Okay so "stable" here means "guaranteed not to change between rustc invocations". We must not hash pointers, for example. Good to know.
michaelwoerister commentedon Sep 20, 2018
"Stable" here means stable across compilation sessions and crate boundaries. For example, if you
Hash
aTy
you get a different value in two different compiler processes (because you are actually hashing a pointer to an interned data structure). If youStableHash
it, the hash value will be the same for different invocations of the compiler, and it will also be the same, independently of whether the type was defined in the current crate being compiled or if it was loaded from an upstream crate.This is used for telling if something has changed in between to sessions (for incr. comp.) without actually having to have the value stored somewhere. Another example is the hash value at the end of every Rust symbol. This also needs be stable across sessions and crate boundaries.
RalfJung commentedon Sep 20, 2018
@michaelwoerister thanks, that helps! Why does this kind of stability require access to a "context" (
StableHashingContext
) though?eddyb commentedon Sep 20, 2018
@RalfJung To cache some kinds of more expensive hashes and to look up IDs (
NodeId
,DefId
, etc.), as you can't hash the numerical value of the ID, but rather the "definition" that it refers to.michaelwoerister commentedon Sep 21, 2018
It's not just caching.
NodeId
,DefId
,Span
, etc are not stable things. The context provides the data needed for mapping them into a stable format. For example mappingSpan
from au32
tofile:line:col
.eddyb commentedon Sep 21, 2018
That's what I meant by "looking up IDs".
michaelwoerister commentedon Sep 21, 2018
Right, I wasn't reading your answer properly
:)
8 remaining items