104104//! implemented.
105105
106106use crate :: errors;
107- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
107+ use rustc_data_structures:: fx:: { FxHashSet , FxIndexSet } ;
108108use rustc_data_structures:: svh:: Svh ;
109+ use rustc_data_structures:: unord:: { UnordMap , UnordSet } ;
109110use rustc_data_structures:: { base_n, flock} ;
110111use rustc_errors:: ErrorGuaranteed ;
111112use rustc_fs_util:: { link_or_copy, try_canonicalize, LinkOrCopy } ;
@@ -636,7 +637,7 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
636637 // First do a pass over the crate directory, collecting lock files and
637638 // session directories
638639 let mut session_directories = FxIndexSet :: default ( ) ;
639- let mut lock_files = FxIndexSet :: default ( ) ;
640+ let mut lock_files = UnordSet :: default ( ) ;
640641
641642 for dir_entry in crate_directory. read_dir ( ) ? {
642643 let Ok ( dir_entry) = dir_entry else {
@@ -659,30 +660,28 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
659660 }
660661
661662 // Now map from lock files to session directories
662- let lock_file_to_session_dir: FxIndexMap < String , Option < String > > = lock_files
663- . into_iter ( )
664- . map ( |lock_file_name| {
663+ let lock_file_to_session_dir: UnordMap < String , Option < String > > =
664+ UnordMap :: from ( lock_files. into_items ( ) . map ( |lock_file_name| {
665665 assert ! ( lock_file_name. ends_with( LOCK_FILE_EXT ) ) ;
666666 let dir_prefix_end = lock_file_name. len ( ) - LOCK_FILE_EXT . len ( ) ;
667667 let session_dir = {
668668 let dir_prefix = & lock_file_name[ 0 ..dir_prefix_end] ;
669669 session_directories. iter ( ) . find ( |dir_name| dir_name. starts_with ( dir_prefix) )
670670 } ;
671671 ( lock_file_name, session_dir. map ( String :: clone) )
672- } )
673- . collect ( ) ;
672+ } ) ) ;
674673
675674 // Delete all lock files, that don't have an associated directory. They must
676675 // be some kind of leftover
677- for ( lock_file_name, directory_name) in & lock_file_to_session_dir {
676+ lock_file_to_session_dir . items ( ) . all ( | ( lock_file_name, directory_name) | {
678677 if directory_name. is_none ( ) {
679678 let Ok ( timestamp) = extract_timestamp_from_session_dir ( lock_file_name) else {
680679 debug ! (
681680 "found lock-file with malformed timestamp: {}" ,
682681 crate_directory. join( & lock_file_name) . display( )
683682 ) ;
684683 // Ignore it
685- continue ;
684+ return true ;
686685 } ;
687686
688687 let lock_file_path = crate_directory. join ( & * * lock_file_name) ;
@@ -702,17 +701,18 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
702701 ) ;
703702 }
704703 }
705- }
704+ true
705+ } ) ;
706706
707707 // Filter out `None` directories
708- let lock_file_to_session_dir: FxIndexMap < String , String > = lock_file_to_session_dir
709- . into_iter ( )
710- . filter_map ( |( lock_file_name, directory_name) | directory_name. map ( |n| ( lock_file_name, n) ) )
711- . collect ( ) ;
708+ let lock_file_to_session_dir: UnordMap < String , String > =
709+ UnordMap :: from ( lock_file_to_session_dir . into_items ( ) . filter_map (
710+ |( lock_file_name, directory_name) | directory_name. map ( |n| ( lock_file_name, n) ) ,
711+ ) ) ;
712712
713713 // Delete all session directories that don't have a lock file.
714714 for directory_name in session_directories {
715- if !lock_file_to_session_dir. values ( ) . any ( |dir| * dir == directory_name) {
715+ if !lock_file_to_session_dir. items ( ) . any ( |( _ , dir) | * dir == directory_name) {
716716 let path = crate_directory. join ( directory_name) ;
717717 if let Err ( err) = safe_remove_dir_all ( & path) {
718718 sess. emit_warning ( errors:: InvalidGcFailed { path : & path, err } ) ;
@@ -721,103 +721,103 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
721721 }
722722
723723 // Now garbage collect the valid session directories.
724- let mut deletion_candidates = vec ! [ ] ;
725-
726- for ( lock_file_name, directory_name) in & lock_file_to_session_dir {
727- debug ! ( "garbage_collect_session_directories() - inspecting: {}" , directory_name) ;
724+ let deletion_candidates =
725+ lock_file_to_session_dir. items ( ) . filter_map ( |( lock_file_name, directory_name) | {
726+ debug ! ( "garbage_collect_session_directories() - inspecting: {}" , directory_name) ;
728727
729- let Ok ( timestamp) = extract_timestamp_from_session_dir ( directory_name) else {
728+ let Ok ( timestamp) = extract_timestamp_from_session_dir ( directory_name) else {
730729 debug ! (
731730 "found session-dir with malformed timestamp: {}" ,
732731 crate_directory. join( directory_name) . display( )
733732 ) ;
734733 // Ignore it
735- continue ;
734+ return None ;
736735 } ;
737736
738- if is_finalized ( directory_name) {
739- let lock_file_path = crate_directory. join ( lock_file_name) ;
740- match flock:: Lock :: new (
741- & lock_file_path,
742- false , // don't wait
743- false , // don't create the lock-file
744- true ,
745- ) {
746- // get an exclusive lock
747- Ok ( lock) => {
748- debug ! (
749- "garbage_collect_session_directories() - \
737+ if is_finalized ( directory_name) {
738+ let lock_file_path = crate_directory. join ( lock_file_name) ;
739+ match flock:: Lock :: new (
740+ & lock_file_path,
741+ false , // don't wait
742+ false , // don't create the lock-file
743+ true ,
744+ ) {
745+ // get an exclusive lock
746+ Ok ( lock) => {
747+ debug ! (
748+ "garbage_collect_session_directories() - \
750749 successfully acquired lock"
751- ) ;
752- debug ! (
753- "garbage_collect_session_directories() - adding \
750+ ) ;
751+ debug ! (
752+ "garbage_collect_session_directories() - adding \
754753 deletion candidate: {}",
755- directory_name
756- ) ;
757-
758- // Note that we are holding on to the lock
759- deletion_candidates. push ( (
760- timestamp,
761- crate_directory. join ( directory_name) ,
762- Some ( lock) ,
763- ) ) ;
764- }
765- Err ( _) => {
766- debug ! (
767- "garbage_collect_session_directories() - \
754+ directory_name
755+ ) ;
756+
757+ // Note that we are holding on to the lock
758+ return Some ( (
759+ ( timestamp, crate_directory. join ( directory_name) ) ,
760+ Some ( lock) ,
761+ ) ) ;
762+ }
763+ Err ( _) => {
764+ debug ! (
765+ "garbage_collect_session_directories() - \
768766 not collecting, still in use"
769- ) ;
767+ ) ;
768+ }
770769 }
771- }
772- } else if is_old_enough_to_be_collected ( timestamp) {
773- // When cleaning out "-working" session directories, i.e.
774- // session directories that might still be in use by another
775- // compiler instance, we only look a directories that are
776- // at least ten seconds old. This is supposed to reduce the
777- // chance of deleting a directory in the time window where
778- // the process has allocated the directory but has not yet
779- // acquired the file-lock on it.
780-
781- // Try to acquire the directory lock. If we can't, it
782- // means that the owning process is still alive and we
783- // leave this directory alone.
784- let lock_file_path = crate_directory. join ( lock_file_name) ;
785- match flock:: Lock :: new (
786- & lock_file_path,
787- false , // don't wait
788- false , // don't create the lock-file
789- true ,
790- ) {
791- // get an exclusive lock
792- Ok ( lock) => {
793- debug ! (
794- "garbage_collect_session_directories() - \
770+ } else if is_old_enough_to_be_collected ( timestamp) {
771+ // When cleaning out "-working" session directories, i.e.
772+ // session directories that might still be in use by another
773+ // compiler instance, we only look a directories that are
774+ // at least ten seconds old. This is supposed to reduce the
775+ // chance of deleting a directory in the time window where
776+ // the process has allocated the directory but has not yet
777+ // acquired the file-lock on it.
778+
779+ // Try to acquire the directory lock. If we can't, it
780+ // means that the owning process is still alive and we
781+ // leave this directory alone.
782+ let lock_file_path = crate_directory. join ( lock_file_name) ;
783+ match flock:: Lock :: new (
784+ & lock_file_path,
785+ false , // don't wait
786+ false , // don't create the lock-file
787+ true ,
788+ ) {
789+ // get an exclusive lock
790+ Ok ( lock) => {
791+ debug ! (
792+ "garbage_collect_session_directories() - \
795793 successfully acquired lock"
796- ) ;
794+ ) ;
797795
798- delete_old ( sess, & crate_directory. join ( directory_name) ) ;
796+ delete_old ( sess, & crate_directory. join ( directory_name) ) ;
799797
800- // Let's make it explicit that the file lock is released at this point,
801- // or rather, that we held on to it until here
802- drop ( lock) ;
803- }
804- Err ( _) => {
805- debug ! (
806- "garbage_collect_session_directories() - \
798+ // Let's make it explicit that the file lock is released at this point,
799+ // or rather, that we held on to it until here
800+ drop ( lock) ;
801+ }
802+ Err ( _) => {
803+ debug ! (
804+ "garbage_collect_session_directories() - \
807805 not collecting, still in use"
808- ) ;
806+ ) ;
807+ }
809808 }
810- }
811- } else {
812- debug ! (
813- "garbage_collect_session_directories() - not finalized, not \
809+ } else {
810+ debug ! (
811+ "garbage_collect_session_directories() - not finalized, not \
814812 old enough"
815- ) ;
816- }
817- }
813+ ) ;
814+ }
815+ None
816+ } ) ;
817+ let deletion_candidates = UnordMap :: from ( deletion_candidates) ;
818818
819819 // Delete all but the most recent of the candidates
820- for ( path, lock) in all_except_most_recent ( deletion_candidates ) {
820+ all_except_most_recent ( deletion_candidates ) . into_items ( ) . all ( | ( path, lock) | {
821821 debug ! ( "garbage_collect_session_directories() - deleting `{}`" , path. display( ) ) ;
822822
823823 if let Err ( err) = safe_remove_dir_all ( & path) {
@@ -829,7 +829,8 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
829829 // Let's make it explicit that the file lock is released at this point,
830830 // or rather, that we held on to it until here
831831 drop ( lock) ;
832- }
832+ true
833+ } ) ;
833834
834835 Ok ( ( ) )
835836}
@@ -845,18 +846,19 @@ fn delete_old(sess: &Session, path: &Path) {
845846}
846847
847848fn all_except_most_recent (
848- deletion_candidates : Vec < ( SystemTime , PathBuf , Option < flock:: Lock > ) > ,
849- ) -> FxIndexMap < PathBuf , Option < flock:: Lock > > {
850- let most_recent = deletion_candidates. iter ( ) . map ( |& ( timestamp, .. ) | timestamp) . max ( ) ;
849+ deletion_candidates : UnordMap < ( SystemTime , PathBuf ) , Option < flock:: Lock > > ,
850+ ) -> UnordMap < PathBuf , Option < flock:: Lock > > {
851+ let most_recent = deletion_candidates. items ( ) . map ( |( & ( timestamp, _ ) , _ ) | timestamp) . max ( ) ;
851852
852853 if let Some ( most_recent) = most_recent {
853- deletion_candidates
854- . into_iter ( )
855- . filter ( |& ( timestamp, ..) | timestamp != most_recent)
856- . map ( |( _, path, lock) | ( path, lock) )
857- . collect ( )
854+ UnordMap :: from (
855+ deletion_candidates
856+ . into_items ( )
857+ . filter ( |& ( ( timestamp, _) , _) | timestamp != most_recent)
858+ . map ( |( ( _, path) , lock) | ( path, lock) ) ,
859+ )
858860 } else {
859- FxIndexMap :: default ( )
861+ UnordMap :: default ( )
860862 }
861863}
862864
0 commit comments