Skip to content

Commit

Permalink
Auto merge of rust-lang#86143 - bjorn3:revert_revert_merge_crate_disa…
Browse files Browse the repository at this point in the history
…mbiguator, 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`
  • Loading branch information
bors committed Jul 6, 2021
2 parents d7901f3 + 56c6a48 commit d04ec47
Show file tree
Hide file tree
Showing 67 changed files with 224 additions and 295 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::unhash::UnhashMap;
use rustc_index::vec::IndexVec;
use rustc_span::crate_disambiguator::CrateDisambiguator;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym, Symbol};

Expand Down Expand Up @@ -325,7 +324,7 @@ impl Definitions {
}

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

let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
let parent_hash = DefPathHash::new(stable_crate_id, 0);
let def_path_hash = key.compute_stable_hash(parent_hash);

Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_span::crate_disambiguator::CrateDisambiguator;
use rustc_span::def_id::{DefPathHash, StableCrateId};

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

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

let h0 = mk_test_hash("foo", d0);
let h1 = mk_test_hash("foo", d1);
let h0 = mk_test_hash(id0);
let h1 = mk_test_hash(id1);

assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
assert_ne!(h0.local_hash(), h1.local_hash());

fn mk_test_hash(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> DefPathHash {
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
let parent_hash = DefPathHash::new(stable_crate_id, 0);

let key = DefKey {
Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use rustc_data_structures::svh::Svh;
use rustc_data_structures::{base_n, flock};
use rustc_errors::ErrorReported;
use rustc_fs_util::{link_or_copy, LinkOrCopy};
use rustc_session::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};

use std::fs as std_fs;
use std::io;
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
pub fn prepare_session_directory(
sess: &Session,
crate_name: &str,
crate_disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
) -> Result<(), ErrorReported> {
if sess.opts.incremental.is_none() {
return Ok(());
Expand All @@ -200,7 +200,7 @@ pub fn prepare_session_directory(
debug!("prepare_session_directory");

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

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

fn crate_path(
sess: &Session,
crate_name: &str,
crate_disambiguator: CrateDisambiguator,
) -> PathBuf {
fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();

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

let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
let crate_name = format!("{}-{}", crate_name, stable_crate_id);
incr_dir.join(crate_name)
}

Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, Wor
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{ErrorReported, PResult};
use rustc_expand::base::ExtCtxt;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::Crate;
use rustc_lint::LintStore;
use rustc_metadata::creader::CStore;
Expand Down Expand Up @@ -195,9 +195,13 @@ pub fn register_plugins<'a>(
let crate_types = util::collect_crate_types(sess, &krate.attrs);
sess.init_crate_types(crate_types);

let disambiguator = util::compute_crate_disambiguator(sess);
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator)?;
let stable_crate_id = StableCrateId::new(
crate_name,
sess.crate_types().contains(&CrateType::Executable),
sess.opts.cg.metadata.clone(),
);
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
rustc_incremental::prepare_session_directory(sess, &crate_name, stable_crate_id)?;

if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {
Expand Down
36 changes: 0 additions & 36 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, AttrVec, BlockCheckMode};
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
#[cfg(parallel_compiler)]
use rustc_data_structures::jobserver;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry;
use rustc_metadata::dynamic_lib::DynamicLibrary;
Expand All @@ -18,7 +16,6 @@ use rustc_session::config::{self, CrateType};
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
use rustc_session::parse::CrateConfig;
use rustc_session::CrateDisambiguator;
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
use rustc_span::edition::Edition;
use rustc_span::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -487,39 +484,6 @@ pub fn get_codegen_sysroot(
}
}

pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
use std::hash::Hasher;

// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
// should still be safe enough to avoid collisions in practice.
let mut hasher = StableHasher::new();

let mut metadata = session.opts.cg.metadata.clone();
// We don't want the crate_disambiguator to dependent on the order
// -C metadata arguments, so sort them:
metadata.sort();
// Every distinct -C metadata value is only incorporated once:
metadata.dedup();

hasher.write(b"metadata");
for s in &metadata {
// Also incorporate the length of a metadata string, so that we generate
// different values for `-Cmetadata=ab -Cmetadata=c` and
// `-Cmetadata=a -Cmetadata=bc`
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}

// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
let is_exe = session.crate_types().contains(&CrateType::Executable);
hasher.write(if is_exe { b"exe" } else { b"lib" });

CrateDisambiguator::from(hasher.finish::<Fingerprint>())
}

pub(crate) fn check_attr_crate_type(
sess: &Session,
attrs: &[ast::Attribute],
Expand Down
19 changes: 7 additions & 12 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_session::{CrateDisambiguator, Session};
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -222,10 +222,8 @@ impl<'a> CrateLoader<'a> {
metadata_loader: Box<MetadataLoaderDyn>,
local_crate_name: &str,
) -> Self {
let local_crate_stable_id =
StableCrateId::new(local_crate_name, sess.local_crate_disambiguator());
let mut stable_crate_ids = FxHashMap::default();
stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE);
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);

CrateLoader {
sess,
Expand Down Expand Up @@ -327,17 +325,14 @@ impl<'a> CrateLoader<'a> {

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

// Check for conflicts with any crate loaded so far
let mut res = Ok(());
self.cstore.iter_crate_data(|_, other| {
if other.name() == root.name() && // same crate-name
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
other.hash() != root.hash()
{
// but different SVH
Expand Down Expand Up @@ -411,7 +406,7 @@ impl<'a> CrateLoader<'a> {
None => (&source, &crate_root),
};
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator())?)
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
} else {
None
};
Expand Down Expand Up @@ -664,7 +659,7 @@ impl<'a> CrateLoader<'a> {
fn dlsym_proc_macros(
&self,
path: &Path,
disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
) -> Result<&'static [ProcMacro], CrateError> {
// Make sure the path contains a / or the linker will search for it.
let path = env::current_dir().unwrap().join(path);
Expand All @@ -673,7 +668,7 @@ impl<'a> CrateLoader<'a> {
Err(s) => return Err(CrateError::DlOpen(s)),
};

let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
let decls = unsafe {
let sym = match lib.symbol(&sym) {
Ok(f) => f,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ use rustc_session::config::{self, CrateType};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
use rustc_session::search_paths::PathKind;
use rustc_session::utils::CanonicalizedPath;
use rustc_session::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use rustc_target::spec::{Target, TargetTriple};
Expand Down Expand Up @@ -787,7 +787,7 @@ pub fn find_plugin_registrar(
metadata_loader: &dyn MetadataLoader,
span: Span,
name: Symbol,
) -> (PathBuf, CrateDisambiguator) {
) -> (PathBuf, StableCrateId) {
match find_plugin_registrar_impl(sess, metadata_loader, name) {
Ok(res) => res,
// `core` is always available if we got as far as loading plugins.
Expand All @@ -799,7 +799,7 @@ fn find_plugin_registrar_impl<'a>(
sess: &'a Session,
metadata_loader: &dyn MetadataLoader,
name: Symbol,
) -> Result<(PathBuf, CrateDisambiguator), CrateError> {
) -> Result<(PathBuf, StableCrateId), CrateError> {
info!("find plugin registrar `{}`", name);
let mut locator = CrateLocator::new(
sess,
Expand All @@ -816,7 +816,7 @@ fn find_plugin_registrar_impl<'a>(

match locator.maybe_load_library_crate()? {
Some(library) => match library.source.dylib {
Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
None => Err(CrateError::NonDylibPlugin(name)),
},
None => Err(locator.into_error()),
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,6 @@ impl CrateRoot<'_> {
self.name
}

crate fn disambiguator(&self) -> CrateDisambiguator {
self.disambiguator
}

crate fn hash(&self) -> Svh {
self.hash
}
Expand Down Expand Up @@ -1940,8 +1936,8 @@ impl CrateMetadata {
self.root.name
}

crate fn disambiguator(&self) -> CrateDisambiguator {
self.root.disambiguator
crate fn stable_crate_id(&self) -> StableCrateId {
self.root.stable_crate_id
}

crate fn hash(&self) -> Svh {
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, Visibility};
use rustc_session::utils::NativeLibKind;
use rustc_session::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};
use rustc_span::source_map::{Span, Spanned};
use rustc_span::symbol::Symbol;

Expand Down Expand Up @@ -185,7 +185,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
}
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
foreign_modules => { cdata.get_foreign_modules(tcx) }
crate_disambiguator => { cdata.root.disambiguator }
crate_hash => { cdata.root.hash }
crate_host_hash => { cdata.host_hash }
crate_name => { cdata.root.name }
Expand Down Expand Up @@ -489,8 +488,8 @@ impl CrateStore for CStore {
self.get_crate_data(cnum).root.name
}

fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
self.get_crate_data(cnum).root.disambiguator
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
self.get_crate_data(cnum).root.stable_crate_id
}

fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
triple: tcx.sess.opts.target_triple.clone(),
hash: tcx.crate_hash(LOCAL_CRATE),
disambiguator: tcx.sess.local_crate_disambiguator(),
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
panic_strategy: tcx.sess.panic_strategy(),
edition: tcx.sess.edition(),
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use rustc_middle::mir;
use rustc_middle::ty::{self, ReprOptions, Ty};
use rustc_serialize::opaque::Encoder;
use rustc_session::config::SymbolManglingVersion;
use rustc_session::CrateDisambiguator;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{Ident, Symbol};
Expand Down Expand Up @@ -202,7 +201,6 @@ crate struct CrateRoot<'tcx> {
triple: TargetTriple,
extra_filename: String,
hash: Svh,
disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
panic_strategy: PanicStrategy,
edition: Edition,
Expand Down
Loading

0 comments on commit d04ec47

Please sign in to comment.