Skip to content

Commit 06bbc9f

Browse files
committed
Auto merge of #123854 - petrochenkov:searchdirs2, r=<try>
linker: Remove laziness and caching from native search directory walks It shouldn't be necessary for performance now. Follow up to #123827.
2 parents 7bdae13 + ed62b57 commit 06bbc9f

File tree

5 files changed

+23
-122
lines changed

5 files changed

+23
-122
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use regex::Regex;
4242
use tempfile::Builder as TempFileBuilder;
4343

4444
use itertools::Itertools;
45-
use std::cell::OnceCell;
4645
use std::collections::BTreeSet;
4746
use std::ffi::{OsStr, OsString};
4847
use std::fs::{read, File, OpenOptions};
@@ -52,20 +51,6 @@ use std::path::{Path, PathBuf};
5251
use std::process::{ExitStatus, Output, Stdio};
5352
use std::{env, fmt, fs, io, mem, str};
5453

55-
#[derive(Default)]
56-
pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
57-
58-
impl SearchPaths {
59-
pub(super) fn get(&self, sess: &Session) -> impl Iterator<Item = &Path> {
60-
let native_search_paths = || {
61-
Vec::from_iter(
62-
sess.target_filesearch(PathKind::Native).search_path_dirs().map(|p| p.to_owned()),
63-
)
64-
};
65-
self.0.get_or_init(native_search_paths).iter().map(|p| &**p)
66-
}
67-
}
68-
6954
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) {
7055
if let Err(e) = fs::remove_file(path) {
7156
if e.kind() != io::ErrorKind::NotFound {
@@ -381,24 +366,21 @@ fn link_rlib<'a>(
381366
// feature then we'll need to figure out how to record what objects were
382367
// loaded from the libraries found here and then encode that into the
383368
// metadata of the rlib we're generating somehow.
384-
let search_paths = SearchPaths::default();
385369
for lib in codegen_results.crate_info.used_libraries.iter() {
386370
let NativeLibKind::Static { bundle: None | Some(true), .. } = lib.kind else {
387371
continue;
388372
};
389-
let search_paths = search_paths.get(sess);
390373
if flavor == RlibFlavor::Normal
391374
&& let Some(filename) = lib.filename
392375
{
393-
let path = find_native_static_library(filename.as_str(), true, search_paths, sess);
376+
let path = find_native_static_library(filename.as_str(), true, sess);
394377
let src = read(path)
395378
.map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?;
396379
let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src);
397380
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
398381
packed_bundled_libs.push(wrapper_file);
399382
} else {
400-
let path =
401-
find_native_static_library(lib.name.as_str(), lib.verbatim, search_paths, sess);
383+
let path = find_native_static_library(lib.name.as_str(), lib.verbatim, sess);
402384
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
403385
sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error })
404386
});
@@ -2497,7 +2479,6 @@ fn add_native_libs_from_crate(
24972479
archive_builder_builder: &dyn ArchiveBuilderBuilder,
24982480
codegen_results: &CodegenResults,
24992481
tmpdir: &Path,
2500-
search_paths: &SearchPaths,
25012482
bundled_libs: &FxIndexSet<Symbol>,
25022483
cnum: CrateNum,
25032484
link_static: bool,
@@ -2560,7 +2541,7 @@ fn add_native_libs_from_crate(
25602541
cmd.link_staticlib_by_path(&path, whole_archive);
25612542
}
25622543
} else {
2563-
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
2544+
cmd.link_staticlib_by_name(name, verbatim, whole_archive);
25642545
}
25652546
}
25662547
}
@@ -2574,7 +2555,7 @@ fn add_native_libs_from_crate(
25742555
// link kind is unspecified.
25752556
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
25762557
if link_static {
2577-
cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
2558+
cmd.link_staticlib_by_name(name, verbatim, false);
25782559
}
25792560
} else {
25802561
if link_dynamic {
@@ -2621,7 +2602,6 @@ fn add_local_native_libraries(
26212602
}
26222603
}
26232604

2624-
let search_paths = SearchPaths::default();
26252605
// All static and dynamic native library dependencies are linked to the local crate.
26262606
let link_static = true;
26272607
let link_dynamic = true;
@@ -2631,7 +2611,6 @@ fn add_local_native_libraries(
26312611
archive_builder_builder,
26322612
codegen_results,
26332613
tmpdir,
2634-
&search_paths,
26352614
&Default::default(),
26362615
LOCAL_CRATE,
26372616
link_static,
@@ -2663,7 +2642,6 @@ fn add_upstream_rust_crates<'a>(
26632642
.find(|(ty, _)| *ty == crate_type)
26642643
.expect("failed to find crate type in dependency format list");
26652644

2666-
let search_paths = SearchPaths::default();
26672645
for &cnum in &codegen_results.crate_info.used_crates {
26682646
// We may not pass all crates through to the linker. Some crates may appear statically in
26692647
// an existing dylib, meaning we'll pick up all the symbols from the dylib.
@@ -2720,7 +2698,6 @@ fn add_upstream_rust_crates<'a>(
27202698
archive_builder_builder,
27212699
codegen_results,
27222700
tmpdir,
2723-
&search_paths,
27242701
&bundled_libs,
27252702
cnum,
27262703
link_static,
@@ -2738,7 +2715,6 @@ fn add_upstream_native_libraries(
27382715
tmpdir: &Path,
27392716
link_output_kind: LinkOutputKind,
27402717
) {
2741-
let search_paths = SearchPaths::default();
27422718
for &cnum in &codegen_results.crate_info.used_crates {
27432719
// Static libraries are not linked here, they are linked in `add_upstream_rust_crates`.
27442720
// FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries
@@ -2760,7 +2736,6 @@ fn add_upstream_native_libraries(
27602736
archive_builder_builder,
27612737
codegen_results,
27622738
tmpdir,
2763-
&search_paths,
27642739
&Default::default(),
27652740
cnum,
27662741
link_static,

compiler/rustc_codegen_ssa/src/back/linker.rs

+12-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::command::Command;
22
use super::symbol_export;
3-
use crate::back::link::SearchPaths;
43
use crate::errors;
54
use rustc_span::symbol::sym;
65

@@ -172,13 +171,7 @@ pub trait Linker {
172171
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
173172
bug!("framework linked with unsupported linker")
174173
}
175-
fn link_staticlib_by_name(
176-
&mut self,
177-
name: &str,
178-
verbatim: bool,
179-
whole_archive: bool,
180-
search_paths: &SearchPaths,
181-
);
174+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
182175
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
183176
fn include_path(&mut self, path: &Path);
184177
fn framework_path(&mut self, path: &Path);
@@ -482,13 +475,7 @@ impl<'a> Linker for GccLinker<'a> {
482475
self.cmd.arg("-framework").arg(name);
483476
}
484477

485-
fn link_staticlib_by_name(
486-
&mut self,
487-
name: &str,
488-
verbatim: bool,
489-
whole_archive: bool,
490-
search_paths: &SearchPaths,
491-
) {
478+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
492479
self.hint_static();
493480
let colon = if verbatim && self.is_gnu { ":" } else { "" };
494481
if !whole_archive {
@@ -497,8 +484,7 @@ impl<'a> Linker for GccLinker<'a> {
497484
// -force_load is the macOS equivalent of --whole-archive, but it
498485
// involves passing the full path to the library to link.
499486
self.linker_arg("-force_load");
500-
let search_paths = search_paths.get(self.sess);
501-
self.linker_arg(find_native_static_library(name, verbatim, search_paths, self.sess));
487+
self.linker_arg(find_native_static_library(name, verbatim, self.sess));
502488
} else {
503489
self.linker_arg("--whole-archive");
504490
self.cmd.arg(format!("-l{colon}{name}"));
@@ -825,13 +811,7 @@ impl<'a> Linker for MsvcLinker<'a> {
825811
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
826812
}
827813

828-
fn link_staticlib_by_name(
829-
&mut self,
830-
name: &str,
831-
verbatim: bool,
832-
whole_archive: bool,
833-
_search_paths: &SearchPaths,
834-
) {
814+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
835815
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
836816
let suffix = if verbatim { "" } else { ".lib" };
837817
self.cmd.arg(format!("{prefix}{name}{suffix}"));
@@ -1064,13 +1044,7 @@ impl<'a> Linker for EmLinker<'a> {
10641044
self.cmd.arg("-l").arg(name);
10651045
}
10661046

1067-
fn link_staticlib_by_name(
1068-
&mut self,
1069-
name: &str,
1070-
_verbatim: bool,
1071-
_whole_archive: bool,
1072-
_search_paths: &SearchPaths,
1073-
) {
1047+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
10741048
self.cmd.arg("-l").arg(name);
10751049
}
10761050

@@ -1243,13 +1217,7 @@ impl<'a> Linker for WasmLd<'a> {
12431217
self.cmd.arg("-l").arg(name);
12441218
}
12451219

1246-
fn link_staticlib_by_name(
1247-
&mut self,
1248-
name: &str,
1249-
_verbatim: bool,
1250-
whole_archive: bool,
1251-
_search_paths: &SearchPaths,
1252-
) {
1220+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
12531221
if !whole_archive {
12541222
self.cmd.arg("-l").arg(name);
12551223
} else {
@@ -1396,13 +1364,7 @@ impl<'a> Linker for L4Bender<'a> {
13961364
bug!("dylibs are not supported on L4Re");
13971365
}
13981366

1399-
fn link_staticlib_by_name(
1400-
&mut self,
1401-
name: &str,
1402-
_verbatim: bool,
1403-
whole_archive: bool,
1404-
_search_paths: &SearchPaths,
1405-
) {
1367+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
14061368
self.hint_static();
14071369
if !whole_archive {
14081370
self.cmd.arg(format!("-PC{name}"));
@@ -1580,20 +1542,13 @@ impl<'a> Linker for AixLinker<'a> {
15801542
self.cmd.arg(format!("-l{name}"));
15811543
}
15821544

1583-
fn link_staticlib_by_name(
1584-
&mut self,
1585-
name: &str,
1586-
verbatim: bool,
1587-
whole_archive: bool,
1588-
search_paths: &SearchPaths,
1589-
) {
1545+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
15901546
self.hint_static();
15911547
if !whole_archive {
15921548
self.cmd.arg(format!("-l{name}"));
15931549
} else {
15941550
let mut arg = OsString::from("-bkeepfile:");
1595-
let search_path = search_paths.get(self.sess);
1596-
arg.push(find_native_static_library(name, verbatim, search_path, self.sess));
1551+
arg.push(find_native_static_library(name, verbatim, self.sess));
15971552
self.cmd.arg(arg);
15981553
}
15991554
}
@@ -1792,13 +1747,7 @@ impl<'a> Linker for PtxLinker<'a> {
17921747
panic!("external dylibs not supported")
17931748
}
17941749

1795-
fn link_staticlib_by_name(
1796-
&mut self,
1797-
_name: &str,
1798-
_verbatim: bool,
1799-
_whole_archive: bool,
1800-
_search_paths: &SearchPaths,
1801-
) {
1750+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18021751
panic!("staticlibs not supported")
18031752
}
18041753

@@ -1880,13 +1829,7 @@ impl<'a> Linker for LlbcLinker<'a> {
18801829
panic!("external dylibs not supported")
18811830
}
18821831

1883-
fn link_staticlib_by_name(
1884-
&mut self,
1885-
_name: &str,
1886-
_verbatim: bool,
1887-
_whole_archive: bool,
1888-
_search_paths: &SearchPaths,
1889-
) {
1832+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18901833
panic!("staticlibs not supported")
18911834
}
18921835

@@ -1977,13 +1920,7 @@ impl<'a> Linker for BpfLinker<'a> {
19771920
panic!("external dylibs not supported")
19781921
}
19791922

1980-
fn link_staticlib_by_name(
1981-
&mut self,
1982-
_name: &str,
1983-
_verbatim: bool,
1984-
_whole_archive: bool,
1985-
_search_paths: &SearchPaths,
1986-
) {
1923+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
19871924
panic!("staticlibs not supported")
19881925
}
19891926

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn configure_and_expand(
171171
if cfg!(windows) {
172172
old_path = env::var_os("PATH").unwrap_or(old_path);
173173
let mut new_path = Vec::from_iter(
174-
sess.host_filesearch(PathKind::All).search_path_dirs().map(|p| p.to_owned()),
174+
sess.host_filesearch(PathKind::All).search_paths().map(|p| p.dir.clone()),
175175
);
176176
for path in env::split_paths(&old_path) {
177177
if !new_path.contains(&path) {

compiler/rustc_metadata/src/native_libs.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ use rustc_target::spec::abi::Abi;
1717

1818
use crate::errors;
1919

20-
use std::path::{Path, PathBuf};
21-
22-
pub fn find_native_static_library<'a>(
23-
name: &str,
24-
verbatim: bool,
25-
search_paths: impl Iterator<Item = &'a Path>,
26-
sess: &Session,
27-
) -> PathBuf {
20+
use std::path::PathBuf;
21+
22+
pub fn find_native_static_library(name: &str, verbatim: bool, sess: &Session) -> PathBuf {
2823
let formats = if verbatim {
2924
vec![("".into(), "".into())]
3025
} else {
@@ -35,9 +30,9 @@ pub fn find_native_static_library<'a>(
3530
if os == unix { vec![os] } else { vec![os, unix] }
3631
};
3732

38-
for path in search_paths {
33+
for path in sess.target_filesearch(PathKind::Native).search_paths() {
3934
for (prefix, suffix) in &formats {
40-
let test = path.join(format!("{prefix}{name}{suffix}"));
35+
let test = path.dir.join(format!("{prefix}{name}{suffix}"));
4136
if test.exists() {
4237
return test;
4338
}
@@ -60,8 +55,7 @@ fn find_bundled_library(
6055
&& (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
6156
{
6257
let verbatim = verbatim.unwrap_or(false);
63-
let search_paths = sess.target_filesearch(PathKind::Native).search_path_dirs();
64-
return find_native_static_library(name.as_str(), verbatim, search_paths, sess)
58+
return find_native_static_library(name.as_str(), verbatim, sess)
6559
.file_name()
6660
.and_then(|s| s.to_str())
6761
.map(Symbol::intern);

compiler/rustc_session/src/filesearch.rs

-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ impl<'a> FileSearch<'a> {
4545
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
4646
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
4747
}
48-
49-
/// Returns just the directories within the search paths.
50-
pub fn search_path_dirs(&self) -> impl Iterator<Item = &'a Path> {
51-
self.search_paths().map(|sp| &*sp.dir)
52-
}
5348
}
5449

5550
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {

0 commit comments

Comments
 (0)