Skip to content

Commit

Permalink
Optimize Build::print: Avoid unnecessary heap alloc (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu authored Jul 20, 2023
1 parent b030a29 commit 803cf9c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,25 +1132,31 @@ impl Build {
});

if let Some(atlmfc_lib) = atlmfc_lib {
self.print(&format!(
self.print(&format_args!(
"cargo:rustc-link-search=native={}",
atlmfc_lib.display()
));
}
}

if self.link_lib_modifiers.is_empty() {
self.print(&format!("cargo:rustc-link-lib=static={}", lib_name));
self.print(&format_args!("cargo:rustc-link-lib=static={}", lib_name));
} else {
let m = self.link_lib_modifiers.join(",");
self.print(&format!("cargo:rustc-link-lib=static:{}={}", m, lib_name));
self.print(&format_args!(
"cargo:rustc-link-lib=static:{}={}",
m, lib_name
));
}
self.print(&format!("cargo:rustc-link-search=native={}", dst.display()));
self.print(&format_args!(
"cargo:rustc-link-search=native={}",
dst.display()
));

// Add specific C++ libraries, if enabled.
if self.cpp {
if let Some(stdlib) = self.get_cpp_link_stdlib()? {
self.print(&format!("cargo:rustc-link-lib={}", stdlib));
self.print(&format_args!("cargo:rustc-link-lib={}", stdlib));
}
}

Expand Down Expand Up @@ -2328,7 +2334,7 @@ impl Build {
ArchSpec::Catalyst(_) => "macosx".to_owned(),
};

self.print(&format!("Detecting {} SDK path for {}", os, sdk));
self.print(&format_args!("Detecting {} SDK path for {}", os, sdk));
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
sdkroot
} else {
Expand Down Expand Up @@ -3128,10 +3134,10 @@ impl Build {
return val.clone();
}
if self.emit_rerun_if_env_changed && !provided_by_cargo(v) {
self.print(&format!("cargo:rerun-if-env-changed={}", v));
self.print(&format_args!("cargo:rerun-if-env-changed={}", v));
}
let r = env::var(v).ok();
self.print(&format!("{} = {:?}", v, r));
self.print(&format_args!("{} = {:?}", v, r));
cache.insert(v.to_string(), r.clone());
r
}
Expand Down Expand Up @@ -3174,7 +3180,7 @@ impl Build {
.collect())
}

fn print(&self, s: &str) {
fn print(&self, s: &dyn Display) {
if self.cargo_metadata {
println!("{}", s);
}
Expand Down

0 comments on commit 803cf9c

Please sign in to comment.