Add StorageValue::append and speed-up deposit_event (#2282)

* Adds deposit event benchmark

* Add `StorageValue::append`

`StorageValue::append` can be used by types that implement `EncodeAppend` to speed-up situations where you just want to append
an item to storage without wanting to decode all previous items.

* Stay at 100 events

* Fixes compilation

* Use correct year and increase spec version
This commit is contained in:
Bastian Köcher
2019-04-16 13:17:02 +02:00
committed by GitHub
parent a6cbc965a0
commit 72840bd71e
14 changed files with 513 additions and 319 deletions
@@ -97,6 +97,12 @@ pub trait Storage {
/// Take a value from storage, deleting it after reading.
fn take_or_default<T: codec::Decode + Default>(&self, key: &[u8]) -> T { self.take(key).unwrap_or_default() }
/// Get a Vec of bytes from storage.
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Put a raw byte slice into storage.
fn put_raw(&self, key: &[u8], value: &[u8]);
}
// We use a construct like this during when genesis storage is being built.
@@ -117,6 +123,14 @@ impl<S: sr_primitives::BuildStorage> Storage for (crate::rstd::cell::RefCell<&mu
fn kill(&self, key: &[u8]) {
UnhashedStorage::kill(self, &S::hash(key))
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
UnhashedStorage::get_raw(self, key)
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
UnhashedStorage::put_raw(self, key, value)
}
}
/// A strongly-typed value kept in storage.
@@ -150,6 +164,20 @@ pub trait StorageValue<T: codec::Codec> {
fn kill<S: Storage>(storage: &S) {
storage.kill(Self::key())
}
/// Append the given items to the value in the storage.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<S: Storage, I: codec::Encode>(
items: &[I], storage: &S
) -> Result<(), &'static str> where T: codec::EncodeAppend<Item=I> {
let new_val = <T as codec::EncodeAppend>::append(
storage.get_raw(Self::key()).unwrap_or_default(),
items,
).ok_or_else(|| "Could not append given item")?;
storage.put_raw(Self::key(), &new_val);
Ok(())
}
}
/// A strongly-typed list in storage.
@@ -576,6 +604,14 @@ mod tests {
fn kill(&self, key: &[u8]) {
self.borrow_mut().remove(key);
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
self.borrow_mut().insert(key.to_owned(), value.to_owned());
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
self.borrow().get(key).cloned()
}
}
storage_items! {
+30 -3
View File
@@ -19,7 +19,7 @@
use crate::rstd::prelude::*;
use crate::rstd::borrow::Borrow;
use runtime_io::{self, twox_128};
use crate::codec::{Codec, Encode, Decode, KeyedVec, Input};
use crate::codec::{Codec, Encode, Decode, KeyedVec, Input, EncodeAppend};
#[macro_use]
pub mod generator;
@@ -153,6 +153,14 @@ impl crate::GenericStorage for RuntimeStorage {
fn take<T: Decode>(&self, key: &[u8]) -> Option<T> {
take(key)
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
get_raw(key)
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
put_raw(key, value)
}
}
impl crate::GenericUnhashedStorage for RuntimeStorage {
@@ -184,6 +192,14 @@ impl crate::GenericUnhashedStorage for RuntimeStorage {
fn take<T: Decode>(&self, key: &[u8]) -> Option<T> {
unhashed::take(key)
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
unhashed::get_raw(key)
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
unhashed::put_raw(key, value)
}
}
/// A trait for working with macro-generated storage values under the substrate storage API.
@@ -211,6 +227,12 @@ pub trait StorageValue<T: Codec> {
/// Take a value from storage, removing it afterwards.
fn take() -> Self::Query;
/// Append the given item to the value in the storage.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<I: Encode>(items: &[I]) -> Result<(), &'static str>
where T: EncodeAppend<Item=I>;
}
impl<T: Codec, U> StorageValue<T> for U where U: generator::StorageValue<T> {
@@ -237,6 +259,11 @@ impl<T: Codec, U> StorageValue<T> for U where U: generator::StorageValue<T> {
fn take() -> Self::Query {
U::take(&RuntimeStorage)
}
fn append<I: Encode>(items: &[I]) -> Result<(), &'static str>
where T: EncodeAppend<Item=I>
{
U::append(items, &RuntimeStorage)
}
}
/// A strongly-typed list in storage.
@@ -560,7 +587,7 @@ pub trait StorageVec {
/// child storage NOTE could replace unhashed by having only one kind of storage (root being null storage
/// key (storage_key can become Option<&[u8]>).
/// This module is a currently only a variant of unhashed with additional `storage_key`.
/// Note that `storage_key` must be unique and strong (strong in the sense of being long enough to
/// Note that `storage_key` must be unique and strong (strong in the sense of being long enough to
/// avoid collision from a resistant hash function (which unique implies)).
pub mod child {
use super::{runtime_io, Codec, Decode, Vec, IncrementalChildInput};
@@ -632,7 +659,7 @@ pub mod child {
runtime_io::read_child_storage(storage_key, key, &mut [0;0][..], 0).is_some()
}
/// Remove all `storage_key` key/values
/// Remove all `storage_key` key/values
pub fn kill_storage(storage_key: &[u8]) {
runtime_io::kill_child_storage(storage_key)
}
@@ -55,6 +55,12 @@ pub trait UnhashedStorage {
/// Take a value from storage, deleting it after reading.
fn take_or_default<T: codec::Decode + Default>(&self, key: &[u8]) -> T { self.take(key).unwrap_or_default() }
/// Get a Vec of bytes from storage.
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Put a raw byte slice into storage.
fn put_raw(&self, key: &[u8], value: &[u8]);
}
// We use a construct like this during when genesis storage is being built.
@@ -82,6 +88,14 @@ impl<H> UnhashedStorage for (crate::rstd::cell::RefCell<&mut sr_primitives::Stor
!key.starts_with(prefix)
})
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
self.0.borrow().get(key).cloned()
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
self.0.borrow_mut().insert(key.to_vec(), value.to_vec());
}
}
/// An implementation of a map with a two keys.