Skip to content

feat: remove extra Rows from ReusableRows - #23802

Open
ariel-miculas wants to merge 2 commits into
apache:mainfrom
ariel-miculas:remove-extra-rows-in-reusable-rows
Open

feat: remove extra Rows from ReusableRows#23802
ariel-miculas wants to merge 2 commits into
apache:mainfrom
ariel-miculas:remove-extra-rows-in-reusable-rows

Conversation

@ariel-miculas

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

This change reduces the peak allocated memory by removing the unnecessary Rows from being cached.

It will only work after #23619 is merged, until then test_round_robin_tie_breaker_success will fail:

Error: Internal("Rows from RowCursorStream is still in use by consumer") test sorts::sort_preserving_merge::tests::test_round_robin_tie_breaker_success ... FAILED

The failure is triggered by prev_cursors from SortPreservingMergeStream keeping the previous Cursor alive for round robin tie breaking purposes. The optimization from #23619 only keeps the last Row, so there's no longer a need to keep two Rows cached in ReusableRows.

What changes are included in this PR?

Are these changes tested?

There's an existing test that currenly fails with this change and it will pass once #23619 is merged

Are there any user-facing changes?

No

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Jul 22, 2026
@kumarUjjawal

Copy link
Copy Markdown
Contributor

Hi @ariel-miculas Thank you for working on this. Can you look into the ci failures.

@ariel-miculas

Copy link
Copy Markdown
Contributor Author

@kumarUjjawal the CI failures are expected and the tests will pass once #23619 is merged

Fixes: apache#23801

It will only work after apache#23619
is merged, until then test_round_robin_tie_breaker_success will fail:

Error: Internal("Rows from RowCursorStream is still in use by consumer")
test sorts::sort_preserving_merge::tests::test_round_robin_tie_breaker_success ... FAILED

The failure is triggered by prev_cursors from SortPreservingMergeStream
keeping the previous Cursor alive for round robin tie breaking purposes.
The optimization from apache#23619 only keeps the last Row, so there's no
longer a need to keep two Rows cached in ReusableRows.
@ariel-miculas
ariel-miculas force-pushed the remove-extra-rows-in-reusable-rows branch from 7f03b3b to d50a09a Compare August 13, 2026 06:51
@codecov-commenter

codecov-commenter commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.19%. Comparing base (186f96f) to head (12db718).
⚠️ Report is 32 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/physical-plan/src/sorts/stream.rs 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23802      +/-   ##
==========================================
+ Coverage   81.13%   81.19%   +0.05%     
==========================================
  Files        1112     1110       -2     
  Lines      386716   388734    +2018     
  Branches   386716   388734    +2018     
==========================================
+ Hits       313765   315618    +1853     
- Misses      54479    54529      +50     
- Partials    18472    18587     +115     

☔ 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.

@ariel-miculas

Copy link
Copy Markdown
Contributor Author

All the tests have passed, updates to the reservation are not required since the spare Rows was never accounted for, see: #23619 (comment)

@ariel-miculas

Copy link
Copy Markdown
Contributor Author

@kumarUjjawal could you please have a look?

@kumarUjjawal kumarUjjawal 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.

Looks good 👍

@@ -96,36 +96,28 @@ impl FusedStreams {
/// A pair of `Arc<Rows>` that can be reused

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.

isn't this just one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good catch

@Dandandan

Copy link
Copy Markdown
Contributor

run benchmark sort_tpch

@Dandandan

Copy link
Copy Markdown
Contributor

Very nice, looks much better now

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5297366857-1601-g9sl5 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 remove-extra-rows-in-reusable-rows (12db718) to 186f96f (merge-base) diff

Run configuration
run benchmark sort_tpch

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 remove-extra-rows-in-reusable-rows (12db718) to 186f96f (merge-base) diff

Run configuration
run benchmark sort_tpch
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

Comparing HEAD and remove-extra-rows-in-reusable-rows
--------------------
Benchmark sort_tpch1.json
--------------------
┏━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query ┃      HEAD ┃ remove-extra-rows-in-reusable-rows ┃    Change ┃
┡━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Q1    │ 121.00 ms │                          120.69 ms │ no change │
│ Q2    │ 105.24 ms │                          104.64 ms │ no change │
│ Q3    │ 627.36 ms │                          624.08 ms │ no change │
│ Q4    │ 198.29 ms │                          195.18 ms │ no change │
│ Q5    │ 261.29 ms │                          261.01 ms │ no change │
│ Q6    │ 273.42 ms │                          273.23 ms │ no change │
│ Q7    │ 445.03 ms │                          443.69 ms │ no change │
│ Q8    │ 314.70 ms │                          306.67 ms │ no change │
│ Q9    │ 333.39 ms │                          328.60 ms │ no change │
│ Q10   │ 484.08 ms │                          480.82 ms │ no change │
│ Q11   │ 248.63 ms │                          248.20 ms │ no change │
└───────┴───────────┴────────────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                                 ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                                 │ 3412.42ms │
│ Total Time (remove-extra-rows-in-reusable-rows)   │ 3386.81ms │
│ Average Time (HEAD)                               │  310.22ms │
│ Average Time (remove-extra-rows-in-reusable-rows) │  307.89ms │
│ Queries Faster                                    │         0 │
│ Queries Slower                                    │         0 │
│ Queries with No Change                            │        11 │
│ Queries with Failure                              │         0 │
└───────────────────────────────────────────────────┴───────────┘

Distribution per query (min / mean ±stddev / max):

Comparing HEAD and remove-extra-rows-in-reusable-rows
--------------------
Benchmark sort_tpch1.json
--------------------
┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query ┃                               HEAD ┃ remove-extra-rows-in-reusable-rows ┃    Change ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Q1    │  121.00 / 121.73 ±1.00 / 123.72 ms │  120.69 / 121.32 ±0.73 / 122.75 ms │ no change │
│ Q2    │  105.24 / 106.38 ±0.93 / 107.88 ms │  104.64 / 106.00 ±1.06 / 107.27 ms │ no change │
│ Q3    │  627.36 / 629.83 ±1.66 / 632.03 ms │  624.08 / 630.43 ±4.12 / 635.01 ms │ no change │
│ Q4    │  198.29 / 201.07 ±3.62 / 208.23 ms │  195.18 / 196.38 ±0.82 / 197.71 ms │ no change │
│ Q5    │  261.29 / 261.84 ±0.35 / 262.28 ms │  261.01 / 262.53 ±1.88 / 266.23 ms │ no change │
│ Q6    │  273.42 / 274.52 ±1.09 / 276.56 ms │  273.23 / 274.20 ±0.66 / 275.26 ms │ no change │
│ Q7    │  445.03 / 447.04 ±2.27 / 449.83 ms │  443.69 / 445.89 ±1.31 / 447.82 ms │ no change │
│ Q8    │  314.70 / 321.65 ±7.48 / 333.55 ms │  306.67 / 317.29 ±9.80 / 334.37 ms │ no change │
│ Q9    │ 333.39 / 348.53 ±11.60 / 362.39 ms │ 328.60 / 344.16 ±16.30 / 375.60 ms │ no change │
│ Q10   │  484.08 / 486.67 ±2.75 / 490.31 ms │  480.82 / 482.54 ±2.09 / 486.56 ms │ no change │
│ Q11   │ 248.63 / 261.47 ±17.74 / 295.72 ms │ 248.20 / 263.14 ±23.34 / 309.47 ms │ no change │
└───────┴────────────────────────────────────┴────────────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                                 ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                                 │ 3460.75ms │
│ Total Time (remove-extra-rows-in-reusable-rows)   │ 3443.90ms │
│ Average Time (HEAD)                               │  314.61ms │
│ Average Time (remove-extra-rows-in-reusable-rows) │  313.08ms │
│ Queries Faster                                    │         0 │
│ Queries Slower                                    │         0 │
│ Queries with No Change                            │        11 │
│ Queries with Failure                              │         0 │
└───────────────────────────────────────────────────┴───────────┘

Resource Usage

sort_tpch — base (merge-base)

Metric Value
Wall time 20.0s
Peak memory 2.5 GiB
Avg memory 1.1 GiB
CPU user 63.4s
CPU sys 2.6s
Peak spill 0 B

sort_tpch — branch

Metric Value
Wall time 20.0s
Peak memory 2.5 GiB
Avg memory 1.2 GiB
CPU user 63.8s
CPU sys 2.9s
Peak spill 0 B

File an issue against this benchmark runner

@ariel-miculas

ariel-miculas commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

I think we need peak_memory per query to see the improvement.
+ large RecordBatches (like 30-50 MB) and ideally a sort on all the columns

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

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove the buffering of an extra Rows in ReusableRows

5 participants