1
Fork 0

Rollup merge of #138783 - bjorn3:cache_current_dll_path, r=lqd

Cache current_dll_path output

Computing the current dll path is somewhat expensive relative to other work when compiling `fn main() {}` as `dladdr` needs to iterate over the symbol table of librustc_driver.so until it finds a match.
This commit is contained in:
Jacob Pratt 2025-03-23 20:44:12 -04:00 committed by GitHub
commit ab138e6aa8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -60,6 +60,14 @@ pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
#[cfg(unix)]
fn current_dll_path() -> Result<PathBuf, String> {
use std::sync::OnceLock;
// This is somewhat expensive relative to other work when compiling `fn main() {}` as `dladdr`
// needs to iterate over the symbol table of librustc_driver.so until it finds a match.
// As such cache this to avoid recomputing if we try to get the sysroot in multiple places.
static CURRENT_DLL_PATH: OnceLock<Result<PathBuf, String>> = OnceLock::new();
CURRENT_DLL_PATH
.get_or_init(|| {
use std::ffi::{CStr, OsStr};
use std::os::unix::prelude::*;
@ -115,11 +123,13 @@ fn current_dll_path() -> Result<PathBuf, String> {
if (*current).ldinfo_next == 0 {
break;
}
current =
(current as *mut i8).offset((*current).ldinfo_next as isize) as *mut libc::ld_info;
current = (current as *mut i8).offset((*current).ldinfo_next as isize)
as *mut libc::ld_info;
}
return Err(format!("current dll's address {} is not in the load map", addr));
}
})
.clone()
}
#[cfg(windows)]