Skip to content

use triomphe::Arc for InternedObligationCauseCode instead of Arc #137135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3666,6 +3666,7 @@ dependencies = [
"tempfile",
"thin-vec",
"tracing",
"triomphe",
"windows 0.59.0",
]

Expand Down Expand Up @@ -4183,6 +4184,7 @@ dependencies = [
"smallvec",
"thin-vec",
"tracing",
"triomphe",
]

[[package]]
Expand Down Expand Up @@ -4492,6 +4494,7 @@ dependencies = [
"smallvec",
"tempfile",
"thin-vec",
"triomphe",
]

[[package]]
Expand Down Expand Up @@ -4694,6 +4697,7 @@ dependencies = [
"smallvec",
"thin-vec",
"tracing",
"triomphe",
]

[[package]]
Expand Down Expand Up @@ -5630,6 +5634,15 @@ dependencies = [
"tracing-subscriber",
]

[[package]]
name = "triomphe"
version = "0.1.14"
source = "git+https://github.com/Manishearth/triomphe.git?rev=6ff0527036632be33a6ed45e5e8a0fb6243dee47#6ff0527036632be33a6ed45e5e8a0fb6243dee47"
dependencies = [
"serde",
"stable_deref_trait",
]

[[package]]
name = "twox-hash"
version = "1.6.3"
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ codegen-units = 1
# FIXME: LTO cannot be enabled for binaries in a workspace
# <https://github.com/rust-lang/cargo/issues/9330>
# lto = true

[patch.crates-io]
triomphe = { git = 'https://github.com/Manishearth/triomphe.git', rev = '6ff0527036632be33a6ed45e5e8a0fb6243dee47' }
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ stacker = "0.1.17"
tempfile = "3.2"
thin-vec = "0.2.12"
tracing = "0.1"
triomphe = { version = "0.1.14", features = ["unstable_dropck_eyepatch"] }
# tidy-alphabetical-end

[dependencies.parking_lot]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_data_structures/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl_dyn_send!(
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
[std::sync::mpsc::Sender<T> where T: DynSend]
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
[triomphe::Arc<T> where T: ?Sized + DynSync + DynSend]
[std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
[std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
[std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
Expand Down Expand Up @@ -139,6 +140,7 @@ impl_dyn_sync!(
[std::sync::OnceLock<T> where T: DynSend + DynSync]
[std::sync::Mutex<T> where T: ?Sized + DynSend]
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
[triomphe::Arc<T> where T: ?Sized + DynSync + DynSend]
[std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
[std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
[std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
}
}

impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::triomphe::Arc<T> {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(**self).hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for str {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rustc_type_ir = { path = "../rustc_type_ir" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
tracing = "0.1"
triomphe = "0.1.14"
# tidy-alphabetical-end

[features]
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ mod structural_impls;

use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use rustc_errors::{Applicability, Diag, EmissionGuarantee};
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_hir::def_id::DefId;
use rustc_macros::{
Expand All @@ -23,6 +21,7 @@ use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};
use rustc_span::{DUMMY_SP, Span, Symbol};
use smallvec::{SmallVec, smallvec};
use thin_vec::ThinVec;
use {rustc_hir as hir, triomphe};

pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
use crate::mir::ConstraintCategory;
Expand Down Expand Up @@ -157,7 +156,7 @@ pub struct UnifyReceiverContext<'tcx> {
pub struct InternedObligationCauseCode<'tcx> {
/// `None` for `ObligationCauseCode::Misc` (a common case, occurs ~60% of
/// the time). `Some` otherwise.
code: Option<Arc<ObligationCauseCode<'tcx>>>,
code: Option<triomphe::Arc<ObligationCauseCode<'tcx>>>,
}

impl<'tcx> std::fmt::Debug for InternedObligationCauseCode<'tcx> {
Expand All @@ -171,7 +170,11 @@ impl<'tcx> ObligationCauseCode<'tcx> {
#[inline(always)]
fn into(self) -> InternedObligationCauseCode<'tcx> {
InternedObligationCauseCode {
code: if let ObligationCauseCode::Misc = self { None } else { Some(Arc::new(self)) },
code: if let ObligationCauseCode::Misc = self {
None
} else {
Some(triomphe::Arc::new(self))
},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
indexmap = "2.0.0"
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
triomphe = "0.1.14"
# tidy-alphabetical-end

[dev-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_serialize/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::sync::Arc;

use smallvec::{Array, SmallVec};
use thin_vec::ThinVec;
use triomphe;

/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
Expand Down Expand Up @@ -486,6 +487,18 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
}
}

impl<S: Encoder, T: Encodable<S>> Encodable<S> for triomphe::Arc<T> {
fn encode(&self, s: &mut S) {
(**self).encode(s);
}
}

impl<D: Decoder, T: Decodable<D>> Decodable<D> for triomphe::Arc<T> {
fn decode(d: &mut D) -> triomphe::Arc<T> {
triomphe::Arc::new(Decodable::decode(d))
}
}

impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
fn encode(&self, s: &mut S) {
(**self).encode(s)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rustc_type_ir_macros = { path = "../rustc_type_ir_macros" }
smallvec = { version = "1.8.1", default-features = false }
thin-vec = "0.2.12"
tracing = "0.1"
triomphe = "0.1.14"
# tidy-alphabetical-end

[features]
Expand Down
38 changes: 38 additions & 0 deletions compiler/rustc_type_ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use std::sync::Arc;
use rustc_index::{Idx, IndexVec};
use thin_vec::ThinVec;
use tracing::{debug, instrument};
use triomphe;

use crate::inherent::*;
use crate::visit::{TypeVisitable, TypeVisitableExt as _};
Expand Down Expand Up @@ -310,6 +311,43 @@ impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Arc<T> {
}
}

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for triomphe::Arc<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
// We merely want to replace the contained `T`, if at all possible,
// so that we don't needlessly allocate a new `Arc` or indeed clone
// the contained type.
unsafe {
// First step is to ensure that we have a unique reference to
// the contained type, which `Arc::make_mut` will accomplish (by
// allocating a new `Arc` and cloning the `T` only if required).
// This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
// panicking during `make_mut` does not leak the `T`.
triomphe::Arc::make_mut(&mut self);

// Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
// is `repr(transparent)`.
let ptr = triomphe::Arc::into_raw(self).cast::<mem::ManuallyDrop<T>>();
let mut unique = triomphe::Arc::from_raw(ptr);

// Call to `Arc::make_mut` above guarantees that `unique` is the
// sole reference to the contained value, so we can avoid doing
// a checked `get_mut` here.
let slot = triomphe::Arc::get_mut(&mut unique).unwrap_unchecked();

// Semantically move the contained type out from `unique`, fold
// it, then move the folded value back into `unique`. Should
// folding fail, `ManuallyDrop` ensures that the "moved-out"
// value is not re-dropped.
let owned = mem::ManuallyDrop::take(slot);
let folded = owned.try_fold_with(folder)?;
*slot = mem::ManuallyDrop::new(folded);

// Cast back to `Arc<T>`.
Ok(triomphe::Arc::from_raw(triomphe::Arc::into_raw(unique).cast()))
}
}
}

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
*self = (*self).try_fold_with(folder)?;
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_type_ir/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Arc<T> {
}
}

impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for triomphe::Arc<T> {
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
(**self).visit_with(visitor)
}
}

impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Box<T> {
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
(**self).visit_with(visitor)
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"tracing-log",
"tracing-subscriber",
"tracing-tree",
"triomphe",
"twox-hash",
"type-map",
"typenum",
Expand Down
2 changes: 2 additions & 0 deletions src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const ALLOWED_SOURCES: &[&str] = &[
r#""registry+https://github.com/rust-lang/crates.io-index""#,
// This is `rust_team_data` used by `site` in src/tools/rustc-perf,
r#""git+https://github.com/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
// temporary to test
r#""git+https://github.com/Manishearth/triomphe.git?rev=6ff0527036632be33a6ed45e5e8a0fb6243dee47#6ff0527036632be33a6ed45e5e8a0fb6243dee47""#,
];

/// Checks for external package sources. `root` is the path to the directory that contains the
Expand Down
Loading