Skip to content

Commit d04ec47

Browse files
committed
Auto merge of rust-lang#86143 - bjorn3:revert_revert_merge_crate_disambiguator, r=michaelwoerister
Reland "Merge CrateDisambiguator into StableCrateId" Reverts rust-lang#85891 as this revert of rust-lang#85804 made perf even worse. r? `@Mark-Simulacrum`
2 parents d7901f3 + 56c6a48 commit d04ec47

File tree

67 files changed

+224
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+224
-295
lines changed

Diff for: compiler/rustc_hir/src/definitions.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::stable_hasher::StableHasher;
1313
use rustc_data_structures::unhash::UnhashMap;
1414
use rustc_index::vec::IndexVec;
15-
use rustc_span::crate_disambiguator::CrateDisambiguator;
1615
use rustc_span::hygiene::ExpnId;
1716
use rustc_span::symbol::{kw, sym, Symbol};
1817

@@ -325,7 +324,7 @@ impl Definitions {
325324
}
326325

327326
/// Adds a root definition (no parent) and a few other reserved definitions.
328-
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
327+
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
329328
let key = DefKey {
330329
parent: None,
331330
disambiguated_data: DisambiguatedDefPathData {
@@ -334,7 +333,6 @@ impl Definitions {
334333
},
335334
};
336335

337-
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
338336
let parent_hash = DefPathHash::new(stable_crate_id, 0);
339337
let def_path_hash = key.compute_stable_hash(parent_hash);
340338

Diff for: compiler/rustc_hir/src/tests.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
2-
use rustc_data_structures::fingerprint::Fingerprint;
3-
use rustc_span::crate_disambiguator::CrateDisambiguator;
42
use rustc_span::def_id::{DefPathHash, StableCrateId};
53

64
#[test]
@@ -13,17 +11,16 @@ fn def_path_hash_depends_on_crate_id() {
1311
// the crate by changing the crate disambiguator (e.g. via bumping the
1412
// crate's version number).
1513

16-
let d0 = CrateDisambiguator::from(Fingerprint::new(12, 34));
17-
let d1 = CrateDisambiguator::from(Fingerprint::new(56, 78));
14+
let id0 = StableCrateId::new("foo", false, vec!["1".to_string()]);
15+
let id1 = StableCrateId::new("foo", false, vec!["2".to_string()]);
1816

19-
let h0 = mk_test_hash("foo", d0);
20-
let h1 = mk_test_hash("foo", d1);
17+
let h0 = mk_test_hash(id0);
18+
let h1 = mk_test_hash(id1);
2119

2220
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
2321
assert_ne!(h0.local_hash(), h1.local_hash());
2422

25-
fn mk_test_hash(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> DefPathHash {
26-
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
23+
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
2724
let parent_hash = DefPathHash::new(stable_crate_id, 0);
2825

2926
let key = DefKey {

Diff for: compiler/rustc_incremental/src/persist/fs.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use rustc_data_structures::svh::Svh;
108108
use rustc_data_structures::{base_n, flock};
109109
use rustc_errors::ErrorReported;
110110
use rustc_fs_util::{link_or_copy, LinkOrCopy};
111-
use rustc_session::{CrateDisambiguator, Session};
111+
use rustc_session::{Session, StableCrateId};
112112

113113
use std::fs as std_fs;
114114
use std::io;
@@ -189,7 +189,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
189189
pub fn prepare_session_directory(
190190
sess: &Session,
191191
crate_name: &str,
192-
crate_disambiguator: CrateDisambiguator,
192+
stable_crate_id: StableCrateId,
193193
) -> Result<(), ErrorReported> {
194194
if sess.opts.incremental.is_none() {
195195
return Ok(());
@@ -200,7 +200,7 @@ pub fn prepare_session_directory(
200200
debug!("prepare_session_directory");
201201

202202
// {incr-comp-dir}/{crate-name-and-disambiguator}
203-
let crate_dir = crate_path(sess, crate_name, crate_disambiguator);
203+
let crate_dir = crate_path(sess, crate_name, stable_crate_id);
204204
debug!("crate-dir: {}", crate_dir.display());
205205
create_dir(sess, &crate_dir, "crate")?;
206206

@@ -648,19 +648,12 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
648648
Ok(UNIX_EPOCH + duration)
649649
}
650650

651-
fn crate_path(
652-
sess: &Session,
653-
crate_name: &str,
654-
crate_disambiguator: CrateDisambiguator,
655-
) -> PathBuf {
651+
fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
656652
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
657653

658-
// The full crate disambiguator is really long. 64 bits of it should be
659-
// sufficient.
660-
let crate_disambiguator = crate_disambiguator.to_fingerprint().to_smaller_hash();
661-
let crate_disambiguator = base_n::encode(crate_disambiguator as u128, INT_ENCODE_BASE);
654+
let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);
662655

663-
let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
656+
let crate_name = format!("{}-{}", crate_name, stable_crate_id);
664657
incr_dir.join(crate_name)
665658
}
666659

Diff for: compiler/rustc_interface/src/passes.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, Wor
1111
use rustc_data_structures::temp_dir::MaybeTempDir;
1212
use rustc_errors::{ErrorReported, PResult};
1313
use rustc_expand::base::ExtCtxt;
14-
use rustc_hir::def_id::LOCAL_CRATE;
14+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1515
use rustc_hir::Crate;
1616
use rustc_lint::LintStore;
1717
use rustc_metadata::creader::CStore;
@@ -195,9 +195,13 @@ pub fn register_plugins<'a>(
195195
let crate_types = util::collect_crate_types(sess, &krate.attrs);
196196
sess.init_crate_types(crate_types);
197197

198-
let disambiguator = util::compute_crate_disambiguator(sess);
199-
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
200-
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator)?;
198+
let stable_crate_id = StableCrateId::new(
199+
crate_name,
200+
sess.crate_types().contains(&CrateType::Executable),
201+
sess.opts.cg.metadata.clone(),
202+
);
203+
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
204+
rustc_incremental::prepare_session_directory(sess, &crate_name, stable_crate_id)?;
201205

202206
if sess.opts.incremental.is_some() {
203207
sess.time("incr_comp_garbage_collect_session_directories", || {

Diff for: compiler/rustc_interface/src/util.rs

-36
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
22
use rustc_ast::ptr::P;
33
use rustc_ast::{self as ast, AttrVec, BlockCheckMode};
44
use rustc_codegen_ssa::traits::CodegenBackend;
5-
use rustc_data_structures::fingerprint::Fingerprint;
65
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
76
#[cfg(parallel_compiler)]
87
use rustc_data_structures::jobserver;
9-
use rustc_data_structures::stable_hasher::StableHasher;
108
use rustc_data_structures::sync::Lrc;
119
use rustc_errors::registry::Registry;
1210
use rustc_metadata::dynamic_lib::DynamicLibrary;
@@ -18,7 +16,6 @@ use rustc_session::config::{self, CrateType};
1816
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
1917
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
2018
use rustc_session::parse::CrateConfig;
21-
use rustc_session::CrateDisambiguator;
2219
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
2320
use rustc_span::edition::Edition;
2421
use rustc_span::lev_distance::find_best_match_for_name;
@@ -487,39 +484,6 @@ pub fn get_codegen_sysroot(
487484
}
488485
}
489486

490-
pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
491-
use std::hash::Hasher;
492-
493-
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
494-
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
495-
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
496-
// should still be safe enough to avoid collisions in practice.
497-
let mut hasher = StableHasher::new();
498-
499-
let mut metadata = session.opts.cg.metadata.clone();
500-
// We don't want the crate_disambiguator to dependent on the order
501-
// -C metadata arguments, so sort them:
502-
metadata.sort();
503-
// Every distinct -C metadata value is only incorporated once:
504-
metadata.dedup();
505-
506-
hasher.write(b"metadata");
507-
for s in &metadata {
508-
// Also incorporate the length of a metadata string, so that we generate
509-
// different values for `-Cmetadata=ab -Cmetadata=c` and
510-
// `-Cmetadata=a -Cmetadata=bc`
511-
hasher.write_usize(s.len());
512-
hasher.write(s.as_bytes());
513-
}
514-
515-
// Also incorporate crate type, so that we don't get symbol conflicts when
516-
// linking against a library of the same name, if this is an executable.
517-
let is_exe = session.crate_types().contains(&CrateType::Executable);
518-
hasher.write(if is_exe { b"exe" } else { b"lib" });
519-
520-
CrateDisambiguator::from(hasher.finish::<Fingerprint>())
521-
}
522-
523487
pub(crate) fn check_attr_crate_type(
524488
sess: &Session,
525489
attrs: &[ast::Attribute],

Diff for: compiler/rustc_metadata/src/creader.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_session::config::{self, CrateType, ExternLocation};
2121
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
2222
use rustc_session::output::validate_crate_name;
2323
use rustc_session::search_paths::PathKind;
24-
use rustc_session::{CrateDisambiguator, Session};
24+
use rustc_session::Session;
2525
use rustc_span::edition::Edition;
2626
use rustc_span::symbol::{sym, Symbol};
2727
use rustc_span::{Span, DUMMY_SP};
@@ -222,10 +222,8 @@ impl<'a> CrateLoader<'a> {
222222
metadata_loader: Box<MetadataLoaderDyn>,
223223
local_crate_name: &str,
224224
) -> Self {
225-
let local_crate_stable_id =
226-
StableCrateId::new(local_crate_name, sess.local_crate_disambiguator());
227225
let mut stable_crate_ids = FxHashMap::default();
228-
stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE);
226+
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
229227

230228
CrateLoader {
231229
sess,
@@ -327,17 +325,14 @@ impl<'a> CrateLoader<'a> {
327325

328326
fn verify_no_symbol_conflicts(&self, root: &CrateRoot<'_>) -> Result<(), CrateError> {
329327
// Check for (potential) conflicts with the local crate
330-
if self.local_crate_name == root.name()
331-
&& self.sess.local_crate_disambiguator() == root.disambiguator()
332-
{
328+
if self.sess.local_stable_crate_id() == root.stable_crate_id() {
333329
return Err(CrateError::SymbolConflictsCurrent(root.name()));
334330
}
335331

336332
// Check for conflicts with any crate loaded so far
337333
let mut res = Ok(());
338334
self.cstore.iter_crate_data(|_, other| {
339-
if other.name() == root.name() && // same crate-name
340-
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
335+
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
341336
other.hash() != root.hash()
342337
{
343338
// but different SVH
@@ -411,7 +406,7 @@ impl<'a> CrateLoader<'a> {
411406
None => (&source, &crate_root),
412407
};
413408
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
414-
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator())?)
409+
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
415410
} else {
416411
None
417412
};
@@ -664,7 +659,7 @@ impl<'a> CrateLoader<'a> {
664659
fn dlsym_proc_macros(
665660
&self,
666661
path: &Path,
667-
disambiguator: CrateDisambiguator,
662+
stable_crate_id: StableCrateId,
668663
) -> Result<&'static [ProcMacro], CrateError> {
669664
// Make sure the path contains a / or the linker will search for it.
670665
let path = env::current_dir().unwrap().join(path);
@@ -673,7 +668,7 @@ impl<'a> CrateLoader<'a> {
673668
Err(s) => return Err(CrateError::DlOpen(s)),
674669
};
675670

676-
let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
671+
let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
677672
let decls = unsafe {
678673
let sym = match lib.symbol(&sym) {
679674
Ok(f) => f,

Diff for: compiler/rustc_metadata/src/locator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ use rustc_session::config::{self, CrateType};
226226
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
227227
use rustc_session::search_paths::PathKind;
228228
use rustc_session::utils::CanonicalizedPath;
229-
use rustc_session::{CrateDisambiguator, Session};
229+
use rustc_session::{Session, StableCrateId};
230230
use rustc_span::symbol::{sym, Symbol};
231231
use rustc_span::Span;
232232
use rustc_target::spec::{Target, TargetTriple};
@@ -787,7 +787,7 @@ pub fn find_plugin_registrar(
787787
metadata_loader: &dyn MetadataLoader,
788788
span: Span,
789789
name: Symbol,
790-
) -> (PathBuf, CrateDisambiguator) {
790+
) -> (PathBuf, StableCrateId) {
791791
match find_plugin_registrar_impl(sess, metadata_loader, name) {
792792
Ok(res) => res,
793793
// `core` is always available if we got as far as loading plugins.
@@ -799,7 +799,7 @@ fn find_plugin_registrar_impl<'a>(
799799
sess: &'a Session,
800800
metadata_loader: &dyn MetadataLoader,
801801
name: Symbol,
802-
) -> Result<(PathBuf, CrateDisambiguator), CrateError> {
802+
) -> Result<(PathBuf, StableCrateId), CrateError> {
803803
info!("find plugin registrar `{}`", name);
804804
let mut locator = CrateLocator::new(
805805
sess,
@@ -816,7 +816,7 @@ fn find_plugin_registrar_impl<'a>(
816816

817817
match locator.maybe_load_library_crate()? {
818818
Some(library) => match library.source.dylib {
819-
Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
819+
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
820820
None => Err(CrateError::NonDylibPlugin(name)),
821821
},
822822
None => Err(locator.into_error()),

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,6 @@ impl CrateRoot<'_> {
633633
self.name
634634
}
635635

636-
crate fn disambiguator(&self) -> CrateDisambiguator {
637-
self.disambiguator
638-
}
639-
640636
crate fn hash(&self) -> Svh {
641637
self.hash
642638
}
@@ -1940,8 +1936,8 @@ impl CrateMetadata {
19401936
self.root.name
19411937
}
19421938

1943-
crate fn disambiguator(&self) -> CrateDisambiguator {
1944-
self.root.disambiguator
1939+
crate fn stable_crate_id(&self) -> StableCrateId {
1940+
self.root.stable_crate_id
19451941
}
19461942

19471943
crate fn hash(&self) -> Svh {

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::middle::stability::DeprecationEntry;
1818
use rustc_middle::ty::query::Providers;
1919
use rustc_middle::ty::{self, TyCtxt, Visibility};
2020
use rustc_session::utils::NativeLibKind;
21-
use rustc_session::{CrateDisambiguator, Session};
21+
use rustc_session::{Session, StableCrateId};
2222
use rustc_span::source_map::{Span, Spanned};
2323
use rustc_span::symbol::Symbol;
2424

@@ -185,7 +185,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
185185
}
186186
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
187187
foreign_modules => { cdata.get_foreign_modules(tcx) }
188-
crate_disambiguator => { cdata.root.disambiguator }
189188
crate_hash => { cdata.root.hash }
190189
crate_host_hash => { cdata.host_hash }
191190
crate_name => { cdata.root.name }
@@ -489,8 +488,8 @@ impl CrateStore for CStore {
489488
self.get_crate_data(cnum).root.name
490489
}
491490

492-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
493-
self.get_crate_data(cnum).root.disambiguator
491+
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
492+
self.get_crate_data(cnum).root.stable_crate_id
494493
}
495494

496495
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
671671
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
672672
triple: tcx.sess.opts.target_triple.clone(),
673673
hash: tcx.crate_hash(LOCAL_CRATE),
674-
disambiguator: tcx.sess.local_crate_disambiguator(),
675674
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
676675
panic_strategy: tcx.sess.panic_strategy(),
677676
edition: tcx.sess.edition(),

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_middle::mir;
1818
use rustc_middle::ty::{self, ReprOptions, Ty};
1919
use rustc_serialize::opaque::Encoder;
2020
use rustc_session::config::SymbolManglingVersion;
21-
use rustc_session::CrateDisambiguator;
2221
use rustc_span::edition::Edition;
2322
use rustc_span::hygiene::MacroKind;
2423
use rustc_span::symbol::{Ident, Symbol};
@@ -202,7 +201,6 @@ crate struct CrateRoot<'tcx> {
202201
triple: TargetTriple,
203202
extra_filename: String,
204203
hash: Svh,
205-
disambiguator: CrateDisambiguator,
206204
stable_crate_id: StableCrateId,
207205
panic_strategy: PanicStrategy,
208206
edition: Edition,

0 commit comments

Comments
 (0)