hello-stephen commented on code in PR #70774:
URL: https://github.com/apache/doris/pull/70774#discussion_r4784178008
##########
be/src/core/value/bitmap_value.h:
##########
@@ -1035,6 +1041,351 @@ class Roaring64Map {
*bytes_read = (is_v1 ? 0 : 1) + readbytes;
return r;
}
+
+ // From CRoaring `ra_portable_header_size()` (`roaring_array.c`): header
size with run_bitset.
+ static size_t portable_header_size(int32_t size) {
+ if (size <= 0) {
+ return sizeof(uint32_t) * 2;
+ }
+ const size_t n = static_cast<size_t>(size);
+ // Always include run_bitset since cookie is fixed to SERIAL_COOKIE
(12347).
+ const size_t run_bitset_bytes = (n + 7) / 8;
+ const size_t base = sizeof(uint32_t) + run_bitset_bytes + n *
sizeof(uint32_t);
+ return (size < roaring::internal::NO_OFFSET_THRESHOLD) ? base
+ : (base + n *
sizeof(uint32_t));
+ }
+
+ // Upper bound for fixed-12347 serialization (+ ceil(n/8)-4 bytes when
n>32 without RUN yet).
+ static size_t max_serialized_size(const roaring::Roaring& src, bool is_v1)
{
+ const auto& ra = src.roaring.high_low_container;
+ if (ra.size <= 0 || (ra.flags & ROARING_FLAG_FROZEN) != 0) {
+ return src.getSizeInBytes(is_v1);
+ }
+ size_t portable_max = src.getSizeInBytes(true);
+ if (ra.size > 32 && !roaring::internal::ra_has_run_container(&ra)) {
+ portable_max += ((static_cast<size_t>(ra.size) + 7) / 8) -
sizeof(uint32_t);
+ }
+ if (is_v1) {
+ return portable_max;
+ }
+ const uint64_t card =
roaring::api::roaring_bitmap_get_cardinality(&src.roaring);
+ const size_t flat_size =
+ 1 + sizeof(uint32_t) + static_cast<size_t>(card) *
sizeof(uint32_t);
+ if (card * sizeof(uint32_t) + sizeof(uint32_t) <=
+ portable_header_size(ra.size) + 2 * static_cast<size_t>(ra.size)) {
+ return flat_size;
+ }
+ // Non-early-flat callers fall back to flat only when flat_payload <=
portable_size <= portable_max.
+ return 1 + portable_max;
+ }
+
+ // From CRoaring `run_container_from_array()` + `run_container_write()`:
array -> runs bytes.
+ static size_t write_array_as_run(const
roaring::internal::array_container_t* ac, int32_t n_runs,
+ char* buf) {
+ char* out = buf;
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out),
static_cast<uint16_t>(n_runs));
+ out += sizeof(uint16_t);
+ if (ac->cardinality > 0) {
+ uint16_t start = ac->array[0];
+ uint16_t prev = start;
+ for (int32_t i = 1; i < ac->cardinality; ++i) {
+ const uint16_t cur = ac->array[i];
+ if (cur == static_cast<uint16_t>(prev + 1U)) {
+ prev = cur;
+ } else {
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out), start);
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out +
sizeof(uint16_t)),
+ static_cast<uint16_t>(prev - start));
+ out += sizeof(roaring::internal::rle16_t);
+ start = prev = cur;
+ }
+ }
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out), start);
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out +
sizeof(uint16_t)),
+ static_cast<uint16_t>(prev - start));
+ out += sizeof(roaring::internal::rle16_t);
+ }
+ return static_cast<size_t>(out - buf);
+ }
+
+ // From CRoaring `convert_run_optimize()` + `run_container_write()`:
bitset -> runs bytes.
+ static size_t write_bitset_as_run(const
roaring::internal::bitset_container_t* bc,
+ int32_t n_runs, char* buf) {
+ char* out = buf;
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out),
static_cast<uint16_t>(n_runs));
+ out += sizeof(uint16_t);
+
+ auto emit_run = [&out](uint32_t start, uint32_t end) {
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out),
static_cast<uint16_t>(start));
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out +
sizeof(uint16_t)),
+ static_cast<uint16_t>(end - start));
+ out += sizeof(roaring::internal::rle16_t);
+ };
+
+ bool in_run = false;
+ uint32_t run_start = 0;
+ for (uint32_t w = 0; w <
roaring::internal::BITSET_CONTAINER_SIZE_IN_WORDS; ++w) {
+ uint64_t word = bc->words[w];
+ if (in_run) {
+ if (word == ~UINT64_C(0)) {
+ continue;
+ }
+ const uint32_t trailing_ones =
static_cast<uint32_t>(std::countr_one(word));
+ emit_run(run_start, (w << 6) + trailing_ones - 1U);
+ in_run = false;
+ word &= (~UINT64_C(0)) << trailing_ones;
+ }
+ while (word != 0) {
+ const uint32_t b =
static_cast<uint32_t>(std::countr_zero(word));
+ const uint32_t ones =
static_cast<uint32_t>(std::countr_one(word >> b));
+ const uint32_t start = (w << 6) + b;
+ if (b + ones == 64U) {
+ run_start = start;
+ in_run = true;
+ break;
+ }
+ emit_run(start, start + ones - 1U);
+ word &= (~UINT64_C(0)) << (b + ones);
+ }
+ }
+ if (in_run) {
+ emit_run(run_start,
(roaring::internal::BITSET_CONTAINER_SIZE_IN_WORDS << 6) - 1U);
+ }
+ return static_cast<size_t>(out - buf);
+ }
+
+ // From CRoaring `array_container_from_run()` + `array_container_write()`:
runs -> array bytes.
+ static size_t write_run_as_array(const roaring::internal::run_container_t*
rc, char* buf) {
+ char* out = buf;
+ for (int32_t i = 0; i < rc->n_runs; ++i) {
+ const auto& r = rc->runs[i];
+ const uint32_t end = static_cast<uint32_t>(r.value) + r.length;
+ for (uint32_t v = r.value; v <= end; ++v) {
+ encode_fixed16_le(reinterpret_cast<uint8_t*>(out),
static_cast<uint16_t>(v));
+ out += sizeof(uint16_t);
+ }
+ }
+ return static_cast<size_t>(out - buf);
+ }
+
+ // From CRoaring `bitset_container_from_run()` +
`bitset_container_write()`: runs -> bitset bytes.
+ static size_t write_run_as_bitset(const
roaring::internal::run_container_t* rc, char* buf) {
+ // Stage in aligned stack words then memcpy so unaligned dst buffers
stay safe.
+ uint64_t words[roaring::internal::BITSET_CONTAINER_SIZE_IN_WORDS] = {};
+ for (int32_t i = 0; i < rc->n_runs; ++i) {
+ const auto& r = rc->runs[i];
+ roaring::internal::bitset_set_range(words, r.value,
+ static_cast<uint32_t>(r.value)
+ r.length + 1U);
+ }
+ constexpr size_t bitset_bytes =
+ roaring::internal::BITSET_CONTAINER_SIZE_IN_WORDS *
sizeof(uint64_t);
+ memcpy(buf, words, bitset_bytes);
+ return bitset_bytes;
+ }
+
+ // From CRoaring `convert_run_optimize()` /
`convert_run_to_efficient_container()`:
+ static size_t write_optimized_container_streaming(const
roaring::internal::container_t* c,
+ uint8_t orig_type, char*
buf, bool* is_run) {
+ // 1. Check whether the container can be optimized (array/bitset ->
run, or run -> array/bitset).
+ // 2. Stream the smallest representation directly into `buf` read-only
and set `*is_run`.
+ switch (orig_type) {
+ case ARRAY_CONTAINER_TYPE: {
+ const auto* ac = static_cast<const
roaring::internal::array_container_t*>(c);
+ const int32_t n_runs =
roaring::internal::array_container_number_of_runs(ac);
+ if
(roaring::internal::run_container_serialized_size_in_bytes(n_runs) <
+
roaring::internal::array_container_serialized_size_in_bytes(ac->cardinality)) {
+ *is_run = true;
+ return write_array_as_run(ac, n_runs, buf);
+ }
+ *is_run = false;
+ return
static_cast<size_t>(roaring::internal::array_container_write(ac, buf));
+ }
+ case BITSET_CONTAINER_TYPE: {
+ const auto* bc = static_cast<const
roaring::internal::bitset_container_t*>(c);
+ const int32_t n_runs =
roaring::internal::bitset_container_number_of_runs(
+ const_cast<roaring::internal::bitset_container_t*>(bc));
+ if
(roaring::internal::run_container_serialized_size_in_bytes(n_runs) <
+
roaring::internal::bitset_container_serialized_size_in_bytes()) {
+ *is_run = true;
+ return write_bitset_as_run(bc, n_runs, buf);
+ }
+ *is_run = false;
+ return
static_cast<size_t>(roaring::internal::bitset_container_write(bc, buf));
+ }
+ case RUN_CONTAINER_TYPE: {
+ const auto* rc = static_cast<const
roaring::internal::run_container_t*>(c);
+ const int32_t card =
roaring::internal::run_container_cardinality(rc);
+ const int32_t run_bytes =
+
roaring::internal::run_container_serialized_size_in_bytes(rc->n_runs);
+ if (card <= roaring::internal::DEFAULT_MAX_SIZE) {
+ if
(roaring::internal::array_container_serialized_size_in_bytes(card) < run_bytes)
{
+ *is_run = false;
+ return write_run_as_array(rc, buf);
+ }
+ } else if
(roaring::internal::bitset_container_serialized_size_in_bytes() < run_bytes) {
+ *is_run = false;
+ return write_run_as_bitset(rc, buf);
+ }
+ *is_run = true;
+ return
static_cast<size_t>(roaring::internal::run_container_write(rc, buf));
+ }
+ default:
+ *is_run = false;
+ DCHECK(false) << "Unsupported CRoaring container type: " <<
static_cast<int>(orig_type);
+ return 0;
+ }
+ }
+
+ // From CRoaring `ra_portable_serialize()` (`roaring_array.c`):
+ // Header layout (fixed cookie 12347 so no pre-scan is needed before
streaming payloads):
+ // [ cookie(2B)=12347 | size-1(2B) | run_bitset(ceil(size/8)B, zeroed) |
+ // (key(2B), card-1(2B)) * size | offsets(4B) * size (if size >= 4,
reserved) ]
+ static char* write_portable_header(const roaring::api::roaring_array_t&
ra, char* buf,
+ uint8_t** run_bitset_out, char**
offsets_out) {
+ const uint32_t size = static_cast<uint32_t>(ra.size);
+ // 1. Always write fixed SERIAL_COOKIE (12347) | ((size - 1) << 16).
+ encode_fixed32_le(
+ reinterpret_cast<uint8_t*>(buf),
+ static_cast<uint32_t>(roaring::internal::SERIAL_COOKIE) |
((size - 1U) << 16));
Review Comment:
[P1] Preserve 64-bit bitmap framing for legacy readers
A BITMAP64 whose first 32-bit group holds four spaced IDs in one array
container now writes 17 bytes for that group in v1. BEs without #71169
remeasure the decoded group as 24 bytes using the canonical 12346 header, so
they start the next group seven bytes late. The same input writes 18 bytes in
v2 while the legacy reader advances 21. This writer feeds both Block exchange
and persisted segments. Preserve the legacy encoding for those readers or
enable the new encoding only once every reader has the consumed-length fix.
[Doris branch-4.1
reader](https://raw.githubusercontent.com/apache/doris/branch-4.1/be/src/core/value/bitmap_value.h)
##########
be/src/core/value/bitmap_value.h:
##########
@@ -1035,6 +1041,351 @@ class Roaring64Map {
*bytes_read = (is_v1 ? 0 : 1) + readbytes;
return r;
}
+
+ // From CRoaring `ra_portable_header_size()` (`roaring_array.c`): header
size with run_bitset.
+ static size_t portable_header_size(int32_t size) {
+ if (size <= 0) {
+ return sizeof(uint32_t) * 2;
+ }
+ const size_t n = static_cast<size_t>(size);
+ // Always include run_bitset since cookie is fixed to SERIAL_COOKIE
(12347).
+ const size_t run_bitset_bytes = (n + 7) / 8;
+ const size_t base = sizeof(uint32_t) + run_bitset_bytes + n *
sizeof(uint32_t);
+ return (size < roaring::internal::NO_OFFSET_THRESHOLD) ? base
+ : (base + n *
sizeof(uint32_t));
+ }
+
+ // Upper bound for fixed-12347 serialization (+ ceil(n/8)-4 bytes when
n>32 without RUN yet).
+ static size_t max_serialized_size(const roaring::Roaring& src, bool is_v1)
{
+ const auto& ra = src.roaring.high_low_container;
+ if (ra.size <= 0 || (ra.flags & ROARING_FLAG_FROZEN) != 0) {
+ return src.getSizeInBytes(is_v1);
+ }
+ size_t portable_max = src.getSizeInBytes(true);
Review Comment:
[P1] Keep output reservations compact for fresh dense bitmaps
A bitmap built by adding IDs in `[0, 2^20)` has 16 bitset containers. This
bound reserves 131,209 bytes per v1 value, although streaming emits 231 bytes.
A 4,096-row Block can therefore allocate over 512 MiB for under 1 MiB of bitmap
payload, even when all rows share one backing bitmap. The merge-base prepass
compacted these containers before reserving output. V2 has the same regression
for fresh bitsets, and the new dense test calls `optimize()` first. Derive the
reservation from the selected container encodings and cover a freshly built
value in both versions.
##########
be/test/core/value/bitmap_value_test.cpp:
##########
@@ -2009,4 +2201,181 @@ TEST(BitmapValueTest,
deserialize_consumes_actual_input_size) {
config::enable_set_in_bitmap_value = old_enable_set;
}
}
-} // namespace doris
+
+TEST(BitmapValueTest, write_matches_copy_optimize) {
+ const auto old_version = config::bitmap_serialize_version;
+
+ auto verify_bitmap = [](const BitmapValue& bitmap, bool expect_exact_bytes
= true) {
+ const auto* backing = bitmap._bitmap.get();
+ BitmapValue expected_bm(bitmap);
+ expected_bm.optimize();
+
+ for (int16_t version : {int16_t {1}, int16_t {2}}) {
+ config::bitmap_serialize_version = version;
+ const size_t raw_size = bitmap.getSizeInBytes();
+ std::string expected;
+ expected_bm.write(expected);
+
+ std::string streamed;
+ bitmap.write(streamed);
+ EXPECT_LE(streamed.size(), raw_size);
+ if (expect_exact_bytes && expected.size() <= raw_size) {
+ EXPECT_EQ(streamed, expected);
+ } else {
+ BitmapValue decoded;
+ ASSERT_TRUE(decoded.deserialize(streamed.data()));
+ EXPECT_EQ(decoded.cardinality(), expected_bm.cardinality());
+ EXPECT_EQ(decoded.to_string(), expected_bm.to_string());
+ }
+
+ for (size_t unalign : {size_t {0}, size_t {1}, size_t {3}}) {
+ std::string buf(unalign + raw_size + 16, '\xaa');
+ const size_t written = bitmap.write_to(buf.data() + unalign);
+ EXPECT_LE(written, raw_size);
+ EXPECT_EQ(written, streamed.size());
+ EXPECT_EQ(std::string_view(buf.data() + unalign, written),
streamed);
+ }
+
+ EXPECT_EQ(bitmap.getSizeInBytes(), raw_size);
+ }
+ EXPECT_EQ(bitmap._bitmap.get(), backing);
+ };
+
+ // Array -> Run (card=3 boundary and full array), plus non-convertible
card=2 pair.
+ {
+ BitmapValue bm;
+ bm.add(10);
+ bm.add(11);
+ bm.add(12);
+ bm.add(100000);
+ bm.add(100001);
+ verify_bitmap(bm);
+ }
+
+ // Bitset -> Run across 64-bit word boundaries and value 65535 (container
end).
+ {
+ BitmapValue bm;
+ for (uint64_t i = 0; i < 65536; ++i) {
+ bm.add(i);
+ }
+ for (uint64_t i = 65536; i < 65536 + 5000; ++i) {
+ if (i % 64 != 0 && i % 128 != 63) {
+ bm.add(i);
+ }
+ }
+ verify_bitmap(bm);
+ }
+
+ // Non-compressible bitset (>= 2048 runs) stays Bitset, mixed with
compressible containers.
+ {
+ BitmapValue bm;
+ for (uint64_t i = 0; i < 9000; i += 2) {
+ bm.add(i);
+ }
+ for (uint64_t i = 65536; i < 65536 + 5000; ++i) {
+ bm.add(i);
+ }
+ verify_bitmap(bm);
+ }
+
+ // Container counts 3 (< NO_OFFSET_THRESHOLD), 4 (== NO_OFFSET_THRESHOLD),
and 33 (5-byte run bitmap).
+ for (uint32_t num_containers : {3U, 4U, 33U}) {
+ BitmapValue bm;
+ for (uint32_t c = 0; c < num_containers; ++c) {
+ const uint64_t base = static_cast<uint64_t>(c) << 16;
+ bm.add(base + 1);
+ bm.add(base + 2);
+ bm.add(base + 3);
+ }
+ verify_bitmap(bm, false);
Review Comment:
[P3] Exercise the Roaring offset-table boundary
The three- and four-container fixtures contain only 9 and 12 IDs, so the
default inline SET path bypasses the Roaring writer. The five-ID array fixture
has the same coverage gap. Force Roaring in these cases and assert the output
representation. Four consecutive IDs per container also keep the three- and
four-container cases in the portable branch under v2, so the test can check
both sides of the offset-table boundary.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]