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

Evaluate migration of SmallVec to min_const_generics #598

Merged
merged 18 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ scale = { package = "parity-scale-codec", version = "2.0", default-features = fa
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }
scale-info = { version = "0.5", default-features = false, features = ["derive"], optional = true }
cfg-if = "1.0"
array-init = "1.0"
generic-array = "0.14.1"
array-init = { version = "1.0", default-features = false, features = ["const-generics"] }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have to pass this feature, since this dependency will only make it available be default once min_const_generics is in a stable release.

Copy link
Collaborator

@Robbepop Robbepop Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm good point.
best would be to sync array-init with us or vice versa.
good thing we no longer depend on generic-array with its tons of unsafe code.


# Workaround: we actually just need criterion as a dev-dependency, but
# there is an issue with a doubly included std lib when executing
Expand Down Expand Up @@ -54,6 +53,7 @@ std = [
"scale-info/std",
]
ink-fuzz-tests = ["std"]
ink-unstable = ["std"]

[[bench]]
name = "bench_lazy"
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod binary_heap;
pub mod bitstash;
pub mod bitvec;
pub mod hashmap;
#[cfg(feature = "ink-unstable")]
pub mod smallvec;
pub mod stash;
pub mod vec;
Expand All @@ -32,11 +33,14 @@ pub use self::{
bitstash::BitStash,
bitvec::Bitvec,
hashmap::HashMap,
smallvec::SmallVec,
stash::Stash,
vec::Vec,
};

#[cfg(feature = "ink-unstable")]
#[doc(inline)]
pub use self::smallvec::SmallVec;

/// Extends the lifetime 'a to the outliving lifetime 'b for the given reference.
///
/// # Note
Expand Down
33 changes: 9 additions & 24 deletions crates/storage/src/collections/smallvec/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,24 @@ use super::{
Iter,
SmallVec,
};
use crate::{
lazy::LazyArrayLength,
traits::PackedLayout,
};
use crate::traits::PackedLayout;
use core::iter::{
Extend,
FromIterator,
};

impl<T, N> Drop for SmallVec<T, N>
impl<T, const N: usize> Drop for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn drop(&mut self) {
self.clear_cells()
}
}

impl<T, N> core::ops::Index<u32> for SmallVec<T, N>
impl<T, const N: usize> core::ops::Index<u32> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Output = T;

Expand All @@ -56,10 +51,9 @@ where
}
}

impl<T, N> core::ops::IndexMut<u32> for SmallVec<T, N>
impl<T, const N: usize> core::ops::IndexMut<u32> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn index_mut(&mut self, index: u32) -> &mut Self::Output {
let len = self.len();
Expand All @@ -75,10 +69,9 @@ where
}
}

impl<'a, T: 'a, N> IntoIterator for &'a SmallVec<T, N>
impl<'a, T: 'a, const N: usize> IntoIterator for &'a SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a T;
type IntoIter = Iter<'a, T, N>;
Expand All @@ -88,10 +81,9 @@ where
}
}

impl<T, N> Extend<T> for SmallVec<T, N>
impl<T, const N: usize> Extend<T> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn extend<I>(&mut self, iter: I)
where
Expand All @@ -103,10 +95,9 @@ where
}
}

impl<T, N> FromIterator<T> for SmallVec<T, N>
impl<T, const N: usize> FromIterator<T> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn from_iter<I>(iter: I) -> Self
where
Expand All @@ -118,10 +109,9 @@ where
}
}

impl<T, N> core::cmp::PartialEq for SmallVec<T, N>
impl<T, const N: usize> core::cmp::PartialEq for SmallVec<T, N>
where
T: PartialEq + PackedLayout,
N: LazyArrayLength<T>,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
Expand All @@ -131,9 +121,4 @@ where
}
}

impl<T, N> core::cmp::Eq for SmallVec<T, N>
where
T: Eq + PackedLayout,
N: LazyArrayLength<T>,
{
}
impl<T, const N: usize> core::cmp::Eq for SmallVec<T, N> where T: Eq + PackedLayout {}
42 changes: 11 additions & 31 deletions crates/storage/src/collections/smallvec/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
use super::SmallVec;
use crate::{
collections::extend_lifetime,
lazy::LazyArrayLength,
traits::PackedLayout,
};

/// An iterator over shared references to the elements of a small storage vector.
#[derive(Debug, Clone, Copy)]
pub struct Iter<'a, T, N>
pub struct Iter<'a, T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The storage vector to iterate over.
vec: &'a SmallVec<T, N>,
Expand All @@ -34,10 +32,9 @@ where
end: u32,
}

impl<'a, T, N> Iter<'a, T, N>
impl<'a, T, const N: usize> Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new iterator for the given storage vector.
pub(crate) fn new(vec: &'a SmallVec<T, N>) -> Self {
Expand All @@ -54,10 +51,9 @@ where
}
}

impl<'a, T, N> Iterator for Iter<'a, T, N>
impl<'a, T, const N: usize> Iterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a T;

Expand Down Expand Up @@ -86,17 +82,11 @@ where
}
}

impl<'a, T, N> ExactSizeIterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
}
impl<'a, T, const N: usize> ExactSizeIterator for Iter<'a, T, N> where T: PackedLayout {}

impl<'a, T, N> DoubleEndedIterator for Iter<'a, T, N>
impl<'a, T, const N: usize> DoubleEndedIterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn next_back(&mut self) -> Option<Self::Item> {
<Self as DoubleEndedIterator>::nth_back(self, 0)
Expand All @@ -118,10 +108,9 @@ where

/// An iterator over exclusive references to the elements of a small storage vector.
#[derive(Debug)]
pub struct IterMut<'a, T, N>
pub struct IterMut<'a, T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The storage vector to iterate over.
vec: &'a mut SmallVec<T, N>,
Expand All @@ -131,10 +120,9 @@ where
end: u32,
}

impl<'a, T, N> IterMut<'a, T, N>
impl<'a, T, const N: usize> IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new iterator for the given storage vector.
pub(crate) fn new(vec: &'a mut SmallVec<T, N>) -> Self {
Expand All @@ -152,10 +140,9 @@ where
}
}

impl<'a, T, N> IterMut<'a, T, N>
impl<'a, T, const N: usize> IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn get_mut<'b>(&'b mut self, at: u32) -> Option<&'a mut T> {
self.vec.get_mut(at).map(|value| {
Expand All @@ -171,10 +158,9 @@ where
}
}

impl<'a, T, N> Iterator for IterMut<'a, T, N>
impl<'a, T, const N: usize> Iterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a mut T;

Expand Down Expand Up @@ -203,17 +189,11 @@ where
}
}

impl<'a, T, N> ExactSizeIterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
}
impl<'a, T, const N: usize> ExactSizeIterator for IterMut<'a, T, N> where T: PackedLayout {}

impl<'a, T, N> DoubleEndedIterator for IterMut<'a, T, N>
impl<'a, T, const N: usize> DoubleEndedIterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn next_back(&mut self) -> Option<Self::Item> {
<Self as DoubleEndedIterator>::nth_back(self, 0)
Expand Down
22 changes: 7 additions & 15 deletions crates/storage/src/collections/smallvec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::{
lazy::{
Lazy,
LazyArray,
LazyArrayLength,
},
traits::PackedLayout,
};
Expand All @@ -55,31 +54,28 @@ type Index = u32;
/// `Vec` due to the internal differences.
/// - Allows to store up to N elements.
#[derive(Debug)]
pub struct SmallVec<T, N>
pub struct SmallVec<T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The current length of the small vector.
len: Lazy<u32>,
/// The entries of the small vector.
elems: LazyArray<T, N>,
}

impl<T, N> Default for SmallVec<T, N>
impl<T, const N: usize> Default for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn default() -> Self {
Self::new()
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Clears the underlying storage cells of the storage vector.
///
Expand All @@ -103,10 +99,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new empty vector.
pub fn new() -> Self {
Expand Down Expand Up @@ -135,10 +130,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Returns an iterator yielding shared references to all elements.
///
Expand Down Expand Up @@ -196,10 +190,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Appends an element to the back of the vector.
pub fn push(&mut self, value: T) {
Expand All @@ -213,10 +206,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Pops the last element from the vector and returns it.
//
Expand Down
Loading