Mryange opened a new pull request, #62668:
URL: https://github.com/apache/doris/pull/62668
## Problem
`RetentionState` used a `uint8_t events[32]` array (32 bytes) to track up to
32 event flags,
one byte per event. This caused three independent inefficiencies:
1. **`merge()`**: a 32-iteration loop performing 32 byte-level `|=`
operations instead of a single 32-bit OR.
2. **`write()` / `read()`**: manual bit-packing/unpacking loops (~40
instructions each) plus VarInt encoding (1–10 variable bytes), even though the
data is always a fixed 32-bit bitmask.
3. **`reset()`**: clearing 32 bytes vs clearing a single `uint32_t`.
For high-cardinality `GROUP BY` (e.g. millions of distinct users), the hash
table stores one
`RetentionState` per bucket. At 32 bytes/state only 2 states fit per 64-byte
cache line; the
random-access cache miss rate is 8× worse than necessary. In distributed
scenarios the
serialize/deserialize overhead also dominates.
## Solution
Add a new `retention_v2` function that replaces `uint8_t events[32]` with a
single `uint32_t events`
bitmask (4 bytes, one bit per event). The original `retention` function is
kept unchanged for
backward compatibility (see [Upgrade compatibility](#upgrade-compatibility)
below).
State size: 32 bytes → **4 bytes** (8×), making the hash table 8× more
cache-friendly.
## Benchmark
Machine: 192 × 3800 MHz CPUs, L3 99840 KiB × 2
Dataset: 10,000 states, 8 events set per state (Release build)
| Operation | Old (mean) | New (mean) | Speedup |
|---|---|---|---|
| merge (10K states) | 3438 ns | 260 ns | **13.2×** |
| serialize (10K states) | 169564 ns | 28590 ns | **5.9×** |
| deserialize (10K states) | 106163 ns | 7689 ns | **13.8×** |
| reset (10K states) | 6585 ns | 245 ns | **26.9×** |
### Raw benchmark output
```
----------------------------------------------------------------------------------------------
Benchmark Time
CPU Iterations
----------------------------------------------------------------------------------------------
doris::BM_RetentionMerge_Old/repeats:5_mean 3438 ns
3415 ns 5
doris::BM_RetentionMerge_New/repeats:5_mean 260 ns
259 ns 5
doris::BM_RetentionSerialize_Old/repeats:5_mean 169564 ns
168393 ns 5
doris::BM_RetentionSerialize_New/repeats:5_mean 28590 ns
28368 ns 5
doris::BM_RetentionDeserialize_Old/repeats:5_mean 106163 ns
105555 ns 5
doris::BM_RetentionDeserialize_New/repeats:5_mean 7689 ns
7644 ns 5
doris::BM_RetentionReset_Old/repeats:5_mean 6585 ns
6545 ns 5
doris::BM_RetentionReset_New/repeats:5_mean 245 ns
243 ns 5
```
## Upgrade compatibility
Because the serialization format changes (4-byte fixed binary vs. VarInt), a
rolling upgrade
would cause v1 and v2 BE nodes to produce incompatible intermediate
aggregate data.
To preserve backward compatibility, the optimization is introduced as a
**new function
`retention_v2`** rather than replacing the existing `retention`:
| Function | State | Serialization | Notes |
|---|---|---|---|
| `retention` (v1) | `uint8_t[32]` | VarInt | unchanged, fully
backward-compatible |
| `retention_v2` (new) | `uint32_t` | fixed 4 bytes | optimized, use for new
workloads |
New workloads should use `retention_v2` to benefit from the performance
improvements.
The original `retention` function is unmodified and remains available.
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
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]