-
Notifications
You must be signed in to change notification settings - Fork 261
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
ledger/blockstore: Use u32
for shred indices.
#2452
base: master
Are you sure you want to change the base?
Conversation
Shred indices, within a slot, can not exceed a `u32` value. Unfortunately, at the moment, there is no common type that is used when a shred index needs to be stored. Part of the code use `u32`, another considerable part uses `u64` and a few places use `usize`. Ideally, it would be better to have a dedicated type, wrapping the primitive `u32` value. This change is trying to unify the types used, moving most of the code that works with shred indices to `u32`. `u64` is used in a few locations that are serialized into the blockstore or are part of the wire protocol. Those are left as is for now. As the conversion is not 100%, there are still a few conversions left, that could be avoided should both the serialization and the wire protocol code use `u32`. I also was not completely sure about the `usize` types used by the Merkel tree code - those might be convertible to `u32` as well. Stats (roughly): * Removed `as u{32,64}`: $ git diff --cached | grep '^-.*as u\(32\|64\)' | wc -l 50 * Removed `unwrap()`/`expect()`: $ git diff --cached | grep '^-.*\(unwrap()\|expect()\)' | wc -l 20 * Added `as u{32,64}`: $ git diff --cached | grep '^+.*as u\(32\|64\)' | wc -l 7 * Added `unwrap()`/`expect()`: $ git diff --cached | grep '^+.*\(unwrap()\|expect()\)' | wc -l 32 Total is 50 + 20 - 8 - 32 = 30 conversions removed. And a lot of the removed conversion are unchecked, while a lot of the added ones are checked. Current version would still panic, if a value outside the `u32` scope is used - it will just panic further away from the bug location, complicating the debugging efforts. This change also prepares the grounds for removal of most of the conversion that are still present. We would need to convert the serialization and the wire code to `u32` as well. There are a number of `as usize` conversions added in the new code. Those should be safe, as they convert from `u32`. Most of them are used when indexing into `Vec`s.
6daa7fb
to
28ae49b
Compare
@behzadnouri @AshwinSekar Behzad, I think I've showed you this change more than a year ago. |
Thanks @ilya-bobyr for making this change and I do agree having consistent types is generally preferred. However this change is pretty big and intrusive and I am worried something pretty subtle will be missed out in the review. Some of the changes might also have performance implications as u64 is machine word size and u32 is not. I do also see a new That said, smaller parts of this change might be ok to merge if they are done in separate much much smaller commits.
The goal there was to patch some very recently added changes and make blockstore type match the shred struct and as you see the changes are pretty small and very limited in scope. |
I absolutely agree here.
I've done the math in the PR description that suggests that it is a net positive.
I can try splitting this change into smaller chunks. Notice that in this change, I've only updated function arguments and return types.
If either of you are interested in working with me on this, I could see what chunks I can cut it into. |
Shred indices, within a slot, can not exceed a
u32
value.Unfortunately, at the moment, there is no common type that is used when a shred index needs to be stored.
Part of the code use
u32
, another considerable part usesu64
and a few places useusize
.Ideally, it would be better to have a dedicated type, wrapping the primitive
u32
value.This change is trying to unify the types used, moving most of the code that works with shred indices to
u32
.u64
is used in a few locations that are serialized into the blockstore or are part of the wire protocol.Those are left as is for now.
As the conversion is not 100%, there are still a few conversions left, that could be avoided should both the serialization and the wire protocol code use
u32
.I also was not completely sure about the
usize
types used by the Merkel tree code - those might be convertible tou32
as well.Stats (roughly):
as u{32,64}
:unwrap()
/expect()
:as u{32,64}
:unwrap()
/expect()
:Total is
50 + 20 - 7 - 32 = 31
conversions removed.And a lot of the removed conversion are unchecked, while a lot of the added ones are checked.
The current version would still panic if a value outside the
u32
scope is used - it will just panic further away from the bug location, complicating the debugging efforts.This change also prepares the grounds for removal of most of the conversion that are still present.
We would need to convert the serialization and the wire code to
u32
as well.There are a number of
as usize
conversions added in the new code.Those should be safe, as they convert from
u32
.Most of them are used when indexing into
Vec
s.