Skip to content

perf(arrow-ord): Avoid full index materialization for small-limit lexsorts#9991

Open
pchintar wants to merge 2 commits into
apache:mainfrom
pchintar:lexsort-small-limit-topk
Open

perf(arrow-ord): Avoid full index materialization for small-limit lexsorts#9991
pchintar wants to merge 2 commits into
apache:mainfrom
pchintar:lexsort-small-limit-topk

Conversation

@pchintar
Copy link
Copy Markdown
Contributor

@pchintar pchintar commented May 18, 2026

Which issue does this PR close?

Rationale for this change

In arrow-ord/src/sort.rs, lexsort_to_indices currently materializes row indices for the full input before applying the requested lexsort limit.

For example, with:

row_count = 4096
limit = 10

the current implementation still allocates and initializes indices for all 4096 rows:

let mut value_indices = (0..row_count).collect::<Vec<usize>>();

even though only the first 10 sorted indices are returned.

This PR reduces allocation and sorting work for small-limit lexsorts by avoiding full index materialization when the requested limit is a small fraction(limit < 1/10 th of row count) of the input size.

What changes are included in this PR?

This PR adds a bounded top-k heap path for small-limit lexsorts in arrow-ord/src/sort.rs.

The new path:

  • maintains only the current top-k row indices while scanning the input
  • avoids allocating indices for every row
  • sorts only the retained top-k indices before returning the result

The existing partial-sort implementation remains unchanged for larger limits.

To support the bounded heap implementation, this PR also adds:

  • lexsort_topk_fixed

    • Uses the existing fixed-column comparator with the bounded heap path.
  • lexsort_topk

    • Keeps only the current top-k row indices in a bounded max-heap while scanning rows.
  • sift_up_worst_heap

    • Moves a newly inserted row index upward until the heap order is restored.
  • sift_down_worst_heap

    • Moves the current worst retained row index downward after replacement.

Are these changes tested?

Yes.

Existing tests pass:

cargo test -p arrow-ord
cargo test -p arrow --features test_utils

My local Benchmark results from:

cargo bench --package arrow --bench sort_kernel --features test_utils -- "limit"

show improvements for small-limit lexsorts such as limits 10, 100.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added the arrow Changes to the arrow crate label May 18, 2026
@pchintar pchintar force-pushed the lexsort-small-limit-topk branch from b3b7fe3 to eb9a67b Compare May 18, 2026 06:16
@pchintar pchintar force-pushed the lexsort-small-limit-topk branch from eb9a67b to c3fdb35 Compare May 18, 2026 07:04
@pchintar
Copy link
Copy Markdown
Contributor Author

pchintar commented May 18, 2026

@alamb & @etseidl I investigated this integration failure locally since the failure appeared unrelated to the changes in my PR.

My PR only modifies arrow-ord/src/sort.rs, and all local tests pass, including:

  • cargo test -p arrow-ord
  • cargo test -p arrow-integration-testing
  • cargo test -p arrow-integration-test

I also checked the local integration corpus and could not find a generated_binary_view test case there. The failing artifact appears to be generated dynamically during the CI cross-language integration step.

I also noticed the same binary_view Rust producing, .NET consuming failures on many commits made within these 2 days, so this could be a flaky test case perhaps?

@alamb
Copy link
Copy Markdown
Contributor

alamb commented May 22, 2026

run benchmarks sort_kernel

@alamb
Copy link
Copy Markdown
Contributor

alamb commented May 22, 2026

I also noticed the same binary_view Rust producing, .NET consuming failures on many commits made within these 2 days, so this could be a flaky test case perhaps?

I think it was an error in .NET:

@adriangbot
Copy link
Copy Markdown

🤖 Arrow criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c4518835065-284-nqdv8 6.12.68+ #1 SMP Wed Apr 1 02:23:28 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 lexsort-small-limit-topk (b7bdb86) to 4b80f0e (merge-base) diff
BENCH_NAME=sort_kernel
BENCH_COMMAND=cargo bench --features=arrow,async,test_common,experimental,object_store --bench sort_kernel
BENCH_FILTER=
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

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

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                                                   lexsort-small-limit-topk               main
-----                                                   ------------------------               ----
lexsort (bool, bool) 2^12                               1.02     81.0±0.67µs        ? ?/sec    1.00     79.6±0.89µs        ? ?/sec
lexsort (bool, bool) nulls 2^12                         1.03     94.8±2.94µs        ? ?/sec    1.00     92.4±0.41µs        ? ?/sec
lexsort (f32, f32) 2^10                                 1.02     31.5±0.10µs        ? ?/sec    1.00     30.9±0.08µs        ? ?/sec
lexsort (f32, f32) 2^12                                 1.02    253.3±0.73µs        ? ?/sec    1.00    247.8±0.92µs        ? ?/sec
lexsort (f32, f32) 2^12 limit 10                        1.00     12.0±0.03µs        ? ?/sec    2.69     32.3±0.18µs        ? ?/sec
lexsort (f32, f32) 2^12 limit 100                       1.00     23.9±0.06µs        ? ?/sec    1.43     34.2±0.20µs        ? ?/sec
lexsort (f32, f32) 2^12 limit 1000                      1.00     58.5±0.34µs        ? ?/sec    1.01     59.3±0.15µs        ? ?/sec
lexsort (f32, f32) 2^12 limit 2^12                      1.02    253.6±0.79µs        ? ?/sec    1.00    247.6±0.48µs        ? ?/sec
lexsort (f32, f32) nulls 2^10                           1.01     33.0±0.05µs        ? ?/sec    1.00     32.8±0.04µs        ? ?/sec
lexsort (f32, f32) nulls 2^12                           1.00    174.9±0.51µs        ? ?/sec    1.07    187.6±0.71µs        ? ?/sec
lexsort (f32, f32) nulls 2^12 limit 10                  1.00     21.8±0.80µs        ? ?/sec    1.96     42.6±0.09µs        ? ?/sec
lexsort (f32, f32) nulls 2^12 limit 100                 1.00     30.0±1.52µs        ? ?/sec    1.44     43.1±0.09µs        ? ?/sec
lexsort (f32, f32) nulls 2^12 limit 1000                1.00     48.6±0.12µs        ? ?/sec    1.01     49.3±0.19µs        ? ?/sec
lexsort (f32, f32) nulls 2^12 limit 2^12                1.00    174.8±0.49µs        ? ?/sec    1.08    189.2±1.28µs        ? ?/sec
rank f32 2^12                                           1.00     47.1±0.03µs        ? ?/sec    1.00     47.0±0.03µs        ? ?/sec
rank f32 nulls 2^12                                     1.00     23.5±0.02µs        ? ?/sec    1.00     23.5±0.02µs        ? ?/sec
rank string[10] 2^12                                    1.00    161.7±0.89µs        ? ?/sec    1.00    160.9±0.13µs        ? ?/sec
rank string[10] nulls 2^12                              1.00     75.1±0.10µs        ? ?/sec    1.02     76.5±0.92µs        ? ?/sec
sort f32 2^12                                           1.00     42.6±0.01µs        ? ?/sec    1.00     42.7±0.03µs        ? ?/sec
sort f32 nulls 2^12                                     1.00     21.6±0.01µs        ? ?/sec    1.00     21.6±0.01µs        ? ?/sec
sort f32 nulls to indices 2^12                          1.00     25.8±0.07µs        ? ?/sec    1.00     25.8±0.02µs        ? ?/sec
sort f32 to indices 2^12                                1.00     49.3±0.03µs        ? ?/sec    1.00     49.4±0.03µs        ? ?/sec
sort i32 2^10                                           1.00      6.4±0.01µs        ? ?/sec    1.00      6.5±0.02µs        ? ?/sec
sort i32 2^12                                           1.00     32.0±0.02µs        ? ?/sec    1.00     32.1±0.08µs        ? ?/sec
sort i32 nulls 2^10                                     1.00      3.8±0.01µs        ? ?/sec    1.00      3.8±0.01µs        ? ?/sec
sort i32 nulls 2^12                                     1.00     16.3±0.01µs        ? ?/sec    1.00     16.3±0.01µs        ? ?/sec
sort i32 nulls to indices 2^10                          1.00      5.3±0.01µs        ? ?/sec    1.00      5.3±0.01µs        ? ?/sec
sort i32 nulls to indices 2^12                          1.00     22.6±0.01µs        ? ?/sec    1.00     22.7±0.03µs        ? ?/sec
sort i32 to indices 2^10                                1.00      9.2±0.01µs        ? ?/sec    1.00      9.2±0.01µs        ? ?/sec
sort i32 to indices 2^12                                1.00     42.7±0.07µs        ? ?/sec    1.00     42.7±0.07µs        ? ?/sec
sort primitive run 2^12                                 1.00      5.0±0.00µs        ? ?/sec    1.00      5.0±0.01µs        ? ?/sec
sort primitive run to indices 2^12                      1.02      5.3±0.01µs        ? ?/sec    1.00      5.1±0.00µs        ? ?/sec
sort string[0-100] nulls to indices 2^12                1.00     27.6±0.07µs        ? ?/sec    1.00     27.7±0.08µs        ? ?/sec
sort string[0-100] to indices 2^12                      1.00     52.9±0.15µs        ? ?/sec    1.00     52.8±0.15µs        ? ?/sec
sort string[0-10] nulls to indices 2^12                 1.00     29.6±0.06µs        ? ?/sec    1.01     29.8±0.07µs        ? ?/sec
sort string[0-10] to indices 2^12                       1.01     57.3±0.14µs        ? ?/sec    1.00     56.9±0.13µs        ? ?/sec
sort string[0-400] nulls to indices 2^12                1.00     28.2±0.07µs        ? ?/sec    1.00     28.2±0.07µs        ? ?/sec
sort string[0-400] to indices 2^12                      1.00     53.4±0.15µs        ? ?/sec    1.00     53.5±0.16µs        ? ?/sec
sort string[1000] nulls to indices 2^12                 1.00     28.9±0.08µs        ? ?/sec    1.00     28.8±0.09µs        ? ?/sec
sort string[1000] to indices 2^12                       1.00     53.2±0.15µs        ? ?/sec    1.00     53.1±0.16µs        ? ?/sec
sort string[100] nulls to indices 2^12                  1.00     28.1±0.07µs        ? ?/sec    1.00     28.0±0.07µs        ? ?/sec
sort string[100] to indices 2^12                        1.00     53.2±0.16µs        ? ?/sec    1.00     53.0±0.16µs        ? ?/sec
sort string[10] dict nulls to indices 2^12              1.00     98.8±0.25µs        ? ?/sec    1.01     99.8±0.11µs        ? ?/sec
sort string[10] dict to indices 2^12                    1.00    204.4±0.34µs        ? ?/sec    1.01    206.9±0.28µs        ? ?/sec
sort string[10] nulls to indices 2^12                   1.00     28.6±0.08µs        ? ?/sec    1.00     28.6±0.08µs        ? ?/sec
sort string[10] to indices 2^12                         1.00     52.6±0.14µs        ? ?/sec    1.00     52.4±0.23µs        ? ?/sec
sort string_view[0-400] nulls to indices 2^12           1.00     36.3±0.02µs        ? ?/sec    1.00     36.4±0.03µs        ? ?/sec
sort string_view[0-400] to indices 2^12                 1.00     72.9±0.18µs        ? ?/sec    1.13     82.1±0.16µs        ? ?/sec
sort string_view[10] nulls to indices 2^12              1.11     36.1±0.10µs        ? ?/sec    1.00     32.6±0.03µs        ? ?/sec
sort string_view[10] to indices 2^12                    1.12     69.7±0.12µs        ? ?/sec    1.00     62.2±0.05µs        ? ?/sec
sort string_view_inlined[0-12] nulls to indices 2^12    1.00     31.9±0.02µs        ? ?/sec    1.10     35.0±0.10µs        ? ?/sec
sort string_view_inlined[0-12] to indices 2^12          1.00     67.3±0.12µs        ? ?/sec    1.00     67.4±0.23µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 500.1s
Peak memory 3.1 GiB
Avg memory 3.0 GiB
CPU user 497.8s
CPU sys 0.8s
Peak spill 0 B

branch

Metric Value
Wall time 500.1s
Peak memory 3.0 GiB
Avg memory 3.0 GiB
CPU user 495.4s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

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

Labels

arrow Changes to the arrow crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(arrow-ord): Avoid full index materialization for small-limit lexsorts

4 participants