Port storage::BTreeMap to storage2 #414
Labels
A-ink_storage
[ink_storage] Work Item
B-enhancement
New feature or request
P-low
Low priority issue.
The new
storage2
module is still missing aBTreeMap
storage data structure implementation.Low Priority
Since the
storage2
module already has a map-like collection this issue is not as pressing as the similar issue about addingBinaryHeap
orVecDeque
to thestorage2
module.Old Implementation
A
BTreeMap
for the storage has already been implemented by @cmichi in the past for the oldstorage
(revision 1) module. At that time it was really important to have since the oldstorage::HashMap
was not even a real collection so we didn't have any other proper map-like storage collections.Outline
This issue is about porting the
BTreeMap
from the oldstorage
module to thestorage2
module building on its new lazy abstractions and being operated over the newSpreadLayout
trait with semantic that is common with the collections of the newstorage2
module.Benchmarks
Most of all we are this time also interested in how both map-like collections compare in performance and storage accesses with each other. So the main topic with this issue is also providing a set of benchmarks to provide us with more insight into when which map-like storage collection is a better fit.
ToDo List
Subsets of this ToDo list can suffice to craft a PR so we can implement the
storage2::BTreeMap
in many PRs incrementally.BTreeMap
over to thestorage2
module with this API:new
: Creates a new empty b-tree map.len
: Returns the number of elements stored in the b-tree map.is_empty
: Returnstrue
if the b-tree map is empty.contains_key
: Returnstrue
if the given key is contained in the b-tree map.get
: Returns a shared reference to the value at the given key.get_mut
: Returns an exclusive reference to the value at the given key.get_key_value
: Returns a shared reference to the key-value pair at the given key.put
: Inserts a new key-value pair into the b-tree map. Returns the old value if any.insert
: Inserts a new key-value pair into the b-tree map. Does NOT return the old value since this tries to minimize the contract storage read accesses.take
: Removes the key-value pair identified by the given key and returns it.remove
: Removes the key-value pair identified by the given key and does NOT return it. This tries to avoid read accesses to the contract storage.iter
: Returns an iterator over shared references to the key-value pair of the b-tree map.keys
: Returns an iterator over shared references to the keys of the b-tree map.values
: Returns an iterator over shared references to the values of the b-tree map.values_mut
: Returns an iterator over exclusive references to the values of the b-tree map.clear
: Clears all elements from the b-tree map with minimal storage writes and no storage reads. This should be much more efficient than manually removing all elements.BTreeMap
similar to the one in the oldstorage
module.BTreeMap
.storage2::BTreeMap
.storage2::HashMap
.SpreadLayout
trait for the b-tree map.Default
: For simply contract instantiation.Debug
: Should be derivable.PartialEq
+Eq
ifT: PartialEq
andT: Eq
: Mostly important for testing but anyways nice to have in general.Drop
: Must be implemented in order to command the underlyingLazyIndexMap
to clear its cells. For an example how to implement this properly take a look at thestorage2::Vec
.Extend
andFromIterator
: Useful traits to initialize a b-tree map from an iterator.IntoIterator
for&BTreeMap
and&mut BTreeMap
: Generally useful for testing but also nice to have in general.The text was updated successfully, but these errors were encountered: