]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_session/search_paths.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_session / search_paths.rs
index 06f408b4a8d64f8def249268172040bd2565e087..4ff06acaa1fd4eedcb7fc5799599a465673c04f6 100644 (file)
@@ -6,7 +6,31 @@ use std::path::{Path, PathBuf};
 pub struct SearchPath {
     pub kind: PathKind,
     pub dir: PathBuf,
-    pub files: Vec<PathBuf>,
+    pub files: Vec<SearchPathFile>,
+}
+
+// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
+// it is searched repeatedly by `find_library_crate`, and the searches involve
+// checking the prefix and suffix of the filename of each `PathBuf`. This is
+// doable, but very slow, because it involves calls to `file_name` and
+// `extension` that are themselves slow.
+//
+// This type augments the `PathBuf` with an `Option<String>` containing the
+// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
+// `Option<String>` than the `PathBuf`. (It's an `Option` because
+// `Path::file_name` can fail; if that happens then all subsequent checking
+// will also fail, which is fine.)
+#[derive(Clone, Debug)]
+pub struct SearchPathFile {
+    pub path: PathBuf,
+    pub file_name_str: Option<String>,
+}
+
+impl SearchPathFile {
+    fn new(path: PathBuf) -> SearchPathFile {
+        let file_name_str = path.file_name().and_then(|f| f.to_str()).map(|s| s.to_string());
+        SearchPathFile { path, file_name_str }
+    }
 }
 
 #[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, RustcEncodable, RustcDecodable)]
@@ -60,7 +84,9 @@ impl SearchPath {
     fn new(kind: PathKind, dir: PathBuf) -> Self {
         // Get the files within the directory.
         let files = match std::fs::read_dir(&dir) {
-            Ok(files) => files.filter_map(|p| p.ok().map(|s| s.path())).collect::<Vec<_>>(),
+            Ok(files) => files
+                .filter_map(|e| e.ok().map(|e| SearchPathFile::new(e.path())))
+                .collect::<Vec<_>>(),
             Err(..) => vec![],
         };