Skip to content
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

Implement Entry API for storage2::LazyHashMap #480

Merged
merged 30 commits into from Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
98fe682
[core] Rename Entry to Internal Entry
cmichi Jul 22, 2020
660adef
[core] Add Entry API for storage2::LazyHashMap
cmichi Jul 22, 2020
e3149a0
[core] Add storage2::LazyHashMap::len()
cmichi Jul 22, 2020
94775ab
[core] Migrate tests to use storage2::LazyHashMap::len()
cmichi Jul 22, 2020
0384579
[core] Implement FromIterator and Extend for storage2::LazyHashMap
cmichi Jul 23, 2020
7460c5d
[core] Implement macro to generate LazyHashMap + HashMap Entry API tests
cmichi Jul 23, 2020
5dc160f
[core] Remove redundant storage2::HashMap Entry API tests
cmichi Jul 23, 2020
3f7c8d6
[core] Make storage2::HashMap Entry API use storage2::LazyHashMap's E…
cmichi Jul 24, 2020
0e7ebff
[core] Move parameterized Entry API tests into separate file
cmichi Jul 24, 2020
465f73c
[core] Rename InternalEntry to StorageEntry
cmichi Jul 24, 2020
e3a561c
[core] Make lazy_hmap module public
cmichi Jul 24, 2020
6748f60
[core] Generate Entry API benches for LazyHashMap and HashMap from macro
cmichi Jul 24, 2020
2a94f64
[core] Minor streamlining
cmichi Jul 24, 2020
5fe1bec
[core] Display hashmap variant in benchmark description
cmichi Jul 24, 2020
6be446a
[core] Fix comment
cmichi Jul 24, 2020
6bbc5e4
[core] Fix typos
cmichi Jul 31, 2020
f21e3d0
[core] Make more use of BTreeMap Entry API
cmichi Jul 31, 2020
5a39bd3
[core] Replace unwrap with expect
cmichi Jul 31, 2020
b0471e0
[core] Improve comment
cmichi Jul 31, 2020
093a814
[core] Handle loading from storage
cmichi Jul 31, 2020
dc33fc3
[core] Restrict unsafe
cmichi Jul 31, 2020
bc8a1ad
[core] Less ops for case "entry not in cache, but in storage"
cmichi Aug 28, 2020
7292c29
[core] Rename len()
cmichi Aug 31, 2020
c45ad81
[core] Fix typo
cmichi Aug 31, 2020
5a6cefb
[core] Fix visibility
cmichi Aug 31, 2020
debb98e
[core] Address comments
cmichi Sep 1, 2020
f5d011d
[core] Add test to verify that cache is marked as 'Mutated'
cmichi Sep 2, 2020
d64972c
Merge branch 'master' into cmichi-implement-lazyhashmap-entry-api
cmichi Sep 2, 2020
ee66762
[core] Shorten code with utility function
cmichi Sep 2, 2020
758643c
[core] Improve naming
cmichi Sep 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 12 additions & 24 deletions core/src/storage2/collections/hashmap/mod.rs
Expand Up @@ -107,48 +107,42 @@ struct ValueEntry<V> {
}

/// A vacant entry with previous and next vacant indices.
pub struct OccupiedEntry<'a, K, V, H = Blake2x256Hasher>
pub struct OccupiedEntry<'a, K, V>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// A reference to the `Stash` instance, containing the keys.
keys: &'a mut Stash<K>,
/// The `LazyHashMap::OccupiedEntry`.
values_entry: LazyOccupiedEntry<'a, K, ValueEntry<V>, H>,
values_entry: LazyOccupiedEntry<'a, K, ValueEntry<V>>,
}

/// A vacant entry with previous and next vacant indices.
pub struct VacantEntry<'a, K, V, H = Blake2x256Hasher>
pub struct VacantEntry<'a, K, V>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// A reference to the `Stash` instance, containing the keys.
keys: &'a mut Stash<K>,
/// The `LazyHashMap::VacantEntry`.
values_entry: LazyVacantEntry<'a, K, ValueEntry<V>, H>,
values_entry: LazyVacantEntry<'a, K, ValueEntry<V>>,
}

/// An entry within the stash.
///
/// The vacant entries within a storage stash form a doubly linked list of
/// vacant entries that is used to quickly re-use their vacant storage.
pub enum Entry<'a, K: 'a, V: 'a, H = Blake2x256Hasher>
pub enum Entry<'a, K: 'a, V: 'a>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// A vacant entry that holds the index to the next and previous vacant entry.
Vacant(VacantEntry<'a, K, V, H>),
Vacant(VacantEntry<'a, K, V>),
/// An occupied entry that holds the value.
Occupied(OccupiedEntry<'a, K, V, H>),
Occupied(OccupiedEntry<'a, K, V>),
}

impl<K, V, H> HashMap<K, V, H>
Expand Down Expand Up @@ -389,7 +383,7 @@ where
}

/// Gets the given key's corresponding entry in the map for in-place manipulation.
pub fn entry(&mut self, key: K) -> Entry<K, V, H> {
pub fn entry(&mut self, key: K) -> Entry<K, V> {
let entry = self.values.entry(key);
match entry {
LazyEntry::Occupied(o) => {
Expand All @@ -408,12 +402,10 @@ where
}
}

impl<'a, K, V, H> Entry<'a, K, V, H>
impl<'a, K, V> Entry<'a, K, V>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout + core::fmt::Debug + core::cmp::Eq + Default,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// Returns a reference to this entry's key.
pub fn key(&self) -> &K {
Expand Down Expand Up @@ -485,17 +477,15 @@ where
}

/// Inserts `value` into `entry`.
fn insert(value: V, entry: VacantEntry<'a, K, V, H>) -> &'a mut V {
fn insert(value: V, entry: VacantEntry<'a, K, V>) -> &'a mut V {
entry.insert(value)
}
}

impl<'a, K, V, H> VacantEntry<'a, K, V, H>
impl<'a, K, V> VacantEntry<'a, K, V>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// Gets a reference to the key that would be used when inserting a value through the VacantEntry.
pub fn key(&self) -> &K {
Expand All @@ -518,12 +508,10 @@ where
}
}

impl<'a, K, V, H> OccupiedEntry<'a, K, V, H>
impl<'a, K, V> OccupiedEntry<'a, K, V>
where
K: Ord + Clone + PackedLayout,
V: PackedLayout,
H: Hasher,
Key: From<<H as Hasher>::Output>,
{
/// Gets a reference to the key in the entry.
pub fn key(&self) -> &K {
Expand Down