Skip to content

perf: avoid scanning array_agg state in size - #24097

Open
lyne7-sc wants to merge 1 commit into
apache:mainfrom
lyne7-sc:perf/array-agg-constant-time-size
Open

perf: avoid scanning array_agg state in size#24097
lyne7-sc wants to merge 1 commit into
apache:mainfrom
lyne7-sc:perf/array-agg-constant-time-size

Conversation

@lyne7-sc

@lyne7-sc lyne7-sc commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

ArrayAggAccumulator::size() and ArrayAggGroupsAccumulator::size() scanned all retained batches on every call. Since aggregate operators call size() for each input batch, memory accounting became increasingly expensive as the accumulated state grew.

What changes are included in this PR?

Track retained array and entry memory incrementally when accumulator state is updated, retracted, cleared, merged, or partially emitted.

This avoids repeatedly scanning accumulated state in size() while preserving the existing memory estimation behavior.

Are these changes tested?

Yes. Unit tests cover incremental memory tracking.

Are there any user-facing changes?

No. This only improves internal memory accounting.

@github-actions github-actions Bot added the functions Changes to functions implementation label Aug 5, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.14286% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.90%. Comparing base (31ffab1) to head (5199f58).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/array_agg.rs 87.14% 1 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24097      +/-   ##
==========================================
- Coverage   80.90%   80.90%   -0.01%     
==========================================
  Files        1102     1102              
  Lines      376807   377137     +330     
  Branches   376807   377137     +330     
==========================================
+ Hits       304874   305140     +266     
- Misses      53775    53793      +18     
- Partials    18158    18204      +46     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@neilconway

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5195800441-1458-mnqfd 6.12.85+ #1 SMP Wed Jun 17 20:31:55 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array-agg-constant-time-size (5199f58) to 31ffab1 (merge-base) diff

Run configuration
run benchmark array_agg

Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing perf/array-agg-constant-time-size (5199f58) to 31ffab1 (merge-base) diff

Run configuration
run benchmark array_agg
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array-agg-constant-time-size
-----                                                                              ----                                   ---------------------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.00    496.8±5.75µs        ? ?/sec    2.14   1062.5±8.98µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.02      4.7±0.00µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.00    501.9±5.92µs        ? ?/sec    2.11   1060.3±8.96µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.00    500.1±5.41µs        ? ?/sec    2.13   1065.7±9.56µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.00    501.9±5.79µs        ? ?/sec    2.13   1067.6±9.63µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.00    214.2±2.31µs        ? ?/sec    2.16    463.3±4.89µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.02      4.5±0.00µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    216.4±2.48µs        ? ?/sec    2.14    462.6±4.34µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.00    217.9±2.50µs        ? ?/sec    2.13    463.3±3.98µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.00    215.3±2.49µs        ? ?/sec    2.16    464.4±4.29µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.00     73.4±2.27ns        ? ?/sec    1.09     79.7±4.52ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.00     73.0±2.52ns        ? ?/sec    1.02     74.8±1.20ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     81.8±1.23ns        ? ?/sec    2.24    183.5±2.73ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             1.00    665.6±3.93µs        ? ?/sec    1.00    667.7±3.42µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             1.00    165.1±1.02µs        ? ?/sec    1.00    164.8±1.01µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 310.1s
Peak memory 30.1 MiB
Avg memory 12.7 MiB
CPU user 191.1s
CPU sys 0.1s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 305.1s
Peak memory 30.4 MiB
Avg memory 13.6 MiB
CPU user 193.2s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@neilconway

neilconway commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

@lyne7-sc Looks like the benchmarks show some significant regressions. I'm guessing this is because they don't call size() directly, and a more representative benchmark would show net improvements? At minimum it would be good to add a benchmark for the workload that this PR is intended to optimize.

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

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants