linrrzqqq opened a new pull request, #70774:
URL: https://github.com/apache/doris/pull/70774

   ### Background
   
   Doris stores multi-value bitmaps in CRoaring (`array`, `bitset`, `run` 
containers) and serializes them into portable Roaring format for shuffle 
exchange, storage, JSONB/Arrow/ORC, and functions like `bitmap_to_base64`.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #71169
   PR Type: Performance
   
   Problem Summary:
   
   Previously, bitmap sizing and serialization called `runOptimize()` to 
compact containers before writing, which mutated `BitmapValue` in place on 
read-only paths (unsafe on shared `ColumnBitmap` columns) and allocated/rewrote 
intermediate containers.
   This PR makes sizing and serialization strictly read-only by 
**stream-compacting each container directly into the output buffer in a single 
pass** using a fixed portable header (cookie `12347`), with `getSizeInBytes()` 
returning a cheap upper bound and `write_to()` returning the exact bytes 
written.
   
   ```text
   Before (master):
     getSizeInBytes() -> runOptimize() mutates containers in place (data race + 
container reallocs)
     write_to(buf)    -> writes mutated containers
   
   After (this PR):
     getSizeInBytes() -> read-only upper bound (no mutation, no container 
conversion)
     write_to(buf)    -> 1. write fixed cookie-12347 header (run_bitset zeroed, 
offsets reserved)
                         2. stream each container directly into buf 
(array/bitset <-> run on the fly)
                         3. backfill offsets[i] and run_bitset[i] in place -> 
return exact written_bytes
   ```
   
   ### Details
   
   #### 1. Single-Pass Streaming Compaction & Fixed `12347` Header
   
   Standard CRoaring portable serialization picks cookie `12346` (no 
`run_bitset`) or `12347` (with `run_bitset`) by checking containers beforehand. 
To stream in one pass without pre-scanning or mutating containers, this PR 
always emits cookie `12347` and reserves `run_bitset` and `offsets` up front:
   
   ```text
   Portable wire layout written by streaming serializer (cookie 12347):
   
+--------------+----------+------------------+----------------------+-----------------------+--------------------+
   | cookie=12347 | size - 1 | run_bitset       | (key, card-1) * size | 
offsets (if size>=4)  | container payloads |
   |     2 B      |   2 B    | ceil(size / 8) B |      4 * size B      |      4 
* size B       | streamed directly  |
   
+--------------+----------+------------------+----------------------+-----------------------+--------------------+
                                   ^                                            
   ^
                                   |  set bit i when container i                
   |  write (payload - buf)
                                   |  streams out as a run container            
   |  before container i
   ```
   
   Key behavioral / API changes:
   - **Container compaction on the fly**: For each container, pick the smallest 
representation (`array`, `bitset`, or `run`) and stream its bytes directly to 
`buf` without modifying the source container or allocating a temporary 
container.
   - **Fixed cookie `12347` header**: Always reserves `ceil(size / 8)` bytes 
for `run_bitset` even when no container ends up as a run container. This is 
standard CRoaring portable format and is decoded identically by existing 
deserializers (with exact read-size calculation from input headers fixed in 
#71169).
   - **V2 (`tag=1` flat vs `tag=2` portable) selection**: Short-circuits to 
flat `uint32_t[]` (`tag=1`) when flat is guaranteed smaller than the portable 
header + minimum payload; otherwise streams portable directly into `buf + 1` 
and falls back to flat only if flat is smaller.
   - **Upper-bound sizing + exact write return**: `getSizeInBytes()` returns a 
read-only upper bound (`src.getSizeInBytes` + potential `run_bitset` bytes); 
`write_to(char*)` returns the actual `written_bytes`. Serialization callers now 
frame/resize outputs using the returned length (or `write(std::string&)`).
   
   #### 2. Benchmark Results
   
   Measured on Doris BE Release build (`master` `b42159f05de` vs. this PR 
`29510457dd8`), 2 symmetric reverse-order rounds × 7 measured runs (`N = 14` 
samples/case, identical SQL results on all cases):
   
   | Case | `master` (median) | This PR (median) | Change |
   |---|---:|---:|---:|
   | `bitmap_shuffle_join` | 1820 ms | 1275 ms | -29.95% |
   | `bitmap_to_base64_large` | 2065 ms | 1530 ms | -25.91% |
   | `explode_bitmap_basic` | 2705 ms | 2080 ms | -23.11% |
   | `explode_bitmap_function` | 1380 ms | 1080 ms | -21.74% |
   | `bitmap_union_to_base64` | 1480 ms | 1370 ms | -7.43% |
   | `bitmap_agg_to_base64` | 150 ms | 140 ms | -6.67% |
   | 15-case total median / geomean | 23260 ms | 20880 ms | -10.23% / -9.63% |
   
   Query Profile on `bitmap_shuffle_join` (3 shuffle exchanges: 27.42 MB + 
27.42 MB + 45.78 MB):
   
   ```text
   Total Exchange SerializeBatchTime:
     master : 162.1 ms + 171.1 ms + 509.3 ms = 842.5 ms
     this PR:  80.4 ms +  61.5 ms + 129.7 ms = 271.6 ms (-67.8%)
   ```
   
   ### Testing
   
   - Verified BE Release build and identical SQL outputs across all 46 bitmap 
operator cases against `master` (`b42159f05de`), plus 14-sample symmetric 
benchmark and Query Profile comparison on 15 focus cases.
   - Unit tests for concurrency, frame bounds, and streaming serialization 
equivalence are included; CI BE UT / regression suites pending trigger.
   
   ### Release note
   
   Bitmap serialization is now strictly read-only with single-pass streaming 
compaction, improving performance for bitmap shuffle, `bitmap_to_base64`, and 
`explode_bitmap` workloads.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (portable serialization always uses standard 
CRoaring cookie `12347` header; `getSizeInBytes()` is an upper bound and 
`write_to()` returns exact written bytes)
   - Does this need documentation: No
   - Pre-submission status: Partially verified (BE Release build + 46-case SQL 
equivalence + 14-sample benchmark verified; waiting on CI BE UT / regression 
suites)
   
   ### AI Disclosure
   
   - Tool: Antigravity
   - Generated: Streaming compaction helpers, benchmark scripts, and PR 
description draft
   - Human review: Reviewed serialization format invariants, design, benchmark 
methodology, and final commit diff


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

Reply via email to