@@ -464,8 +464,10 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
464464 rustc_span:: hygiene:: clear_syntax_context_map ( ) ;
465465 }
466466
467- let hir_hash = compute_hir_hash ( tcx, & owners) ;
468- hir:: Crate { owners, hir_hash }
467+ // Don't hash unless necessary, because it's expensive.
468+ let opt_hir_hash =
469+ if tcx. sess . needs_crate_hash ( ) { Some ( compute_hir_hash ( tcx, & owners) ) } else { None } ;
470+ hir:: Crate { owners, opt_hir_hash }
469471}
470472
471473#[ derive( Copy , Clone , PartialEq , Debug ) ]
@@ -658,42 +660,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
658660
659661 bodies. sort_by_key ( |( k, _) | * k) ;
660662 let bodies = SortedMap :: from_presorted_elements ( bodies) ;
661- let ( hash_including_bodies, hash_without_bodies) = self . hash_owner ( node, & bodies) ;
662- let ( nodes, parenting) =
663- index:: index_hir ( self . tcx . sess , & * self . tcx . definitions_untracked ( ) , node, & bodies) ;
664- let nodes = hir:: OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies } ;
665- let attrs = {
666- let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
663+
664+ // Don't hash unless necessary, because it's expensive.
665+ let ( opt_hash_including_bodies, attrs_hash) = if self . tcx . sess . needs_crate_hash ( ) {
666+ self . tcx . with_stable_hashing_context ( |mut hcx| {
667+ let mut stable_hasher = StableHasher :: new ( ) ;
668+ hcx. with_hir_bodies ( node. def_id ( ) , & bodies, |hcx| {
669+ node. hash_stable ( hcx, & mut stable_hasher)
670+ } ) ;
671+ let h1 = stable_hasher. finish ( ) ;
672+
667673 let mut stable_hasher = StableHasher :: new ( ) ;
668674 attrs. hash_stable ( & mut hcx, & mut stable_hasher) ;
669- stable_hasher. finish ( )
670- } ) ;
671- hir:: AttributeMap { map : attrs, hash }
675+ let h2 = stable_hasher. finish ( ) ;
676+
677+ ( Some ( h1) , Some ( h2) )
678+ } )
679+ } else {
680+ ( None , None )
672681 } ;
682+ let ( nodes, parenting) =
683+ index:: index_hir ( self . tcx . sess , & * self . tcx . definitions_untracked ( ) , node, & bodies) ;
684+ let nodes = hir:: OwnerNodes { opt_hash_including_bodies, nodes, bodies } ;
685+ let attrs = hir:: AttributeMap { map : attrs, opt_hash : attrs_hash } ;
673686
674687 self . arena . alloc ( hir:: OwnerInfo { nodes, parenting, attrs, trait_map } )
675688 }
676689
677- /// Hash the HIR node twice, one deep and one shallow hash. This allows to differentiate
678- /// queries which depend on the full HIR tree and those which only depend on the item signature.
679- fn hash_owner (
680- & mut self ,
681- node : hir:: OwnerNode < ' hir > ,
682- bodies : & SortedMap < hir:: ItemLocalId , & ' hir hir:: Body < ' hir > > ,
683- ) -> ( Fingerprint , Fingerprint ) {
684- self . tcx . with_stable_hashing_context ( |mut hcx| {
685- let mut stable_hasher = StableHasher :: new ( ) ;
686- hcx. with_hir_bodies ( node. def_id ( ) , bodies, |hcx| {
687- node. hash_stable ( hcx, & mut stable_hasher)
688- } ) ;
689- let hash_including_bodies = stable_hasher. finish ( ) ;
690- let mut stable_hasher = StableHasher :: new ( ) ;
691- hcx. without_hir_bodies ( |hcx| node. hash_stable ( hcx, & mut stable_hasher) ) ;
692- let hash_without_bodies = stable_hasher. finish ( ) ;
693- ( hash_including_bodies, hash_without_bodies)
694- } )
695- }
696-
697690 /// This method allocates a new `HirId` for the given `NodeId` and stores it in
698691 /// the `LoweringContext`'s `NodeId => HirId` map.
699692 /// Take care not to call this method if the resulting `HirId` is then not
0 commit comments