Unverified Commit 2817a46d authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Split DynamicEqHash trait into smaller traits in a separate re-usable crate (#81741)

I originally split this off to support #81742, but I ended up rewriting that PR to not depend on this...

However, I think this is still a code quality improvement, so I'm publishing the PR anyways.

A few notable changes:

- This is now three traits (`DynPartialEq`, `DynEq`, and `DynHash`) instead of one (`DynamicEqHash`)
- The `Dyn` naming instead of `Dynamic` matches the more common Rust convention.
- [Trait upcasting is finally stable!](https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/#trait-upcasting) Remove the `.as_any()` idiom. Be very careful with the explicit derefs.
- Fix a bug where the type id was getting added to the hash state twice (once by the `Hash` impl, and a second time by the `DynHash` impl).
- The `HasherMut` struct isn't really needed, as you can use a `&mut &mut Hasher` instead to work around these dyn compatibility issues in the stdlib.

And finally, `MagicAny` is ported to use these `Dyn` traits to dedupe some logic.
parent 7d030a02
......@@ -9107,6 +9107,10 @@ dependencies = [
"utf-8",
]
[[package]]
name = "turbo-dyn-eq-hash"
version = "0.0.1"
[[package]]
name = "turbo-esregex"
version = "0.1.0"
......@@ -9224,6 +9228,7 @@ dependencies = [
"tokio-util",
"tracing",
"triomphe 0.1.12",
"turbo-dyn-eq-hash",
"turbo-rcstr",
"turbo-tasks-build",
"turbo-tasks-hash",
......
......@@ -257,6 +257,7 @@ next-custom-transforms = { path = "crates/next-custom-transforms" }
auto-hash-map = { path = "turbopack/crates/turbo-tasks-auto-hash-map" }
turbo-prehash = { path = "turbopack/crates/turbo-prehash" }
turbo-rcstr = { path = "turbopack/crates/turbo-rcstr" }
turbo-dyn-eq-hash = { path = "turbopack/crates/turbo-dyn-eq-hash" }
turbo-esregex = { path = "turbopack/crates/turbo-esregex" }
turbo-persistence = { path = "turbopack/crates/turbo-persistence" }
turbo-tasks-malloc = { path = "turbopack/crates/turbo-tasks-malloc", default-features = false }
......
[package]
name = "turbo-dyn-eq-hash"
version = "0.0.1"
description = "TBD"
license = "MIT"
edition = "2024"
[lib]
bench = false
[lints]
workspace = true
[dependencies]
use std::{
any::{Any, TypeId},
hash::{Hash, Hasher},
};
/// A dynamic-dispatch version of the [`PartialEq`] trait. Implemented for every type that
/// implements [`Any`] + [`PartialEq`].
pub trait DynPartialEq: Any {
fn dyn_partial_eq(&self, other: &dyn Any) -> bool;
}
impl<T: Any + PartialEq> DynPartialEq for T {
fn dyn_partial_eq(&self, other: &dyn Any) -> bool {
other
.downcast_ref::<Self>()
.map(|other| PartialEq::eq(self, other))
.unwrap_or(false)
}
}
#[macro_export]
macro_rules! impl_partial_eq_for_dyn {
($ty:ty) => {
impl ::std::cmp::PartialEq for $ty {
fn eq(&self, other: &Self) -> bool {
$crate::DynPartialEq::dyn_partial_eq(self, other as &dyn ::std::any::Any)
}
}
};
}
/// A dynamic-dispatch version of the [`Eq`] trait. Implemented for every type that implements
/// [`Any`] + [`Eq`].
pub trait DynEq: DynPartialEq {}
impl<T: Any + Eq> DynEq for T {}
#[macro_export]
macro_rules! impl_eq_for_dyn {
($ty:ty) => {
impl ::std::cmp::Eq for $ty {}
};
}
/// A dynamic-dispatch version of the [`Hash`] trait. Implemented for every type that implements
/// [`Any`] + [`Hash`].
///
/// The `TypeId::of::<Self>()` value is included in the resulting hash, so the hash generated by
/// `DynEqHash` will not match the hash generated by the underlying `Hash` implementation. In other
/// words, the hash of `T` will not match the hash of `T as dyn SomeDynHashTrait`.
pub trait DynHash {
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl<T: Any + Hash> DynHash for T {
fn dyn_hash(&self, mut state: &mut dyn Hasher) {
Hash::hash(&(TypeId::of::<Self>(), self), &mut state);
}
}
#[macro_export]
macro_rules! impl_hash_for_dyn {
($ty:ty) => {
impl ::std::hash::Hash for $ty {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
$crate::DynHash::dyn_hash(self, state as &mut dyn (::std::hash::Hasher))
}
}
};
}
use std::{
any::Any,
fmt::{Display, Formatter},
hash::Hash,
};
......@@ -36,12 +37,12 @@ impl InvalidationReasonKind for WatchChangeKind {
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
write!(
f,
"{} files changed ({}, ...)",
reasons.len(),
reasons[0]
.as_any()
(first_reason as &dyn Any)
.downcast_ref::<WatchChange>()
.unwrap()
.path
......@@ -81,7 +82,10 @@ impl InvalidationReasonKind for WatchStartKind {
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let example = reasons[0].as_any().downcast_ref::<WatchStart>().unwrap();
let first_reason: &dyn InvalidationReason = &*reasons[0];
let example = (first_reason as &dyn Any)
.downcast_ref::<WatchStart>()
.unwrap();
write!(
f,
"{} items started watching (e.g. {} in {})",
......@@ -126,7 +130,10 @@ impl InvalidationReasonKind for InitializeKind {
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let example = reasons[0].as_any().downcast_ref::<Initialize>().unwrap();
let first_reason: &dyn InvalidationReason = &*reasons[0];
let example = (first_reason as &dyn Any)
.downcast_ref::<Initialize>()
.unwrap();
write!(
f,
"{} items invalidated as part of project or filesystem initialization ({}, ...)",
......@@ -166,11 +173,15 @@ impl InvalidationReasonKind for WriteKind {
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
write!(
f,
"{} files written ({}, ...)",
reasons.len(),
reasons[0].as_any().downcast_ref::<Write>().unwrap().path
(first_reason as &dyn Any)
.downcast_ref::<Write>()
.unwrap()
.path
)
}
}
use std::{
any::Any,
fmt,
mem::take,
path::{Path, PathBuf},
......@@ -616,12 +617,12 @@ impl InvalidationReasonKind for InvalidateRescanKind {
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
write!(
f,
"{} items in filesystem invalidated due to notify::Watcher rescan event ({}, ...)",
reasons.len(),
reasons[0]
.as_any()
(first_reason as &dyn Any)
.downcast_ref::<InvalidateRescan>()
.unwrap()
.path
......
......@@ -48,6 +48,7 @@ tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true }
tracing = { workspace = true }
triomphe = { workspace = true, features = ["unsize", "unstable"] }
turbo-dyn-eq-hash = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbo-tasks-macros = { workspace = true }
......
use std::{
any::{Any, TypeId},
fmt::Display,
hash::{Hash, Hasher},
hash::Hash,
mem::replace,
sync::{Arc, Weak},
};
......@@ -10,10 +9,12 @@ use anyhow::Result;
use indexmap::map::Entry;
use serde::{Deserialize, Serialize, de::Visitor};
use tokio::runtime::Handle;
use turbo_dyn_eq_hash::{
DynEq, DynHash, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
};
use crate::{
FxIndexMap, FxIndexSet, TaskId, TurboTasksApi,
magic_any::HasherMut,
manager::{current_task, mark_invalidator, with_turbo_tasks},
trace::TraceRawVcs,
util::StaticOrArc,
......@@ -138,34 +139,11 @@ impl<'de> Deserialize<'de> for Invalidator {
}
}
pub trait DynamicEqHash {
fn as_any(&self) -> &dyn Any;
fn dyn_eq(&self, other: &dyn Any) -> bool;
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl<T: Any + PartialEq + Eq + Hash> DynamicEqHash for T {
fn as_any(&self) -> &dyn Any {
self
}
fn dyn_eq(&self, other: &dyn Any) -> bool {
other
.downcast_ref::<Self>()
.map(|other| self.eq(other))
.unwrap_or(false)
}
fn dyn_hash(&self, state: &mut dyn Hasher) {
Hash::hash(&(TypeId::of::<Self>(), self), &mut HasherMut(state));
}
}
/// A user-facing reason why a task was invalidated. This should only be used
/// for invalidation that were triggered by the user.
///
/// Reasons are deduplicated, so this need to implement [Eq] and [Hash]
pub trait InvalidationReason: DynamicEqHash + Display + Send + Sync + 'static {
pub trait InvalidationReason: DynEq + DynHash + Display + Send + Sync + 'static {
fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
None
}
......@@ -176,7 +154,7 @@ pub trait InvalidationReason: DynamicEqHash + Display + Send + Sync + 'static {
///
/// Reason kinds are used a hash map key, so this need to implement [Eq] and
/// [Hash]
pub trait InvalidationReasonKind: DynamicEqHash + Send + Sync + 'static {
pub trait InvalidationReasonKind: DynEq + DynHash + Send + Sync + 'static {
/// Displays a description of multiple invalidation reasons of the same
/// kind. It is only called with two or more reasons.
fn fmt(
......@@ -186,27 +164,13 @@ pub trait InvalidationReasonKind: DynamicEqHash + Send + Sync + 'static {
) -> std::fmt::Result;
}
macro_rules! impl_eq_hash {
($ty:ty) => {
impl PartialEq for $ty {
fn eq(&self, other: &Self) -> bool {
DynamicEqHash::dyn_eq(self, other.as_any())
}
}
impl Eq for $ty {}
impl Hash for $ty {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_any().type_id().hash(state);
DynamicEqHash::dyn_hash(self, state as &mut dyn Hasher)
}
}
};
}
impl_partial_eq_for_dyn!(dyn InvalidationReason);
impl_eq_for_dyn!(dyn InvalidationReason);
impl_hash_for_dyn!(dyn InvalidationReason);
impl_eq_hash!(dyn InvalidationReason);
impl_eq_hash!(dyn InvalidationReasonKind);
impl_partial_eq_for_dyn!(dyn InvalidationReasonKind);
impl_eq_for_dyn!(dyn InvalidationReasonKind);
impl_hash_for_dyn!(dyn InvalidationReasonKind);
#[derive(PartialEq, Eq, Hash)]
enum MapKey {
......
......@@ -98,8 +98,7 @@ pub use id::{
ExecutionId, LocalTaskId, SessionId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId,
};
pub use invalidation::{
DynamicEqHash, InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator,
get_invalidator,
InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator, get_invalidator,
};
pub use join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt};
pub use key_value_pair::KeyValuePair;
......
......@@ -2,24 +2,22 @@ use core::fmt;
use std::{
any::{Any, TypeId},
fmt::Debug,
hash::{Hash, Hasher},
ops::DerefMut,
hash::Hash,
sync::Arc,
};
use serde::{Deserialize, Serialize, de::DeserializeSeed};
use turbo_dyn_eq_hash::{
DynEq, DynHash, DynPartialEq, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
};
use crate::trace::{TraceRawVcs, TraceRawVcsContext};
pub trait MagicAny: mopa::Any + Send + Sync {
pub trait MagicAny: mopa::Any + DynPartialEq + DynEq + DynHash + Send + Sync {
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send>;
fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result;
fn magic_eq(&self, other: &dyn MagicAny) -> bool;
fn magic_hash(&self, hasher: &mut dyn Hasher);
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext);
#[cfg(debug_assertions)]
......@@ -51,17 +49,6 @@ impl<T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static> MagicAny for T
d.finish()
}
fn magic_eq(&self, other: &dyn MagicAny) -> bool {
match other.downcast_ref::<Self>() {
None => false,
Some(other) => self == other,
}
}
fn magic_hash(&self, hasher: &mut dyn Hasher) {
Hash::hash(&(TypeId::of::<Self>(), self), &mut HasherMut(hasher))
}
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
self.trace_raw_vcs(trace_context);
}
......@@ -78,34 +65,9 @@ impl fmt::Debug for dyn MagicAny {
}
}
impl PartialEq for dyn MagicAny {
fn eq(&self, other: &Self) -> bool {
self.magic_eq(other)
}
}
impl Eq for dyn MagicAny {}
impl Hash for dyn MagicAny {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.magic_hash(hasher)
}
}
pub struct HasherMut<H: ?Sized>(pub H);
impl<H: DerefMut + ?Sized> Hasher for HasherMut<H>
where
H::Target: Hasher,
{
fn finish(&self) -> u64 {
self.0.finish()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}
}
impl_partial_eq_for_dyn!(dyn MagicAny);
impl_eq_for_dyn!(dyn MagicAny);
impl_hash_for_dyn!(dyn MagicAny);
impl TraceRawVcs for dyn MagicAny {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
......
use std::fmt::{Display, Formatter};
use std::{
any::Any,
fmt::{Display, Formatter},
};
use hyper::{Method, Uri};
use turbo_tasks::{FxIndexSet, InvalidationReason, InvalidationReasonKind, util::StaticOrArc};
......@@ -36,7 +39,12 @@ impl InvalidationReasonKind for ServerRequestKind {
) -> std::fmt::Result {
let example = reasons
.into_iter()
.map(|reason| reason.as_any().downcast_ref::<ServerRequest>().unwrap())
.map(|reason| {
let reason: &dyn InvalidationReason = &**reason;
(reason as &dyn Any)
.downcast_ref::<ServerRequest>()
.unwrap()
})
.min_by_key(|reason| reason.uri.path().len())
.unwrap();
write!(
......@@ -84,8 +92,8 @@ impl InvalidationReasonKind for ServerRequestSideEffectsKind {
let example = reasons
.into_iter()
.map(|reason| {
reason
.as_any()
let reason: &dyn InvalidationReason = &**reason;
(reason as &dyn Any)
.downcast_ref::<ServerRequestSideEffects>()
.unwrap()
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment