Skip to content

[opt](be) Cache segment row bitmap cardinality - #66742

Open
Mryange wants to merge 1 commit into
apache:masterfrom
Mryange:optimize-segment-row-bitmap-cardinality
Open

[opt](be) Cache segment row bitmap cardinality#66742
Mryange wants to merge 1 commit into
apache:masterfrom
Mryange:optimize-segment-row-bitmap-cardinality

Conversation

@Mryange

@Mryange Mryange commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

SegmentIterator recalculates the row bitmap cardinality for every output batch. For large segments, this repeatedly traverses all Roaring bitmap containers and adds significant scan overhead. This change caches the cardinality after all bitmap filters are applied during lazy initialization and reuses it for column reservation and batch reads.

Release note

None

Check List (For Author)

  • Test

    • 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
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

### What problem does this PR solve?

Issue Number: N/A

Problem Summary: SegmentIterator recalculates the row bitmap cardinality for every output batch. Large row bitmaps require traversing many Roaring containers for each calculation. Cache the cardinality after all lazy-init filters are applied and reuse it for column reservation and batch reads.

### Release note

None

### Check List (For Author)

- Test: No build or tests were run.
    - clang-format 16 dry-run passed
    - git diff --check passed
- Behavior changed: No. This only removes redundant cardinality calculations.
- Does this need documentation: No.
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@Mryange

Mryange commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review opinion: approval; no blocking correctness issue found.

The change caches the final scan bitmap cardinality after all bitmap-producing filters and reuses that identical value for initial reservation and per-batch read sizing. The placement and one-shot iterator lifecycle preserve the prior semantics while avoiding repeated Roaring cardinality traversal.

Critical checkpoint conclusions:

  • Goal and proof: the implementation accomplishes the stated performance goal at both repeated call sites. Existing scan behavior tests exercise adjacent LIMIT/reverse paths, but this PR adds no direct cache-equivalence or performance test.
  • Scope and clarity: the change is small and focused to SegmentIterator; the member name, initialization, and placement communicate the invariant clearly.
  • Concurrency: no new concurrent state is introduced. A SegmentIterator is scan-local and consumed through its existing one-shot iterator path; the cached member follows the same ownership as _row_bitmap and requires no new lock.
  • Lifecycle and static initialization: all bitmap mutation paths complete before cache publication. Failed lazy initialization retries rebuild the bitmap, cache, and range iterator because _lazy_inited remains false; successful initialization is one-shot. No static/global initialization is involved.
  • Configuration: no configuration item is added or changed.
  • Compatibility: no function symbol, persisted state, storage format, or FE-BE protocol changes are introduced, so rolling-upgrade compatibility is unaffected.
  • Parallel and special paths: condition-cache pruning, key/column/inverted-index filtering, delete bitmaps, explicit row ranges, BM25 score filtering, ANN range/TOP-N, forward/reverse scans, adaptive batch sizing, pushed LIMIT, empty bitmaps, and EOF were traced. No post-publication bitmap mutation or stale-cache reuse was found.
  • Tests and results: no test files or expected-result files are changed. Existing coverage includes relevant LIMIT and reverse-order semantics; no builds or tests were run in this review-only environment, as required by the review bundle.
  • Observability: this internal reuse does not add a failure mode or distributed operation that needs new logging, metrics, or tracing.
  • Transactions, persistence, and data writes: not applicable; the change only affects read-side batch sizing and does not alter visibility, delete-bitmap versions, atomicity, or crash recovery.
  • Cross-process variables: no new value is transmitted between FE and BE.
  • Performance and memory safety: the optimization removes repeated full cardinality calculations. The cached uint64_t matches Roaring::cardinality(); the existing cast_set<uint32_t> boundary is unchanged, adaptive reads remain capped by _initial_block_row_max, and _block_rowids remains sized to that ceiling.
  • Other issues: none substantiated after the full-scope and risk-focused review sweeps.

User focus: no additional focus was supplied; the entire two-file PR was reviewed.

@Mryange

Mryange commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 100.00% (3/3) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 60.32% (26483/43903)
Line Coverage 44.76% (270217/603766)
Region Coverage 40.56% (215809/532100)
Branch Coverage 41.98% (99345/236666)

@Mryange

Mryange commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 17277 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit b825b8cc773bf0d6957e14cb8b3fc8170d2b8031, data reload: false

------ Round 1 ----------------------------------
orders	Doris	NULL	NULL	0	0	0	NULL	0	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	17675	3062	3054	3054
q2	q3	10970	873	502	502
q4	4693	255	207	207
q5	7754	583	426	426
q6	139	118	94	94
q7	546	500	394	394
q8	9427	925	932	925
q9	3909	2423	2386	2386
q10	6577	876	708	708
q11	580	261	247	247
q12	846	403	338	338
q13	17958	1571	1164	1164
q14	162	146	137	137
q15	q16	488	398	376	376
q17	871	752	792	752
q18	3258	2263	2245	2245
q19	1204	881	742	742
q20	687	517	474	474
q21	5376	1872	1897	1872
q22	334	276	234	234
Total cold run time: 93454 ms
Total hot run time: 17277 ms

----- Round 2, with runtime_filter_mode=off -----
orders	Doris	NULL	NULL	150000000	42	6422171781	NULL	22778155	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	3432	3363	3346	3346
q2	q3	2226	2389	2241	2241
q4	1205	1178	893	893
q5	2188	2109	2127	2109
q6	171	120	96	96
q7	1036	916	863	863
q8	1618	1440	1423	1423
q9	3162	3134	3110	3110
q10	1905	1790	1643	1643
q11	372	275	258	258
q12	446	426	341	341
q13	1504	1533	1164	1164
q14	177	179	166	166
q15	q16	407	398	356	356
q17	1044	1041	1037	1037
q18	4997	4431	4785	4431
q19	866	849	827	827
q20	1002	1075	815	815
q21	3569	3023	3192	3023
q22	375	337	316	316
Total cold run time: 31702 ms
Total hot run time: 28458 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 81483 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit b825b8cc773bf0d6957e14cb8b3fc8170d2b8031, data reload: false

query5	4229	430	338	338
query6	433	162	154	154
query7	4896	493	274	274
query8	417	131	132	131
query9	8712	2956	2951	2951
query10	468	263	219	219
query11	5427	1036	920	920
query12	123	73	72	72
query13	1204	444	334	334
query14	6019	2238	2106	2106
query14_1	2022	1999	1988	1988
query15	174	121	115	115
query16	3170	384	370	370
query17	854	456	370	370
query18	2117	327	244	244
query19	300	147	119	119
query20	72	69	72	69
query21	901	121	106	106
query22	5714	5393	5367	5367
query23	6694	6279	6129	6129
query23_1	6149	5974	6156	5974
query24	7312	1105	816	816
query24_1	769	792	801	792
query25	456	327	266	266
query26	1262	296	167	167
query27	2621	441	289	289
query28	4598	1540	1512	1512
query29	976	455	380	380
query30	453	183	152	152
query31	865	440	358	358
query32	104	52	51	51
query33	512	239	186	186
query34	1031	881	505	505
query35	438	557	362	362
query36	591	584	547	547
query37	129	84	73	73
query38	1051	859	833	833
query39	512	481	465	465
query39_1	496	506	486	486
query40	286	125	110	110
query41	52	52	52	52
query42	81	78	81	78
query43	251	247	226	226
query44	
query45	105	103	96	96
query46	798	866	545	545
query47	815	814	750	750
query48	315	309	240	240
query49	660	234	189	189
query50	904	334	270	270
query51	8224	8099	8190	8099
query52	75	73	71	71
query53	205	207	160	160
query54	245	175	159	159
query55	83	60	55	55
query56	223	218	212	212
query57	834	671	618	618
query58	219	193	182	182
query59	1192	1197	1067	1067
query60	261	194	196	194
query61	111	113	117	113
query62	374	202	177	177
query63	188	159	153	153
query64	2267	641	548	548
query65	
query66	1848	302	249	249
query67	11834	9874	9943	9874
query68	
query69	611	235	214	214
query70	645	626	632	626
query71	300	262	251	251
query72	2801	1896	1795	1795
query73	719	608	352	352
query74	1587	1276	1168	1168
query75	1263	1167	1042	1042
query76	1926	726	573	573
query77	481	275	223	223
query78	3958	3618	3211	3211
query79	3238	785	587	587
query80	1599	394	369	369
query81	552	204	176	176
query82	657	141	104	104
query83	329	272	240	240
query84	
query85	980	466	424	424
query86	529	176	174	174
query87	1030	1020	925	925
query88	3521	2178	2131	2131
query89	352	238	214	214
query90	2229	153	147	147
query91	152	140	122	122
query92	71	48	44	44
query93	3458	1200	775	775
query94	733	250	271	250
query95	650	358	330	330
query96	822	600	290	290
query97	1158	1091	1022	1022
query98	187	139	134	134
query99	551	354	304	304
Total cold run time: 186557 ms
Total hot run time: 81483 ms

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 62.09% (28853/46472)
Line Coverage 47.13% (301617/639993)
Region Coverage 42.87% (243877/568851)
Branch Coverage 44.42% (113431/255335)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 100% (0/0) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 74.92% (33727/45015)
Line Coverage 59.86% (378707/632653)
Region Coverage 56.03% (317521/566723)
Branch Coverage 56.87% (144808/254608)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants