Skip to content

Investigate computing crate_hash by hashing metadata #94878

Closed

Description

@cjgillot

View all comments

The crate_hash query is used to detect is metadata has changed before attempting to decode more complex information from it.
Until now, this crate hash has been computed manually using HIR.
Computing it while encoding metadata itself may be more robust and exhaustive.

General instructions:

  • add a StableHasher and a StableHashingContext in rustc_metadata::rmeta::encoder::EncodeContext;
  • double all accesses to the embedded opaque encoder by a call to hash_stable with the same data;
  • manually reimplement encoding and hashing for special datatypes (CrateNum, DefId, LocalDefId...);
  • remove the implementation of the crate_hash query in rustc_middle::hir::map, and reimplement it from the just-encoded metadata;
  • evaluate effect on performance.

Please reach out on zulip if more details are required.
cc @michaelwoerister

Activity

  1. added
    E-mentor
    E-help-wanted
    E-medium
    on Mar 12, 2022
  2. michaelwoerister commented on Mar 15, 2022

    @michaelwoerister
    Contributor

    I think this is very promising. I would suggest to entirely remove the local version of the crate_hash query. That should make things easier to implement.

  3. lqd commented on Mar 15, 2022

    @lqd
    Member

    @rustbot claim

  4. bjorn3 commented on Mar 15, 2022

    @bjorn3
    Member

    The crate metadata is missing the session configuration with which the crate has been compiled. This means that eg changing optimization level probably won't change crate metadata. It probably needs to change the crate hash though. Including tcx.sess.opts.dep_tracking_hash(true) should be enough:

    tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);

  5. michaelwoerister commented on Mar 15, 2022

    @michaelwoerister
    Contributor

    Ideally it wouldn't be necessary because the hash includes everything that is accessible to downstream crates, right?

  6. bjorn3 commented on Mar 15, 2022

    @bjorn3
    Member

    The object files are accessible to downstream crates (they are linked into the final artifact after all), but they depend on both the crate metadata and the compilation session options.

  7. michaelwoerister commented on Mar 15, 2022

    @michaelwoerister
    Contributor

    Yes, but linking is done unconditionally, independent of the crate hash.

  8. bjorn3 commented on Mar 15, 2022

    @bjorn3
    Member

    I think it isn't too far fetched that cargo or other build systems will use the crate hash in the far future instead of mtime or hashing the entire file.

  9. michaelwoerister commented on Mar 15, 2022

    @michaelwoerister
    Contributor

    I don't know. In my opinion the crate hash is an internal implementation detail of the incremental compilation system that's only there to optimize a common code path there. I wouldn't recommend any outside tooling to rely on it.

  10. bjorn3 commented on Mar 15, 2022

    @bjorn3
    Member

    The crate hash existed long before incremental compilation was introduced afaik.

  11. bjorn3 commented on Mar 15, 2022

    @bjorn3
    Member

    The SVH has introduced feb 2014 in ec57db0

    The goal of this hash is to identify when upstream crates have changed but
    downstream crates have not been recompiled.

  12. lqd commented on Apr 22, 2022

    @lqd
    Member

    (I've begun looking at this already, but I've unfortunately realized that I won't have time to work on this soon enough.

    Since it's both short-to-try and has mentorship available from michael and camille, it should be a good opportunity for a new contributor, rather than waiting on me and taking this opportunity away from someone who's interested.

    @rustbot release-assignment
    )

  13. 30 remaining items

  14. added a commit that references this issue on Sep 17, 2026
  15. added 2 commits that reference this issue on Sep 25, 2026
  16. Kobzol commented on Sep 27, 2026

    @Kobzol
    Member

    Should this be closed, now that #154724 has landed?

  17. added 4 commits that reference this issue on Sep 30, 2026
  18. added a commit that references this issue on Oct 1, 2026
  19. added a commit that references this issue on Oct 1, 2026
  20. Daniel-B-Smith commented on Oct 16, 2026

    @Daniel-B-Smith
    Contributor

    I have no strong opinion and am willing to do whatever the local norm is. I have been leaving it open out of paranoia that we'll find a critical bug and need to roll back and retry.

  21. Kobzol commented on Oct 16, 2026

    @Kobzol
    Member

    In that case it can be reopened. I think that we can close this now. Great work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

E-help-wantedE-mediumE-mentorT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions