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

IF: pack size of bitset #57

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 15 additions & 8 deletions libraries/libfc/include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,30 +567,37 @@ namespace fc {

template<typename Stream, typename T>
inline void pack( Stream& s, const boost::dynamic_bitset<T>& value ) {
// pack the size of the bitset, not the number of blocks
const auto num_blocks = value.num_blocks();
FC_ASSERT( num_blocks <= MAX_NUM_ARRAY_ELEMENTS );
fc::raw::pack( s, unsigned_int((uint32_t)num_blocks) );

fc::raw::pack( s, unsigned_int(value.size()) );
// 8 bits per byte
assert(num_blocks == value.size() / (sizeof(T) * CHAR_BIT) + (value.size() % (sizeof(T) * CHAR_BIT)));
// convert bitset to a vector of blocks
std::vector<T> blocks;
blocks.resize(num_blocks);
boost::to_block_range(value, blocks.begin());

// pack the blocks
for (const auto& b: blocks) {
fc::raw::pack( s, b );
fc::raw::pack( s, b );
}
}

template<typename Stream, typename T>
inline void unpack( Stream& s, boost::dynamic_bitset<T>& value ) {
// the packed size is the number of bits in the set, not the number of blocks
unsigned_int size; fc::raw::unpack( s, size );
FC_ASSERT( size.value <= MAX_NUM_ARRAY_ELEMENTS );
std::vector<T> blocks((size_t)size.value);
for( uint64_t i = 0; i < size.value; ++i ) {
fc::raw::unpack( s, blocks[i] );
size_t num_blocks = size / (sizeof(T) * CHAR_BIT) + (value.size() % (sizeof(T) * CHAR_BIT));
FC_ASSERT( num_blocks <= MAX_NUM_ARRAY_ELEMENTS );
std::vector<T> blocks;
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) );
}
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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] );

value = { blocks.cbegin(), blocks.cend() };
value.resize(size.value);
}

template<typename Stream, typename T>
Expand Down
23 changes: 22 additions & 1 deletion libraries/libfc/test/io/test_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BOOST_AUTO_TEST_CASE(dynamic_bitset_test)
constexpr uint8_t bits = 0b00011110;
boost::dynamic_bitset<uint8_t> bs1(8, bits); // bit set size 8

char buff[4];
char buff[32];
datastream<char*> ds(buff, sizeof(buff));

fc::raw::pack( ds, bs1 );
Expand All @@ -35,4 +35,25 @@ BOOST_AUTO_TEST_CASE(dynamic_bitset_test)
BOOST_CHECK(!bs2.test(7));
}

BOOST_AUTO_TEST_CASE(dynamic_bitset_large_test)
{
boost::dynamic_bitset<uint32_t> bs1;
bs1.resize(12345);

bs1.set(42);
bs1.set(23);
bs1.set(12000);

auto packed = fc::raw::pack(bs1);
auto unpacked = fc::raw::unpack<boost::dynamic_bitset<uint32_t>>(packed);

BOOST_TEST(unpacked.at(42));
BOOST_TEST(unpacked.at(23));
BOOST_TEST(unpacked.at(12000));
unpacked.flip(42);
unpacked.flip(23);
unpacked.flip(1200);
BOOST_TEST(unpacked.none());
}

BOOST_AUTO_TEST_SUITE_END()
Loading