Skip to content

Commit f71826e

Browse files
committed
Auto merge of #64303 - nnethercote:avoid-more-Symbol-to-string-operations, r=petrochenkov
Avoid more `Symbol`-to-string operations These commits avoid various `Symbol`-to-string conversions, by doing more operations directly on `Symbol`s. This requires adding a few more static `Symbol`s to the binary. r? @petrochenkov
2 parents c9edc02 + 8138efa commit f71826e

File tree

13 files changed

+93
-81
lines changed

13 files changed

+93
-81
lines changed

Diff for: src/librustc/middle/weak_lang_items.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>,
116116
}
117117

118118
impl<'a, 'tcx> Context<'a, 'tcx> {
119-
fn register(&mut self, name: &str, span: Span) {
120-
$(if name == stringify!($name) {
119+
fn register(&mut self, name: Symbol, span: Span) {
120+
$(if name == sym::$name {
121121
if self.items.$name().is_none() {
122122
self.items.missing.push(lang_items::$item);
123123
}
@@ -136,7 +136,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
136136

137137
fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
138138
if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
139-
self.register(&lang_item.as_str(), i.span);
139+
self.register(lang_item, i.span);
140140
}
141141
intravisit::walk_foreign_item(self, i)
142142
}

Diff for: src/librustc_codegen_llvm/back/archive.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::llvm::{self, ArchiveKind};
1212
use rustc_codegen_ssa::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION};
1313
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, find_library};
1414
use rustc::session::Session;
15+
use syntax::symbol::Symbol;
1516

1617
struct ArchiveConfig<'a> {
1718
pub sess: &'a Session,
@@ -109,7 +110,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
109110

110111
/// Adds all of the contents of a native library to this archive. This will
111112
/// search in the relevant locations for a library named `name`.
112-
fn add_native_library(&mut self, name: &str) {
113+
fn add_native_library(&mut self, name: Symbol) {
113114
let location = find_library(name, &self.config.lib_search_paths,
114115
self.config.sess);
115116
self.add_archive(&location, |_| false).unwrap_or_else(|e| {

Diff for: src/librustc_codegen_ssa/back/archive.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc::session::Session;
2+
use syntax::symbol::Symbol;
23

34
use std::io;
45
use std::path::{Path, PathBuf};
56

6-
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
7+
pub fn find_library(name: Symbol, search_paths: &[PathBuf], sess: &Session)
78
-> PathBuf {
89
// On Windows, static libraries sometimes show up as libfoo.a and other
910
// times show up as foo.lib
@@ -40,7 +41,7 @@ pub trait ArchiveBuilder<'a> {
4041
lto: bool,
4142
skip_objects: bool,
4243
) -> io::Result<()>;
43-
fn add_native_library(&mut self, name: &str);
44+
fn add_native_library(&mut self, name: Symbol);
4445
fn update_symbols(&mut self);
4546

4647
fn build(self);

Diff for: src/librustc_codegen_ssa/back/command.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::mem;
88
use std::process::{self, Output};
99

1010
use rustc_target::spec::LldFlavor;
11+
use syntax::symbol::Symbol;
1112

1213
#[derive(Clone)]
1314
pub struct Command {
@@ -49,6 +50,11 @@ impl Command {
4950
self
5051
}
5152

53+
pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
54+
self.arg(&arg.as_str());
55+
self
56+
}
57+
5258
pub fn args<I>(&mut self, args: I) -> &mut Command
5359
where
5460
I: IntoIterator<Item: AsRef<OsStr>>,

Diff for: src/librustc_codegen_ssa/back/link.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc::hir::def_id::CrateNum;
1313
use rustc_data_structures::fx::FxHashSet;
1414
use rustc_fs_util::fix_windows_verbatim_for_gcc;
1515
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
16+
use syntax::symbol::Symbol;
1617

1718
use crate::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION, CrateInfo, CodegenResults};
1819
use super::archive::ArchiveBuilder;
@@ -316,7 +317,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
316317
NativeLibraryKind::NativeUnknown => continue,
317318
}
318319
if let Some(name) = lib.name {
319-
ab.add_native_library(&name.as_str());
320+
ab.add_native_library(name);
320321
}
321322
}
322323

@@ -1273,15 +1274,14 @@ pub fn add_local_native_libraries(cmd: &mut dyn Linker,
12731274
let search_path = archive_search_paths(sess);
12741275
for lib in relevant_libs {
12751276
let name = match lib.name {
1276-
Some(ref l) => l,
1277+
Some(l) => l,
12771278
None => continue,
12781279
};
12791280
match lib.kind {
1280-
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
1281-
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
1282-
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&name.as_str()),
1283-
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(&name.as_str(),
1284-
&search_path)
1281+
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
1282+
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
1283+
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(name),
1284+
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path)
12851285
}
12861286
}
12871287
}
@@ -1594,7 +1594,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
15941594
cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
15951595
}
15961596
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
1597-
cmd.link_rust_dylib(&unlib(&sess.target, filestem),
1597+
cmd.link_rust_dylib(Symbol::intern(&unlib(&sess.target, filestem)),
15981598
parent.unwrap_or(Path::new("")));
15991599
}
16001600
}
@@ -1637,22 +1637,22 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
16371637
for &(cnum, _) in crates {
16381638
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
16391639
let name = match lib.name {
1640-
Some(ref l) => l,
1640+
Some(l) => l,
16411641
None => continue,
16421642
};
16431643
if !relevant_lib(sess, &lib) {
16441644
continue
16451645
}
16461646
match lib.kind {
1647-
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
1648-
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
1647+
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
1648+
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
16491649
NativeLibraryKind::NativeStaticNobundle => {
16501650
// Link "static-nobundle" native libs only if the crate they originate from
16511651
// is being linked statically to the current crate. If it's linked dynamically
16521652
// or is an rlib already included via some other dylib crate, the symbols from
16531653
// native libs will have already been included in that dylib.
16541654
if data[cnum.as_usize() - 1] == Linkage::Static {
1655-
cmd.link_staticlib(&name.as_str())
1655+
cmd.link_staticlib(name)
16561656
}
16571657
},
16581658
// ignore statically included native libraries here as we've

Diff for: src/librustc_codegen_ssa/back/linker.rs

+42-37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
1717
use rustc::ty::TyCtxt;
1818
use rustc_target::spec::{LinkerFlavor, LldFlavor};
1919
use rustc_serialize::{json, Encoder};
20+
use syntax::symbol::Symbol;
2021

2122
/// For all the linkers we support, and information they might
2223
/// need out of the shared crate context before we get rid of it.
@@ -99,13 +100,13 @@ impl LinkerInfo {
99100
/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
100101
/// MSVC linker (e.g., `link.exe`) is being used.
101102
pub trait Linker {
102-
fn link_dylib(&mut self, lib: &str);
103-
fn link_rust_dylib(&mut self, lib: &str, path: &Path);
104-
fn link_framework(&mut self, framework: &str);
105-
fn link_staticlib(&mut self, lib: &str);
103+
fn link_dylib(&mut self, lib: Symbol);
104+
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path);
105+
fn link_framework(&mut self, framework: Symbol);
106+
fn link_staticlib(&mut self, lib: Symbol);
106107
fn link_rlib(&mut self, lib: &Path);
107108
fn link_whole_rlib(&mut self, lib: &Path);
108-
fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
109+
fn link_whole_staticlib(&mut self, lib: Symbol, search_path: &[PathBuf]);
109110
fn include_path(&mut self, path: &Path);
110111
fn framework_path(&mut self, path: &Path);
111112
fn output_filename(&mut self, path: &Path);
@@ -215,9 +216,13 @@ impl<'a> GccLinker<'a> {
215216
}
216217

217218
impl<'a> Linker for GccLinker<'a> {
218-
fn link_dylib(&mut self, lib: &str) { self.hint_dynamic(); self.cmd.arg(format!("-l{}", lib)); }
219-
fn link_staticlib(&mut self, lib: &str) {
220-
self.hint_static(); self.cmd.arg(format!("-l{}", lib));
219+
fn link_dylib(&mut self, lib: Symbol) {
220+
self.hint_dynamic();
221+
self.cmd.arg(format!("-l{}", lib));
222+
}
223+
fn link_staticlib(&mut self, lib: Symbol) {
224+
self.hint_static();
225+
self.cmd.arg(format!("-l{}", lib));
221226
}
222227
fn link_rlib(&mut self, lib: &Path) { self.hint_static(); self.cmd.arg(lib); }
223228
fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
@@ -232,14 +237,14 @@ impl<'a> Linker for GccLinker<'a> {
232237
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
233238
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
234239

235-
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
240+
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
236241
self.hint_dynamic();
237242
self.cmd.arg(format!("-l{}", lib));
238243
}
239244

240-
fn link_framework(&mut self, framework: &str) {
245+
fn link_framework(&mut self, framework: Symbol) {
241246
self.hint_dynamic();
242-
self.cmd.arg("-framework").arg(framework);
247+
self.cmd.arg("-framework").sym_arg(framework);
243248
}
244249

245250
// Here we explicitly ask that the entire archive is included into the
@@ -248,7 +253,7 @@ impl<'a> Linker for GccLinker<'a> {
248253
// don't otherwise explicitly reference them. This can occur for
249254
// libraries which are just providing bindings, libraries with generic
250255
// functions, etc.
251-
fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
256+
fn link_whole_staticlib(&mut self, lib: Symbol, search_path: &[PathBuf]) {
252257
self.hint_static();
253258
let target = &self.sess.target.target;
254259
if !target.options.is_like_osx {
@@ -539,11 +544,11 @@ impl<'a> Linker for MsvcLinker<'a> {
539544
}
540545
}
541546

542-
fn link_dylib(&mut self, lib: &str) {
547+
fn link_dylib(&mut self, lib: Symbol) {
543548
self.cmd.arg(&format!("{}.lib", lib));
544549
}
545550

546-
fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
551+
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path) {
547552
// When producing a dll, the MSVC linker may not actually emit a
548553
// `foo.lib` file if the dll doesn't actually export any symbols, so we
549554
// check to see if the file is there and just omit linking to it if it's
@@ -554,7 +559,7 @@ impl<'a> Linker for MsvcLinker<'a> {
554559
}
555560
}
556561

557-
fn link_staticlib(&mut self, lib: &str) {
562+
fn link_staticlib(&mut self, lib: Symbol) {
558563
self.cmd.arg(&format!("{}.lib", lib));
559564
}
560565

@@ -605,11 +610,11 @@ impl<'a> Linker for MsvcLinker<'a> {
605610
fn framework_path(&mut self, _path: &Path) {
606611
bug!("frameworks are not supported on windows")
607612
}
608-
fn link_framework(&mut self, _framework: &str) {
613+
fn link_framework(&mut self, _framework: Symbol) {
609614
bug!("frameworks are not supported on windows")
610615
}
611616

612-
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
617+
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
613618
// not supported?
614619
self.link_staticlib(lib);
615620
}
@@ -740,8 +745,8 @@ impl<'a> Linker for EmLinker<'a> {
740745
self.cmd.arg("-L").arg(path);
741746
}
742747

743-
fn link_staticlib(&mut self, lib: &str) {
744-
self.cmd.arg("-l").arg(lib);
748+
fn link_staticlib(&mut self, lib: Symbol) {
749+
self.cmd.arg("-l").sym_arg(lib);
745750
}
746751

747752
fn output_filename(&mut self, path: &Path) {
@@ -752,12 +757,12 @@ impl<'a> Linker for EmLinker<'a> {
752757
self.cmd.arg(path);
753758
}
754759

755-
fn link_dylib(&mut self, lib: &str) {
760+
fn link_dylib(&mut self, lib: Symbol) {
756761
// Emscripten always links statically
757762
self.link_staticlib(lib);
758763
}
759764

760-
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
765+
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
761766
// not supported?
762767
self.link_staticlib(lib);
763768
}
@@ -767,7 +772,7 @@ impl<'a> Linker for EmLinker<'a> {
767772
self.link_rlib(lib);
768773
}
769774

770-
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
775+
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
771776
self.link_dylib(lib);
772777
}
773778

@@ -803,7 +808,7 @@ impl<'a> Linker for EmLinker<'a> {
803808
bug!("frameworks are not supported on Emscripten")
804809
}
805810

806-
fn link_framework(&mut self, _framework: &str) {
811+
fn link_framework(&mut self, _framework: Symbol) {
807812
bug!("frameworks are not supported on Emscripten")
808813
}
809814

@@ -948,12 +953,12 @@ impl<'a> WasmLd<'a> {
948953
}
949954

950955
impl<'a> Linker for WasmLd<'a> {
951-
fn link_dylib(&mut self, lib: &str) {
952-
self.cmd.arg("-l").arg(lib);
956+
fn link_dylib(&mut self, lib: Symbol) {
957+
self.cmd.arg("-l").sym_arg(lib);
953958
}
954959

955-
fn link_staticlib(&mut self, lib: &str) {
956-
self.cmd.arg("-l").arg(lib);
960+
fn link_staticlib(&mut self, lib: Symbol) {
961+
self.cmd.arg("-l").sym_arg(lib);
957962
}
958963

959964
fn link_rlib(&mut self, lib: &Path) {
@@ -995,16 +1000,16 @@ impl<'a> Linker for WasmLd<'a> {
9951000
self.cmd.args(args);
9961001
}
9971002

998-
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
999-
self.cmd.arg("-l").arg(lib);
1003+
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
1004+
self.cmd.arg("-l").sym_arg(lib);
10001005
}
10011006

1002-
fn link_framework(&mut self, _framework: &str) {
1007+
fn link_framework(&mut self, _framework: Symbol) {
10031008
panic!("frameworks not supported")
10041009
}
10051010

1006-
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
1007-
self.cmd.arg("-l").arg(lib);
1011+
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
1012+
self.cmd.arg("-l").sym_arg(lib);
10081013
}
10091014

10101015
fn link_whole_rlib(&mut self, lib: &Path) {
@@ -1162,27 +1167,27 @@ impl<'a> Linker for PtxLinker<'a> {
11621167
::std::mem::replace(&mut self.cmd, Command::new(""))
11631168
}
11641169

1165-
fn link_dylib(&mut self, _lib: &str) {
1170+
fn link_dylib(&mut self, _lib: Symbol) {
11661171
panic!("external dylibs not supported")
11671172
}
11681173

1169-
fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) {
1174+
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
11701175
panic!("external dylibs not supported")
11711176
}
11721177

1173-
fn link_staticlib(&mut self, _lib: &str) {
1178+
fn link_staticlib(&mut self, _lib: Symbol) {
11741179
panic!("staticlibs not supported")
11751180
}
11761181

1177-
fn link_whole_staticlib(&mut self, _lib: &str, _search_path: &[PathBuf]) {
1182+
fn link_whole_staticlib(&mut self, _lib: Symbol, _search_path: &[PathBuf]) {
11781183
panic!("staticlibs not supported")
11791184
}
11801185

11811186
fn framework_path(&mut self, _path: &Path) {
11821187
panic!("frameworks not supported")
11831188
}
11841189

1185-
fn link_framework(&mut self, _framework: &str) {
1190+
fn link_framework(&mut self, _framework: Symbol) {
11861191
panic!("frameworks not supported")
11871192
}
11881193

0 commit comments

Comments
 (0)