Reserve exact size

This commit is contained in:
Antonio Andelic 2024-11-29 13:57:01 +01:00
parent f9113db7a4
commit f56b9c353c
2 changed files with 22 additions and 11 deletions

View File

@ -63,7 +63,7 @@ MarksInCompressedFile::MarksInCompressedFile(const PlainArray & marks)
// Overallocate by +1 element to let the bit packing/unpacking do less bounds checking.
size_t packed_length = (packed_bits + 63) / 64 + 1;
packed.reserve(packed_length);
packed.reserve_exact(packed_length);
packed.resize_fill(packed_length);
// Second pass: write out the packed marks.

View File

@ -35,18 +35,29 @@ TEST(Marks, Compression)
EXPECT_LE((marks.approximateMemoryUsage() - sizeof(MarksInCompressedFile)) * 8, plain.size() * max_bits_per_mark);
};
// Typical.
test(gen(10000, 1'000'000, 0), 30);
{
SCOPED_TRACE("Typical");
test(gen(10000, 1'000'000, 0), 30);
}
// Completely random 64-bit values.
test(gen(10000, UINT64_MAX - 1, UINT64_MAX - 1), 130);
// All zeros.
test(gen(10000, 0, 0), 2);
{
SCOPED_TRACE("Completely random 64-bit values");
test(gen(10000, UINT64_MAX - 1, UINT64_MAX - 1), 130);
}
// Short.
test(gen(10, 1000, 1000), 65);
{
SCOPED_TRACE("All zeros");
test(gen(10000, 0, 0), 2);
}
// Empty.
test(gen(0, 0, 0), 0);
{
SCOPED_TRACE("Short");
test(gen(10, 1000, 1000), 65);
}
{
SCOPED_TRACE("Empty");
test(gen(0, 0, 0), 0);
}
}