-
Notifications
You must be signed in to change notification settings - Fork 26
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
(#52) Optimize unspent utxo #150
Conversation
This implementation of sparse arrays is intended to store 256 elements at most. It relies on 256-bit bitmap that stores indexes of elements that actually exist and uses popcount to restore indexes in the underlying vector. SparseArray implements an iterator.
This wrapper allows to share underlying data storage until it is required to shrink. Shrinks and additions to the storage trigger actual clone operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
sparse-array/src/sparse_array.rs
Outdated
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct SparseArray<V> { | ||
index: BitmapIndex, | ||
data: Vec<V>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be reduced to a Box<[V]>
to save 8 bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about that. Will we still be able to add and remove elements at arbitrary positions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, the Box<[V]>
is for when the array is immutable/frozen. when in the tree, it's useful to reduce the cost of the array by 8 bytes by leaf. But once you want to update it, you can thaw it into a Vec
with into_vec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will change the interface a bit. Instead of set(&mut self, idx: u8, value: V)
and remove(&mut self, idx: u8) -> Option<V>
we will have set(self, idx: u8, value: V) -> Self
and remove(self, idx: u8) -> (Self, Option<V>)
.
Anyway, I think that's ok for an immutable struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I think that's fine. if you want to have a mutable interface, I usually like the pattern of having a Builder
type which use data structure that are more friendly to modification like Vec
in this case.
@vincenthz I used |
@vincenthz I suggest moving this
into a separate issue and then create a reusable bitmap that will be used by both |
yes, we can move the special cases to another low priority issue. Otherwise everything has been addressed and now ready for merge, but I'm less than conviced about the latest commit to move from |
0a020ba
to
ef7dfa4
Compare
dropped this one |
nice one ! |
SparseArray
.FastSparse
) on top of sparse array that allows to share data between its instances and thus allows for faster clones.Data under
FastSparseArray
is shared untilset
orshrink
function is called. It is also possible to avoid copying the underlying structure when callingset
, but actual use cases do not require that.TBD
POPCNT
instruction)TZCNT
)Resolves #52