@@ -243,12 +243,14 @@ use std::cmp;
243243use std:: fmt;
244244use std:: fs;
245245use std:: io:: { self , Read } ;
246+ use std:: ops:: Deref ;
246247use std:: path:: { Path , PathBuf } ;
247248use std:: time:: Instant ;
248249
249250use flate2:: read:: DeflateDecoder ;
250251
251252use rustc_data_structures:: owning_ref:: OwningRef ;
253+
252254pub struct CrateMismatch {
253255 path : PathBuf ,
254256 got : String ,
@@ -856,6 +858,19 @@ fn get_metadata_section(target: &Target,
856858 return ret;
857859}
858860
861+ /// A trivial wrapper for `Mmap` that implements `StableDeref`.
862+ struct StableDerefMmap ( memmap:: Mmap ) ;
863+
864+ impl Deref for StableDerefMmap {
865+ type Target = [ u8 ] ;
866+
867+ fn deref ( & self ) -> & [ u8 ] {
868+ self . 0 . deref ( )
869+ }
870+ }
871+
872+ unsafe impl stable_deref_trait:: StableDeref for StableDerefMmap { }
873+
859874fn get_metadata_section_imp ( target : & Target ,
860875 flavor : CrateFlavor ,
861876 filename : & Path ,
@@ -892,9 +907,14 @@ fn get_metadata_section_imp(target: &Target,
892907 }
893908 }
894909 CrateFlavor :: Rmeta => {
895- let buf = fs:: read ( filename) . map_err ( |_|
896- format ! ( "failed to read rmeta metadata: '{}'" , filename. display( ) ) ) ?;
897- rustc_erase_owner ! ( OwningRef :: new( buf) . map_owner_box( ) )
910+ // mmap the file, because only a small fraction of it is read.
911+ let file = std:: fs:: File :: open ( filename) . map_err ( |_|
912+ format ! ( "failed to open rmeta metadata: '{}'" , filename. display( ) ) ) ?;
913+ let mmap = unsafe { memmap:: Mmap :: map ( & file) } ;
914+ let mmap = mmap. map_err ( |_|
915+ format ! ( "failed to mmap rmeta metadata: '{}'" , filename. display( ) ) ) ?;
916+
917+ rustc_erase_owner ! ( OwningRef :: new( StableDerefMmap ( mmap) ) . map_owner_box( ) )
898918 }
899919 } ;
900920 let blob = MetadataBlob ( raw_bytes) ;
0 commit comments