Unverified Commit 088bbd8d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: improve selective read support to allow `Equivalent` keys (#88760)

### What?

Allow keys for selective reads to be something equivalent to the key instead of only exactly a reference of the key.
parent 13c47b3f
use std::{ use std::{
borrow::Borrow,
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
hash::{BuildHasher, Hash}, hash::{BuildHasher, Hash},
}; };
use indexmap::{IndexMap, IndexSet}; use indexmap::{Equivalent, IndexMap, IndexSet};
use smallvec::SmallVec; use smallvec::SmallVec;
pub trait Keyed { pub trait KeyedEq {
type Key; type Key;
type Value;
fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]>; fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]>;
fn get(&self, key: &Self::Key) -> Option<&Self::Value>; }
fn contains_key(&self, key: &Self::Key) -> bool {
pub trait KeyedAccess<Q: ?Sized> {
type Value;
fn get(&self, key: &Q) -> Option<&Self::Value>;
fn contains_key(&self, key: &Q) -> bool {
self.get(key).is_some() self.get(key).is_some()
} }
} }
impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for HashMap<K, V, H> { impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> KeyedEq for HashMap<K, V, H> {
type Key = K; type Key = K;
type Value = V;
fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> { fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> {
let mut different_keys = SmallVec::new(); let mut different_keys = SmallVec::new();
for (key, value) in self.iter() { for (key, value) in self.iter() {
if let Some(other_value) = other.get(key) { if let Some(other_value) = HashMap::get(other, key) {
if value != other_value { if value != other_value {
different_keys.push(key); different_keys.push(key);
} }
...@@ -44,19 +48,24 @@ impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for HashMap<K, V, H> { ...@@ -44,19 +48,24 @@ impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for HashMap<K, V, H> {
different_keys different_keys
} }
}
fn get(&self, key: &Self::Key) -> Option<&Self::Value> { impl<K: Eq + Hash + Borrow<Q>, V, H: BuildHasher, Q: Eq + Hash + ?Sized> KeyedAccess<Q>
self.get(key) for HashMap<K, V, H>
{
type Value = V;
fn get(&self, key: &Q) -> Option<&Self::Value> {
HashMap::get(self, key)
} }
fn contains_key(&self, key: &Self::Key) -> bool { fn contains_key(&self, key: &Q) -> bool {
self.contains_key(key) HashMap::contains_key(self, key)
} }
} }
impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for IndexMap<K, V, H> { impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> KeyedEq for IndexMap<K, V, H> {
type Key = K; type Key = K;
type Value = V;
fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> { fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> {
let mut different_keys = SmallVec::new(); let mut different_keys = SmallVec::new();
...@@ -81,19 +90,22 @@ impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for IndexMap<K, V, H> { ...@@ -81,19 +90,22 @@ impl<K: Eq + Hash, V: PartialEq, H: BuildHasher> Keyed for IndexMap<K, V, H> {
different_keys different_keys
} }
}
impl<K, V, H: BuildHasher, Q: Equivalent<K> + Hash + ?Sized> KeyedAccess<Q> for IndexMap<K, V, H> {
type Value = V;
fn get(&self, key: &Self::Key) -> Option<&Self::Value> { fn get(&self, key: &Q) -> Option<&Self::Value> {
self.get(key) self.get(key)
} }
fn contains_key(&self, key: &Self::Key) -> bool { fn contains_key(&self, key: &Q) -> bool {
self.contains_key(key) self.contains_key(key)
} }
} }
impl<K: Eq + Hash, H: BuildHasher> Keyed for HashSet<K, H> { impl<K: Eq + Hash, H: BuildHasher> KeyedEq for HashSet<K, H> {
type Key = K; type Key = K;
type Value = ();
fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> { fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> {
let mut different_keys = SmallVec::new(); let mut different_keys = SmallVec::new();
...@@ -114,19 +126,24 @@ impl<K: Eq + Hash, H: BuildHasher> Keyed for HashSet<K, H> { ...@@ -114,19 +126,24 @@ impl<K: Eq + Hash, H: BuildHasher> Keyed for HashSet<K, H> {
different_keys different_keys
} }
}
fn get(&self, key: &Self::Key) -> Option<&Self::Value> { impl<K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, H: BuildHasher> KeyedAccess<Q>
for HashSet<K, H>
{
type Value = ();
fn get(&self, key: &Q) -> Option<&Self::Value> {
if self.contains(key) { Some(&()) } else { None } if self.contains(key) { Some(&()) } else { None }
} }
fn contains_key(&self, key: &Self::Key) -> bool { fn contains_key(&self, key: &Q) -> bool {
self.contains(key) self.contains(key)
} }
} }
impl<K: Eq + Hash, H: BuildHasher> Keyed for IndexSet<K, H> { impl<K: Eq + Hash, H: BuildHasher> KeyedEq for IndexSet<K, H> {
type Key = K; type Key = K;
type Value = ();
fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> { fn different_keys<'l>(&'l self, other: &'l Self) -> SmallVec<[&'l Self::Key; 2]> {
let mut different_keys = SmallVec::new(); let mut different_keys = SmallVec::new();
...@@ -147,12 +164,16 @@ impl<K: Eq + Hash, H: BuildHasher> Keyed for IndexSet<K, H> { ...@@ -147,12 +164,16 @@ impl<K: Eq + Hash, H: BuildHasher> Keyed for IndexSet<K, H> {
different_keys different_keys
} }
}
impl<K, Q: Equivalent<K> + Hash + ?Sized, H: BuildHasher> KeyedAccess<Q> for IndexSet<K, H> {
type Value = ();
fn get(&self, key: &Self::Key) -> Option<&Self::Value> { fn get(&self, key: &Q) -> Option<&Self::Value> {
if self.contains(key) { Some(&()) } else { None } if self.contains(key) { Some(&()) } else { None }
} }
fn contains_key(&self, key: &Self::Key) -> bool { fn contains_key(&self, key: &Q) -> bool {
self.contains(key) self.contains(key)
} }
} }
...@@ -181,7 +202,7 @@ mod tests { ...@@ -181,7 +202,7 @@ mod tests {
[("e", 5), ("d", 4), ("c", 3), ("b", 2), ("a", 1)] [("e", 5), ("d", 4), ("c", 3), ("b", 2), ("a", 1)]
} }
fn assert_diff<T: Keyed>(a: &T, b: &T, expected: &[&T::Key]) fn assert_diff<T: KeyedEq>(a: &T, b: &T, expected: &[&T::Key])
where where
T::Key: std::fmt::Debug + PartialEq, T::Key: std::fmt::Debug + PartialEq,
{ {
......
...@@ -35,7 +35,7 @@ use crate::{ ...@@ -35,7 +35,7 @@ use crate::{
event::{Event, EventListener}, event::{Event, EventListener},
id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId}, id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId},
id_factory::IdFactoryWithReuse, id_factory::IdFactoryWithReuse,
keyed::Keyed, keyed::KeyedEq,
macro_helpers::NativeFunction, macro_helpers::NativeFunction,
magic_any::MagicAny, magic_any::MagicAny,
message_queue::{CompilationEvent, CompilationEventQueue}, message_queue::{CompilationEvent, CompilationEventQueue},
...@@ -2040,8 +2040,8 @@ impl CurrentCellRef { ...@@ -2040,8 +2040,8 @@ impl CurrentCellRef {
pub fn keyed_compare_and_update<T>(&self, new_value: T) pub fn keyed_compare_and_update<T>(&self, new_value: T)
where where
T: PartialEq + VcValueType, T: PartialEq + VcValueType,
VcReadTarget<T>: Keyed, VcReadTarget<T>: KeyedEq,
<VcReadTarget<T> as Keyed>::Key: std::hash::Hash, <VcReadTarget<T> as KeyedEq>::Key: std::hash::Hash,
{ {
self.conditional_update(|old_value| { self.conditional_update(|old_value| {
let Some(old_value) = old_value else { let Some(old_value) = old_value else {
...@@ -2069,8 +2069,8 @@ impl CurrentCellRef { ...@@ -2069,8 +2069,8 @@ impl CurrentCellRef {
new_shared_reference: SharedReference, new_shared_reference: SharedReference,
) where ) where
T: VcValueType + PartialEq, T: VcValueType + PartialEq,
VcReadTarget<T>: Keyed, VcReadTarget<T>: KeyedEq,
<VcReadTarget<T> as Keyed>::Key: std::hash::Hash, <VcReadTarget<T> as KeyedEq>::Key: std::hash::Hash,
{ {
self.conditional_update_with_shared_reference(|old_sr| { self.conditional_update_with_shared_reference(|old_sr| {
let Some(old_sr) = old_sr else { let Some(old_sr) = old_sr else {
......
...@@ -4,7 +4,6 @@ use serde::Serialize; ...@@ -4,7 +4,6 @@ use serde::Serialize;
use crate::{ use crate::{
debug::{ValueDebugFormat, ValueDebugFormatString}, debug::{ValueDebugFormat, ValueDebugFormatString},
keyed::Keyed,
trace::{TraceRawVcs, TraceRawVcsContext}, trace::{TraceRawVcs, TraceRawVcsContext},
}; };
...@@ -128,7 +127,6 @@ where ...@@ -128,7 +127,6 @@ where
impl<A, T, I, J: Iterator<Item = I>> IntoIterator for &MappedReadRef<A, T> impl<A, T, I, J: Iterator<Item = I>> IntoIterator for &MappedReadRef<A, T>
where where
T: Keyed,
for<'b> &'b T: IntoIterator<Item = I, IntoIter = J>, for<'b> &'b T: IntoIterator<Item = I, IntoIter = J>,
{ {
type Item = I; type Item = I;
......
...@@ -2,7 +2,7 @@ use std::{any::type_name, marker::PhantomData}; ...@@ -2,7 +2,7 @@ use std::{any::type_name, marker::PhantomData};
use super::{read::VcRead, traits::VcValueType}; use super::{read::VcRead, traits::VcValueType};
use crate::{ use crate::{
RawVc, Vc, backend::VerificationMode, keyed::Keyed, manager::find_cell_by_type, RawVc, Vc, backend::VerificationMode, keyed::KeyedEq, manager::find_cell_by_type,
task::shared_reference::TypedSharedReference, task::shared_reference::TypedSharedReference,
}; };
...@@ -93,8 +93,8 @@ pub struct VcCellKeyedCompareMode<T> { ...@@ -93,8 +93,8 @@ pub struct VcCellKeyedCompareMode<T> {
impl<T> VcCellMode<T> for VcCellKeyedCompareMode<T> impl<T> VcCellMode<T> for VcCellKeyedCompareMode<T>
where where
T: VcValueType + PartialEq, T: VcValueType + PartialEq,
VcReadTarget<T>: Keyed, VcReadTarget<T>: KeyedEq,
<VcReadTarget<T> as Keyed>::Key: std::hash::Hash, <VcReadTarget<T> as KeyedEq>::Key: std::hash::Hash,
{ {
fn cell(inner: VcReadTarget<T>) -> Vc<T> { fn cell(inner: VcReadTarget<T>) -> Vc<T> {
let cell = find_cell_by_type::<T>(); let cell = find_cell_by_type::<T>();
......
...@@ -34,7 +34,7 @@ pub use self::{ ...@@ -34,7 +34,7 @@ pub use self::{
use crate::{ use crate::{
CellId, RawVc, ResolveTypeError, CellId, RawVc, ResolveTypeError,
debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString}, debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString},
keyed::Keyed, keyed::{KeyedAccess, KeyedEq},
registry, registry,
trace::{TraceRawVcs, TraceRawVcsContext}, trace::{TraceRawVcs, TraceRawVcsContext},
vc::read::{ReadContainsKeyedVcFuture, ReadKeyedVcFuture}, vc::read::{ReadContainsKeyedVcFuture, ReadKeyedVcFuture},
...@@ -674,22 +674,26 @@ where ...@@ -674,22 +674,26 @@ where
impl<T> Vc<T> impl<T> Vc<T>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, VcReadTarget<T>: KeyedEq,
<VcReadTarget<T> as Keyed>::Key: Hash,
{ {
/// Read the value and selects a keyed value from it. Only depends on the used key instead of /// Read the value and selects a keyed value from it. Only depends on the used key instead of
/// the full value. /// the full value.
pub fn get<'l>(self, key: &'l <VcReadTarget<T> as Keyed>::Key) -> ReadKeyedVcFuture<'l, T> { pub fn get<'l, Q>(self, key: &'l Q) -> ReadKeyedVcFuture<'l, T, Q>
where
Q: Hash + ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{
let future: ReadVcFuture<T> = self.node.into_read(T::has_serialization()).into(); let future: ReadVcFuture<T> = self.node.into_read(T::has_serialization()).into();
future.get(key) future.get(key)
} }
/// Read the value and checks if it contains the given key. Only depends on the used key instead /// Read the value and checks if it contains the given key. Only depends on the used key instead
/// of the full value. /// of the full value.
pub fn contains_key<'l>( pub fn contains_key<'l, Q>(self, key: &'l Q) -> ReadContainsKeyedVcFuture<'l, T, Q>
self, where
key: &'l <VcReadTarget<T> as Keyed>::Key, Q: Hash + ?Sized,
) -> ReadContainsKeyedVcFuture<'l, T> { VcReadTarget<T>: KeyedAccess<Q>,
{
let future: ReadVcFuture<T> = self.node.into_read(T::has_serialization()).into(); let future: ReadVcFuture<T> = self.node.into_read(T::has_serialization()).into();
future.contains_key(key) future.contains_key(key)
} }
......
...@@ -15,7 +15,8 @@ use rustc_hash::FxBuildHasher; ...@@ -15,7 +15,8 @@ use rustc_hash::FxBuildHasher;
use super::traits::VcValueType; use super::traits::VcValueType;
use crate::{ use crate::{
MappedReadRef, ReadRawVcFuture, ReadRef, VcCast, VcValueTrait, VcValueTraitCast, MappedReadRef, ReadRawVcFuture, ReadRef, VcCast, VcValueTrait, VcValueTraitCast,
VcValueTypeCast, keyed::Keyed, VcValueTypeCast,
keyed::{KeyedAccess, KeyedEq},
}; };
type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target; type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target;
...@@ -193,12 +194,15 @@ where ...@@ -193,12 +194,15 @@ where
impl<T> ReadVcFuture<T, VcValueTypeCast<T>> impl<T> ReadVcFuture<T, VcValueTypeCast<T>>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, VcReadTarget<T>: KeyedEq,
<VcReadTarget<T> as Keyed>::Key: Hash,
{ {
/// Read the value and selects a keyed value from it. Only depends on the used key instead of /// Read the value and selects a keyed value from it. Only depends on the used key instead of
/// the full value. /// the full value.
pub fn get<'l>(mut self, key: &'l <VcReadTarget<T> as Keyed>::Key) -> ReadKeyedVcFuture<'l, T> { pub fn get<'l, Q>(mut self, key: &'l Q) -> ReadKeyedVcFuture<'l, T, Q>
where
Q: Hash + ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{
self.raw = self.raw.track_with_key(FxBuildHasher.hash_one(key)); self.raw = self.raw.track_with_key(FxBuildHasher.hash_one(key));
ReadKeyedVcFuture { future: self, key } ReadKeyedVcFuture { future: self, key }
} }
...@@ -208,10 +212,11 @@ where ...@@ -208,10 +212,11 @@ where
/// ///
/// Note: This is also invalidated when the value of the key changes, not only when the presence /// Note: This is also invalidated when the value of the key changes, not only when the presence
/// of the key changes. /// of the key changes.
pub fn contains_key<'l>( pub fn contains_key<'l, Q>(mut self, key: &'l Q) -> ReadContainsKeyedVcFuture<'l, T, Q>
mut self, where
key: &'l <VcReadTarget<T> as Keyed>::Key, Q: Hash + ?Sized,
) -> ReadContainsKeyedVcFuture<'l, T> { VcReadTarget<T>: KeyedAccess<Q>,
{
self.raw = self.raw.track_with_key(FxBuildHasher.hash_one(key)); self.raw = self.raw.track_with_key(FxBuildHasher.hash_one(key));
ReadContainsKeyedVcFuture { future: self, key } ReadContainsKeyedVcFuture { future: self, key }
} }
...@@ -284,23 +289,26 @@ where ...@@ -284,23 +289,26 @@ where
} }
pin_project! { pin_project! {
pub struct ReadKeyedVcFuture<'l, T> pub struct ReadKeyedVcFuture<'l, T, Q>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, Q: ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{ {
#[pin] #[pin]
future: ReadVcFuture<T, VcValueTypeCast<T>>, future: ReadVcFuture<T, VcValueTypeCast<T>>,
key: &'l <VcReadTarget<T> as Keyed>::Key, key: &'l Q,
} }
} }
impl<'l, T> Future for ReadKeyedVcFuture<'l, T> impl<'l, T, Q> Future for ReadKeyedVcFuture<'l, T, Q>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, Q: ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{ {
type Output = Result<Option<MappedReadRef<T, <VcReadTarget<T> as Keyed>::Value>>>; type Output = Result<Option<MappedReadRef<T, <VcReadTarget<T> as KeyedAccess<Q>>::Value>>>;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
// Safety: We never move the contents of `self` // Safety: We never move the contents of `self`
...@@ -322,21 +330,23 @@ where ...@@ -322,21 +330,23 @@ where
} }
pin_project! { pin_project! {
pub struct ReadContainsKeyedVcFuture<'l, T> pub struct ReadContainsKeyedVcFuture<'l, T, Q>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, Q: ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{ {
#[pin] #[pin]
future: ReadVcFuture<T, VcValueTypeCast<T>>, future: ReadVcFuture<T, VcValueTypeCast<T>>,
key: &'l <VcReadTarget<T> as Keyed>::Key, key: &'l Q,
} }
} }
impl<'l, T> Future for ReadContainsKeyedVcFuture<'l, T> impl<'l, T, Q> Future for ReadContainsKeyedVcFuture<'l, T, Q>
where where
T: VcValueType, T: VcValueType,
VcReadTarget<T>: Keyed, Q: ?Sized,
VcReadTarget<T>: KeyedAccess<Q>,
{ {
type Output = Result<bool>; type Output = Result<bool>;
......
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