Unverified Commit 47a0cd2d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] Inline commonly used storages (#75284)

### What?

* move code from macro into generic struct
* move dynamic storage access into separate struct
* generate inline storage for common data
parent 111f3345
...@@ -568,6 +568,7 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" ...@@ -568,6 +568,7 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
name = "auto-hash-map" name = "auto-hash-map"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"hashbrown 0.14.5",
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
"serde", "serde",
"smallvec", "smallvec",
...@@ -2756,6 +2757,7 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" ...@@ -2756,6 +2757,7 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [ dependencies = [
"ahash 0.8.11", "ahash 0.8.11",
"allocator-api2", "allocator-api2",
"serde",
] ]
[[package]] [[package]]
......
...@@ -9,6 +9,7 @@ edition = "2021" ...@@ -9,6 +9,7 @@ edition = "2021"
workspace = true workspace = true
[dependencies] [dependencies]
hashbrown = { workspace = true, features = ["serde"]}
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
smallvec = { workspace = true } smallvec = { workspace = true }
use std::{ use std::{
borrow::Borrow, borrow::Borrow,
collections::HashMap,
fmt::{Debug, Formatter}, fmt::{Debug, Formatter},
hash::{BuildHasher, BuildHasherDefault, Hash}, hash::{BuildHasher, BuildHasherDefault, Hash},
marker::PhantomData, marker::PhantomData,
}; };
use hashbrown::hash_map::HashMap;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use serde::{ use serde::{
de::{MapAccess, Visitor}, de::{MapAccess, Visitor},
...@@ -176,12 +176,10 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> AutoMap<K, V, H, ...@@ -176,12 +176,10 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> AutoMap<K, V, H,
None => Entry::Vacant(VacantEntry::List { this, list, key }), None => Entry::Vacant(VacantEntry::List { this, list, key }),
}, },
AutoMap::Map(map) => match map.entry(key) { AutoMap::Map(map) => match map.entry(key) {
std::collections::hash_map::Entry::Occupied(entry) => { hashbrown::hash_map::Entry::Occupied(entry) => {
Entry::Occupied(OccupiedEntry::Map { this, entry }) Entry::Occupied(OccupiedEntry::Map { this, entry })
} }
std::collections::hash_map::Entry::Vacant(entry) => { hashbrown::hash_map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry::Map(entry)),
Entry::Vacant(VacantEntry::Map(entry))
}
}, },
} }
} }
...@@ -198,10 +196,10 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> AutoMap<K, V, H, ...@@ -198,10 +196,10 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> AutoMap<K, V, H,
None => RawEntry::Vacant(VacantRawEntry::List { this, list }), None => RawEntry::Vacant(VacantRawEntry::List { this, list }),
}, },
AutoMap::Map(map) => match map.raw_entry_mut().from_key(key) { AutoMap::Map(map) => match map.raw_entry_mut().from_key(key) {
std::collections::hash_map::RawEntryMut::Occupied(entry) => { hashbrown::hash_map::RawEntryMut::Occupied(entry) => {
RawEntry::Occupied(OccupiedRawEntry::Map { this, entry }) RawEntry::Occupied(OccupiedRawEntry::Map { this, entry })
} }
std::collections::hash_map::RawEntryMut::Vacant(entry) => { hashbrown::hash_map::RawEntryMut::Vacant(entry) => {
RawEntry::Vacant(VacantRawEntry::Map(entry)) RawEntry::Vacant(VacantRawEntry::Map(entry))
} }
}, },
...@@ -397,7 +395,7 @@ impl<'a, K, V, H, const I: usize> IntoIterator for &'a AutoMap<K, V, H, I> { ...@@ -397,7 +395,7 @@ impl<'a, K, V, H, const I: usize> IntoIterator for &'a AutoMap<K, V, H, I> {
pub enum Iter<'a, K, V> { pub enum Iter<'a, K, V> {
List(std::slice::Iter<'a, (K, V)>), List(std::slice::Iter<'a, (K, V)>),
Map(std::collections::hash_map::Iter<'a, K, V>), Map(hashbrown::hash_map::Iter<'a, K, V>),
} }
impl<'a, K, V> Iterator for Iter<'a, K, V> { impl<'a, K, V> Iterator for Iter<'a, K, V> {
...@@ -431,7 +429,7 @@ impl<K, V> Clone for Iter<'_, K, V> { ...@@ -431,7 +429,7 @@ impl<K, V> Clone for Iter<'_, K, V> {
pub enum IterMut<'a, K, V> { pub enum IterMut<'a, K, V> {
List(std::slice::IterMut<'a, (K, V)>), List(std::slice::IterMut<'a, (K, V)>),
Map(std::collections::hash_map::IterMut<'a, K, V>), Map(hashbrown::hash_map::IterMut<'a, K, V>),
} }
impl<'a, K, V> Iterator for IterMut<'a, K, V> { impl<'a, K, V> Iterator for IterMut<'a, K, V> {
...@@ -454,7 +452,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { ...@@ -454,7 +452,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
pub enum IntoIter<K, V, const I: usize> { pub enum IntoIter<K, V, const I: usize> {
List(smallvec::IntoIter<[(K, V); I]>), List(smallvec::IntoIter<[(K, V); I]>),
Map(std::collections::hash_map::IntoIter<K, V>), Map(hashbrown::hash_map::IntoIter<K, V>),
} }
impl<K, V, const I: usize> Iterator for IntoIter<K, V, I> { impl<K, V, const I: usize> Iterator for IntoIter<K, V, I> {
...@@ -477,7 +475,7 @@ impl<K, V, const I: usize> Iterator for IntoIter<K, V, I> { ...@@ -477,7 +475,7 @@ impl<K, V, const I: usize> Iterator for IntoIter<K, V, I> {
pub enum Values<'a, K, V> { pub enum Values<'a, K, V> {
List(std::slice::Iter<'a, (K, V)>), List(std::slice::Iter<'a, (K, V)>),
Map(std::collections::hash_map::Values<'a, K, V>), Map(hashbrown::hash_map::Values<'a, K, V>),
} }
impl<'a, K, V> Iterator for Values<'a, K, V> { impl<'a, K, V> Iterator for Values<'a, K, V> {
...@@ -500,7 +498,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> { ...@@ -500,7 +498,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
pub enum ValuesMut<'a, K, V> { pub enum ValuesMut<'a, K, V> {
List(std::slice::IterMut<'a, (K, V)>), List(std::slice::IterMut<'a, (K, V)>),
Map(std::collections::hash_map::ValuesMut<'a, K, V>), Map(hashbrown::hash_map::ValuesMut<'a, K, V>),
} }
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
...@@ -523,7 +521,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { ...@@ -523,7 +521,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
pub enum IntoValues<K, V, const I: usize> { pub enum IntoValues<K, V, const I: usize> {
List(smallvec::IntoIter<[(K, V); I]>), List(smallvec::IntoIter<[(K, V); I]>),
Map(std::collections::hash_map::IntoValues<K, V>), Map(hashbrown::hash_map::IntoValues<K, V>),
} }
impl<K, V, const I: usize> Iterator for IntoValues<K, V, I> { impl<K, V, const I: usize> Iterator for IntoValues<K, V, I> {
...@@ -586,7 +584,7 @@ pub enum OccupiedEntry<'a, K, V, H, const I: usize> { ...@@ -586,7 +584,7 @@ pub enum OccupiedEntry<'a, K, V, H, const I: usize> {
}, },
Map { Map {
this: *mut AutoMap<K, V, H, I>, this: *mut AutoMap<K, V, H, I>,
entry: std::collections::hash_map::OccupiedEntry<'a, K, V>, entry: hashbrown::hash_map::OccupiedEntry<'a, K, V, H>,
}, },
} }
...@@ -616,6 +614,20 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> OccupiedEntry<'_ ...@@ -616,6 +614,20 @@ impl<K: Eq + Hash, V, H: BuildHasher + Default, const I: usize> OccupiedEntry<'_
OccupiedEntry::Map { entry, this: _ } => entry.remove(), OccupiedEntry::Map { entry, this: _ } => entry.remove(),
} }
} }
pub fn replace_entry_with(self, func: impl FnOnce(&K, V) -> Option<V>) {
match self {
OccupiedEntry::List { list, index } => {
let (key, value) = list.swap_remove(index);
if let Some(value) = func(&key, value) {
list.push((key, value));
}
}
OccupiedEntry::Map { entry, .. } => {
entry.replace_entry_with(func);
}
}
}
} }
pub enum VacantEntry<'a, K, V, H, const I: usize> { pub enum VacantEntry<'a, K, V, H, const I: usize> {
...@@ -624,7 +636,7 @@ pub enum VacantEntry<'a, K, V, H, const I: usize> { ...@@ -624,7 +636,7 @@ pub enum VacantEntry<'a, K, V, H, const I: usize> {
list: &'a mut SmallVec<[(K, V); I]>, list: &'a mut SmallVec<[(K, V); I]>,
key: K, key: K,
}, },
Map(std::collections::hash_map::VacantEntry<'a, K, V>), Map(hashbrown::hash_map::VacantEntry<'a, K, V, H>),
} }
impl<'a, K: Eq + Hash, V, H: BuildHasher + Default + 'a, const I: usize> impl<'a, K: Eq + Hash, V, H: BuildHasher + Default + 'a, const I: usize>
...@@ -659,7 +671,7 @@ pub enum OccupiedRawEntry<'a, K, V, H, const I: usize> { ...@@ -659,7 +671,7 @@ pub enum OccupiedRawEntry<'a, K, V, H, const I: usize> {
}, },
Map { Map {
this: *mut AutoMap<K, V, H, I>, this: *mut AutoMap<K, V, H, I>,
entry: std::collections::hash_map::RawOccupiedEntryMut<'a, K, V, H>, entry: hashbrown::hash_map::RawOccupiedEntryMut<'a, K, V, H>,
}, },
} }
...@@ -696,7 +708,7 @@ pub enum VacantRawEntry<'a, K, V, H, const I: usize> { ...@@ -696,7 +708,7 @@ pub enum VacantRawEntry<'a, K, V, H, const I: usize> {
this: *mut AutoMap<K, V, H, I>, this: *mut AutoMap<K, V, H, I>,
list: &'a mut SmallVec<[(K, V); I]>, list: &'a mut SmallVec<[(K, V); I]>,
}, },
Map(std::collections::hash_map::RawVacantEntryMut<'a, K, V, H>), Map(hashbrown::hash_map::RawVacantEntryMut<'a, K, V, H>),
} }
impl<'a, K: Eq + Hash, V, H: BuildHasher + Default + 'a, const I: usize> impl<'a, K: Eq + Hash, V, H: BuildHasher + Default + 'a, const I: usize>
...@@ -862,7 +874,7 @@ where ...@@ -862,7 +874,7 @@ where
index: usize, index: usize,
f: F, f: F,
}, },
Map(std::collections::hash_map::ExtractIf<'l, K, V, F>), Map(hashbrown::hash_map::ExtractIf<'l, K, V, F>),
} }
impl<K, V, const I: usize, F> Iterator for ExtractIfIter<'_, K, V, I, F> impl<K, V, const I: usize, F> Iterator for ExtractIfIter<'_, K, V, I, F>
......
use either::Either;
use turbo_tasks::KeyValuePair;
use crate::data::{
CachedDataItem, CachedDataItemKey, CachedDataItemStorage, CachedDataItemType,
CachedDataItemValue, CachedDataItemValueRef, CachedDataItemValueRefMut,
};
pub struct DynamicStorage {
map: Vec<CachedDataItemStorage>,
}
impl DynamicStorage {
pub fn new() -> Self {
Self {
map: Default::default(),
}
}
fn get_or_create_map_mut(&mut self, ty: CachedDataItemType) -> &mut CachedDataItemStorage {
let i = self.map.iter().position(|m| m.ty() == ty);
if let Some(i) = i {
&mut self.map[i]
} else {
self.map.reserve_exact(1);
self.map.push(CachedDataItemStorage::new(ty));
self.map.last_mut().unwrap()
}
}
fn get_map_mut(&mut self, ty: CachedDataItemType) -> Option<&mut CachedDataItemStorage> {
self.map.iter_mut().find(|m| m.ty() == ty)
}
fn get_map_index(&mut self, ty: CachedDataItemType) -> Option<usize> {
self.map.iter_mut().position(|m| m.ty() == ty)
}
fn get_or_create_map_index(&mut self, ty: CachedDataItemType) -> usize {
let i = self.map.iter().position(|m| m.ty() == ty);
if let Some(i) = i {
i
} else {
let i = self.map.len();
self.map.reserve_exact(1);
self.map.push(CachedDataItemStorage::new(ty));
i
}
}
fn get_map(&self, ty: CachedDataItemType) -> Option<&CachedDataItemStorage> {
self.map.iter().find(|m| m.ty() == ty)
}
pub fn add(&mut self, item: CachedDataItem) -> bool {
let ty = item.ty();
self.get_or_create_map_mut(ty).add(item)
}
pub fn insert(&mut self, item: CachedDataItem) -> Option<CachedDataItemValue> {
let ty = item.ty();
self.get_or_create_map_mut(ty).insert(item)
}
pub fn remove(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValue> {
self.get_map_index(key.ty()).and_then(|i| {
let storage = &mut self.map[i];
let result = storage.remove(key);
if result.is_some() && storage.is_empty() {
self.map.swap_remove(i);
self.map.shrink_to_fit();
}
result
})
}
pub fn get(&self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRef> {
self.get_map(key.ty()).and_then(|m| m.get(key))
}
pub fn get_mut(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRefMut> {
self.get_map_mut(key.ty()).and_then(|m| m.get_mut(key))
}
pub fn get_mut_or_insert_with(
&mut self,
key: CachedDataItemKey,
f: impl FnOnce() -> CachedDataItemValue,
) -> CachedDataItemValueRefMut<'_> {
self.get_or_create_map_mut(key.ty())
.get_mut_or_insert_with(key, f)
}
pub fn contains_key(&self, key: &CachedDataItemKey) -> bool {
self.get_map(key.ty())
.map(|m| m.contains_key(key))
.unwrap_or_default()
}
pub fn count(&self, ty: CachedDataItemType) -> usize {
self.get_map(ty).map(|m| m.len()).unwrap_or_default()
}
pub fn iter(
&self,
ty: CachedDataItemType,
) -> impl Iterator<Item = (CachedDataItemKey, CachedDataItemValueRef<'_>)> {
self.get_map(ty).map(|m| m.iter()).into_iter().flatten()
}
pub fn iter_all(
&self,
) -> impl Iterator<Item = (CachedDataItemKey, CachedDataItemValueRef<'_>)> {
self.map.iter().flat_map(|m| m.iter())
}
pub fn extract_if<'l, F>(
&'l mut self,
ty: CachedDataItemType,
mut f: F,
) -> impl Iterator<Item = CachedDataItem> + use<'l, F>
where
F: for<'a> FnMut(CachedDataItemKey, CachedDataItemValueRef<'a>) -> bool + 'l,
{
// TODO this could be more efficient when the storage would support extract_if directly.
// This requires some macro magic to make it work...
// But we could potentially avoid the two temporary Vecs.
let Some(i) = self.get_map_index(ty) else {
return Either::Left(std::iter::empty());
};
let storage = &mut self.map[i];
let items_to_extract = storage
.iter()
.filter(|(k, v)| f(*k, *v))
.map(|(key, _)| key)
.collect::<Vec<_>>();
let items = items_to_extract
.into_iter()
.map(move |key| {
let value = storage.remove(&key).unwrap();
CachedDataItem::from_key_and_value(key, value)
})
.collect::<Vec<_>>();
if self.map[i].is_empty() {
self.map.swap_remove(i);
self.map.shrink_to_fit();
}
Either::Right(items.into_iter())
}
pub fn update(
&mut self,
key: CachedDataItemKey,
update: impl FnOnce(Option<CachedDataItemValue>) -> Option<CachedDataItemValue>,
) {
let i = self.get_or_create_map_index(key.ty());
let map = &mut self.map[i];
map.update(key, update);
if map.is_empty() {
self.map.swap_remove(i);
self.map.shrink_to_fit();
}
}
pub fn shrink_to_fit(&mut self, ty: CachedDataItemType) {
if let Some(map) = self.get_map_mut(ty) {
map.shrink_to_fit();
}
}
}
mod dynamic_storage;
mod operation; mod operation;
mod persisted_storage_log; mod persisted_storage_log;
mod storage; mod storage;
......
...@@ -372,7 +372,7 @@ pub trait TaskGuard: Debug { ...@@ -372,7 +372,7 @@ pub trait TaskGuard: Debug {
fn get_mut(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRefMut<'_>>; fn get_mut(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRefMut<'_>>;
fn get_mut_or_insert_with( fn get_mut_or_insert_with(
&mut self, &mut self,
key: &CachedDataItemKey, key: CachedDataItemKey,
insert: impl FnOnce() -> CachedDataItemValue, insert: impl FnOnce() -> CachedDataItemValue,
) -> CachedDataItemValueRefMut<'_>; ) -> CachedDataItemValueRefMut<'_>;
fn has_key(&self, key: &CachedDataItemKey) -> bool; fn has_key(&self, key: &CachedDataItemKey) -> bool;
...@@ -603,7 +603,7 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> { ...@@ -603,7 +603,7 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> {
fn get_mut_or_insert_with( fn get_mut_or_insert_with(
&mut self, &mut self,
key: &CachedDataItemKey, key: CachedDataItemKey,
insert: impl FnOnce() -> CachedDataItemValue, insert: impl FnOnce() -> CachedDataItemValue,
) -> CachedDataItemValueRefMut<'_> { ) -> CachedDataItemValueRefMut<'_> {
self.check_access(key.category()); self.check_access(key.category());
...@@ -612,7 +612,7 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> { ...@@ -612,7 +612,7 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> {
fn has_key(&self, key: &CachedDataItemKey) -> bool { fn has_key(&self, key: &CachedDataItemKey) -> bool {
self.check_access(key.category()); self.check_access(key.category());
self.task.has_key(key) self.task.contains_key(key)
} }
fn count(&self, ty: CachedDataItemType) -> usize { fn count(&self, ty: CachedDataItemType) -> usize {
......
...@@ -8,7 +8,10 @@ use turbo_tasks::{ ...@@ -8,7 +8,10 @@ use turbo_tasks::{
CellId, KeyValuePair, SessionId, TaskId, TraitTypeId, TypedSharedReference, ValueTypeId, CellId, KeyValuePair, SessionId, TaskId, TraitTypeId, TypedSharedReference, ValueTypeId,
}; };
use crate::backend::TaskDataCategory; use crate::{
backend::TaskDataCategory,
data_storage::{AutoMapStorage, OptionStorage, Storage},
};
// this traits are needed for the transient variants of `CachedDataItem` // this traits are needed for the transient variants of `CachedDataItem`
// transient variants are never cloned or compared // transient variants are never cloned or compared
...@@ -694,6 +697,7 @@ pub enum CachedDataUpdate { ...@@ -694,6 +697,7 @@ pub enum CachedDataUpdate {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn test_sizes() { fn test_sizes() {
assert_eq!(std::mem::size_of::<super::CachedDataItem>(), 40); assert_eq!(std::mem::size_of::<super::CachedDataItem>(), 40);
......
use std::hash::{BuildHasherDefault, Hash};
use auto_hash_map::{map::Entry, AutoMap};
use rustc_hash::FxHasher;
pub trait Storage {
type K;
type V;
type Iterator<'l>: Iterator<Item = (&'l Self::K, &'l Self::V)> + 'l
where
Self: 'l;
fn add(&mut self, key: Self::K, value: Self::V) -> bool;
fn insert(&mut self, key: Self::K, value: Self::V) -> Option<Self::V>;
fn remove(&mut self, key: &Self::K) -> Option<Self::V>;
fn contains_key(&self, key: &Self::K) -> bool;
fn get(&self, key: &Self::K) -> Option<&Self::V>;
fn get_mut(&mut self, key: &Self::K) -> Option<&mut Self::V>;
fn get_mut_or_insert_with(&mut self, key: Self::K, f: impl FnOnce() -> Self::V)
-> &mut Self::V;
fn extract_if<'l, F>(&'l mut self, f: F) -> impl Iterator<Item = (Self::K, Self::V)>
where
F: for<'a, 'b> FnMut(&'a Self::K, &'b mut Self::V) -> bool + 'l;
fn update(&mut self, key: Self::K, update: impl FnOnce(Option<Self::V>) -> Option<Self::V>);
fn shrink_to_fit(&mut self);
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn iter(&self) -> Self::Iterator<'_>;
}
fn value_to_key_value<V>(value: &V) -> (&(), &V) {
(&(), value)
}
#[derive(Debug)]
pub struct OptionStorage<V> {
value: Option<V>,
}
impl<V> Default for OptionStorage<V> {
fn default() -> Self {
Self { value: None }
}
}
impl<V> Storage for OptionStorage<V> {
type K = ();
type V = V;
type Iterator<'l>
= std::option::IntoIter<(&'l (), &'l V)>
where
Self: 'l;
fn add(&mut self, _: (), value: V) -> bool {
if self.value.is_none() {
self.value = Some(value);
true
} else {
false
}
}
fn insert(&mut self, _: (), value: V) -> Option<V> {
self.value.replace(value)
}
fn remove(&mut self, _: &()) -> Option<V> {
self.value.take()
}
fn contains_key(&self, _: &()) -> bool {
self.value.is_some()
}
fn get(&self, _: &()) -> Option<&V> {
self.value.as_ref()
}
fn get_mut(&mut self, _: &()) -> Option<&mut V> {
self.value.as_mut()
}
fn get_mut_or_insert_with(&mut self, _: (), f: impl FnOnce() -> V) -> &mut V {
self.value.get_or_insert_with(f)
}
fn shrink_to_fit(&mut self) {
// Nothing to do
}
fn is_empty(&self) -> bool {
self.value.is_none()
}
fn len(&self) -> usize {
if self.value.is_some() {
1
} else {
0
}
}
fn iter(&self) -> Self::Iterator<'_> {
self.value.as_ref().map(value_to_key_value).into_iter()
}
fn extract_if<'l, F>(&'l mut self, mut f: F) -> impl Iterator<Item = (Self::K, Self::V)>
where
F: for<'a, 'b> FnMut(&'a Self::K, &'b mut Self::V) -> bool + 'l,
{
if let Some(value) = self.value.as_mut() {
if f(&(), value) {
return self.value.take().map(|v| ((), v)).into_iter();
}
}
None.into_iter()
}
fn update(&mut self, _: (), update: impl FnOnce(Option<V>) -> Option<V>) {
self.value = update(self.value.take());
}
}
#[derive(Debug)]
pub struct AutoMapStorage<K, V> {
map: AutoMap<K, V, BuildHasherDefault<FxHasher>, 1>,
}
impl<K, V> Default for AutoMapStorage<K, V> {
fn default() -> Self {
Self {
map: AutoMap::default(),
}
}
}
impl<K: Hash + Eq, V> Storage for AutoMapStorage<K, V> {
type K = K;
type V = V;
type Iterator<'l>
= auto_hash_map::map::Iter<'l, K, V>
where
Self: 'l;
fn add(&mut self, key: K, value: V) -> bool {
match self.map.entry(key) {
auto_hash_map::map::Entry::Vacant(entry) => {
entry.insert(value);
true
}
auto_hash_map::map::Entry::Occupied(_) => false,
}
}
fn insert(&mut self, key: K, value: V) -> Option<V> {
self.map.insert(key, value)
}
fn remove(&mut self, key: &K) -> Option<V> {
self.map
.remove(key)
.inspect(|_| self.map.shrink_amortized())
}
fn contains_key(&self, key: &K) -> bool {
self.map.contains_key(key)
}
fn get(&self, key: &K) -> Option<&V> {
self.map.get(key)
}
fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.map.get_mut(key)
}
fn get_mut_or_insert_with(&mut self, key: K, f: impl FnOnce() -> V) -> &mut V {
self.map.entry(key).or_insert_with(f)
}
fn shrink_to_fit(&mut self) {
self.map.shrink_to_fit()
}
fn is_empty(&self) -> bool {
self.map.is_empty()
}
fn len(&self) -> usize {
self.map.len()
}
fn iter(&self) -> Self::Iterator<'_> {
self.map.iter()
}
fn extract_if<'l, F>(&'l mut self, f: F) -> impl Iterator<Item = (Self::K, Self::V)>
where
F: for<'a, 'b> FnMut(&'a Self::K, &'b mut Self::V) -> bool + 'l,
{
self.map.extract_if(f)
}
fn update(&mut self, key: K, update: impl FnOnce(Option<V>) -> Option<V>) {
match self.map.entry(key) {
Entry::Vacant(e) => {
if let Some(new) = update(None) {
e.insert(new);
}
}
Entry::Occupied(e) => e.replace_entry_with(move |_, v| update(Some(v))),
}
}
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
mod backend; mod backend;
mod backing_storage; mod backing_storage;
mod data; mod data;
mod data_storage;
mod database; mod database;
mod kv_backing_storage; mod kv_backing_storage;
mod utils; mod utils;
......
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