about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-04-12 04:38:23 +0200
committerGitHub <noreply@github.com>2024-04-12 04:38:23 +0200
commit7f111834ad7219f32b7bdfb4e2fb4c41ca41c24f (patch)
treeb2a5132784bc19fe0c4d53d6ac9a09a0021c7135 /compiler/rustc_codegen_ssa/src
parent3758e2ffa5f14d1cb50ff6d1436489bb3749f627 (diff)
parent4ded0b82caf33969d44b47c20afcbde4cf523424 (diff)
downloadrust-7f111834ad7219f32b7bdfb4e2fb4c41ca41c24f.tar.gz
rust-7f111834ad7219f32b7bdfb4e2fb4c41ca41c24f.zip
Rollup merge of #123827 - petrochenkov:searchdirs, r=Nadrieril
linker: Avoid some allocations in search directory iteration

This is more a cleanup than actual optimization.
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/back/link.rs27
1 files changed, 12 insertions, 15 deletions
diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs
index ac278de02af..f4374392b9b 100644
--- a/compiler/rustc_codegen_ssa/src/back/link.rs
+++ b/compiler/rustc_codegen_ssa/src/back/link.rs
@@ -56,8 +56,13 @@ use std::{env, fmt, fs, io, mem, str};
 pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
 
 impl SearchPaths {
-    pub(super) fn get(&self, sess: &Session) -> &[PathBuf] {
-        self.0.get_or_init(|| archive_search_paths(sess))
+    pub(super) fn get(&self, sess: &Session) -> impl Iterator<Item = &Path> {
+        let native_search_paths = || {
+            Vec::from_iter(
+                sess.target_filesearch(PathKind::Native).search_path_dirs().map(|p| p.to_owned()),
+            )
+        };
+        self.0.get_or_init(native_search_paths).iter().map(|p| &**p)
     }
 }
 
@@ -310,8 +315,6 @@ fn link_rlib<'a>(
     flavor: RlibFlavor,
     tmpdir: &MaybeTempDir,
 ) -> Result<Box<dyn ArchiveBuilder + 'a>, ErrorGuaranteed> {
-    let lib_search_paths = archive_search_paths(sess);
-
     let mut ab = archive_builder_builder.new_archive_builder(sess);
 
     let trailing_metadata = match flavor {
@@ -378,26 +381,24 @@ fn link_rlib<'a>(
     // feature then we'll need to figure out how to record what objects were
     // loaded from the libraries found here and then encode that into the
     // metadata of the rlib we're generating somehow.
+    let search_paths = SearchPaths::default();
     for lib in codegen_results.crate_info.used_libraries.iter() {
         let NativeLibKind::Static { bundle: None | Some(true), .. } = lib.kind else {
             continue;
         };
+        let search_paths = search_paths.get(sess);
         if flavor == RlibFlavor::Normal
             && let Some(filename) = lib.filename
         {
-            let path = find_native_static_library(filename.as_str(), true, &lib_search_paths, sess);
+            let path = find_native_static_library(filename.as_str(), true, search_paths, sess);
             let src = read(path)
                 .map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?;
             let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src);
             let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
             packed_bundled_libs.push(wrapper_file);
         } else {
-            let path = find_native_static_library(
-                lib.name.as_str(),
-                lib.verbatim,
-                &lib_search_paths,
-                sess,
-            );
+            let path =
+                find_native_static_library(lib.name.as_str(), lib.verbatim, search_paths, sess);
             ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
                 sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error })
             });
@@ -1445,10 +1446,6 @@ fn preserve_objects_for_their_debuginfo(sess: &Session) -> (bool, bool) {
     }
 }
 
-fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
-    sess.target_filesearch(PathKind::Native).search_path_dirs()
-}
-
 #[derive(PartialEq)]
 enum RlibFlavor {
     Normal,