Optimize decode_len (#5975)

* Optimize `decode_len`

Instead of reading the full storage value into the runtime, we only read
at maximum `5bytes` from the storage into the runtime. Furthermore this
drops any handling with regards to set default values in
`decl_storage!`. If the value does not exists or the decoding of the
length fails, it will return `None`. To prevent people from messing
stuff up, this feature relies on the `StorageDecodeLength` trait that is
sealed by `frame-support` (aka only implementable inside this crate).

* Some clean ups

* Update frame/support/src/storage/mod.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
This commit is contained in:
Bastian Köcher
2020-05-12 12:13:28 +02:00
committed by GitHub
parent 0690bb51a8
commit 22db788c08
7 changed files with 91 additions and 87 deletions
@@ -593,38 +593,40 @@ mod test_append_and_len {
});
}
// `decode_len` should always return `None` for default assigments
// in `decl_storage!`.
#[test]
fn len_works_for_default() {
fn len_works_ignores_default_assignment() {
TestExternalities::default().execute_with(|| {
// vec
assert_eq!(JustVec::get(), vec![]);
assert_eq!(JustVec::decode_len(), Ok(0));
assert_eq!(JustVec::decode_len(), None);
assert_eq!(JustVecWithDefault::get(), vec![6, 9]);
assert_eq!(JustVecWithDefault::decode_len(), Ok(2));
assert_eq!(JustVecWithDefault::decode_len(), None);
assert_eq!(OptionVec::get(), None);
assert_eq!(OptionVec::decode_len(), Ok(0));
assert_eq!(OptionVec::decode_len(), None);
// map
assert_eq!(MapVec::get(0), vec![]);
assert_eq!(MapVec::decode_len(0), Ok(0));
assert_eq!(MapVec::decode_len(0), None);
assert_eq!(MapVecWithDefault::get(0), vec![6, 9]);
assert_eq!(MapVecWithDefault::decode_len(0), Ok(2));
assert_eq!(MapVecWithDefault::decode_len(0), None);
assert_eq!(OptionMapVec::get(0), None);
assert_eq!(OptionMapVec::decode_len(0), Ok(0));
assert_eq!(OptionMapVec::decode_len(0), None);
// Double map
assert_eq!(DoubleMapVec::get(0, 0), vec![]);
assert_eq!(DoubleMapVec::decode_len(0, 1), Ok(0));
assert_eq!(DoubleMapVec::decode_len(0, 1), None);
assert_eq!(DoubleMapVecWithDefault::get(0, 0), vec![6, 9]);
assert_eq!(DoubleMapVecWithDefault::decode_len(0, 1), Ok(2));
assert_eq!(DoubleMapVecWithDefault::decode_len(0, 1), None);
assert_eq!(OptionDoubleMapVec::get(0, 0), None);
assert_eq!(OptionDoubleMapVec::decode_len(0, 1), Ok(0));
assert_eq!(OptionDoubleMapVec::decode_len(0, 1), None);
});
}
}