-
Notifications
You must be signed in to change notification settings - Fork 10
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
IF: pack size of bitset #57
Conversation
… it will be the size of the underlying storage
blocks.reserve(num_blocks); | ||
for( size_t i = 0; i < size.value; ++i ) { | ||
T tmp; | ||
fc::raw::unpack( s, tmp ); | ||
blocks.emplace_back( std::move(tmp) ); | ||
} |
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.
Why this change? It is arguably less efficient since it does an extra copy.
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.
Not exactly sure what you mean by this. The vector
unpack expects the size of the vector which is not there in this case. Are you talking about something else?
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 mean using this code instead:
std::vector<T> blocks(num_blocks);
for( size_t i = 0; i < num_blocks; ++i ) {
fc::raw::unpack( s, blocks[i] );
auto add_extra = [&]() { | ||
bool extra_needed = (size % (sizeof(T) * CHAR_BIT)) != 0; | ||
return static_cast<size_t>(extra_needed); // bool => 0,1 | ||
}; | ||
size_t num_blocks = size / (sizeof(T) * CHAR_BIT) + add_extra(); |
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.
or could be the more usual:
auto add_extra = [&]() { | |
bool extra_needed = (size % (sizeof(T) * CHAR_BIT)) != 0; | |
return static_cast<size_t>(extra_needed); // bool => 0,1 | |
}; | |
size_t num_blocks = size / (sizeof(T) * CHAR_BIT) + add_extra(); | |
constexpr size_t word_size = sizeof(T) * CHAR_BIT; | |
size_t num_blocks = (size + word_size - 1) / word_size; |
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.
Thanks. Much nicer.
Note:start |
Change the
pack
/unpack
scheme forboost::dynamic_bitset
used byquorum_certificate
.Before this PR the number of blocks of the underlying storage was packed. On
unpack
the size of the dynamic bitset was the size of the number of bits in the underlying blocks.With this PR, the size of the bitset is pack/unpack so that the bitset can be restored to its original size.
Included in this PR is the introduction of
fc::dynamic_bitset
defined to beboost::dynamic_bitset<uint8_t>
. Usinguint8_t
minimizes space used for packed format and provides a more expected packed format where only the bytes that are needed are packed/unpacked.fc::dynamic_bitset
is used for votes inquorum_certificate
which is included in most blocks.