@@ -452,91 +452,93 @@ fn create_list_of_struct_array(n: usize, list_len: usize, null_density: f32) ->
452452 ) )
453453}
454454
455- /// Head-to-head for the coalesce-peers rewrite: N independent primitive
456- /// `first_value` accumulators (the pre -rewrite plan) vs one struct-valued
457- /// accumulator carrying the same N columns (the post-rewrite plan). Uses the
458- /// same worst-case ordering as [`update_bench`] so every row forces an
459- /// ordering comparison in every accumulator .
460- #[ expect( clippy:: needless_pass_by_value ) ]
461- fn coalesce_comparison_bench (
455+ /// Drive one coalesce-peers variant: `separate xN` (pre-rewrite, one primitive
456+ /// accumulator per column) vs `coalesced struct` (post -rewrite, one struct
457+ /// accumulator). Each accumulator is primed with `worst_ord` (the always-losing
458+ /// ordering value), then fed one `ord` per iteration from `iter_ords`. Varying
459+ /// `iter_ords` selects which path dominates — see [`coalesce_comparison_bench`] .
460+ #[ expect( clippy:: too_many_arguments ) ]
461+ fn run_coalesce_variant (
462462 c : & mut Criterion ,
463463 name : & str ,
464- column_values : Vec < ArrayRef > ,
465- struct_values : ArrayRef ,
466- ord : ArrayRef ,
464+ variant : & str ,
465+ column_values : & [ ArrayRef ] ,
466+ struct_values : & ArrayRef ,
467+ worst_ord : & ArrayRef ,
468+ iter_ords : & [ ArrayRef ] ,
469+ group_indices : & [ usize ] ,
467470 num_groups : usize ,
468471) {
469- let n = ord. len ( ) ;
470- let group_indices: Vec < usize > = ( 0 ..n) . map ( |i| i % num_groups) . collect ( ) ;
471- let worst_ord: ArrayRef = Arc :: new ( Int64Array :: from ( vec ! [ i64 :: MAX ; n] ) ) ;
472-
473472 // Pre-rewrite: one accumulator per column.
474- c. bench_function ( & format ! ( "{name} separate x{}" , column_values. len( ) ) , |b| {
475- b. iter_batched (
476- || {
477- column_values
478- . iter ( )
479- . map ( |values| {
480- let mut acc = prepare_typed_groups_accumulator (
481- true ,
482- values. data_type ( ) . clone ( ) ,
483- ) ;
484- acc. update_batch (
485- & [ Arc :: clone ( values) , Arc :: clone ( & worst_ord) ] ,
486- & group_indices,
487- None ,
488- num_groups,
489- )
490- . unwrap ( ) ;
491- acc
492- } )
493- . collect :: < Vec < _ > > ( )
494- } ,
495- |mut accumulators| {
496- for _ in 0 ..100 {
497- for ( acc, values) in accumulators. iter_mut ( ) . zip ( & column_values) {
498- #[ expect( clippy:: unit_arg) ]
499- black_box (
473+ c. bench_function (
474+ & format ! ( "{name} separate x{} {variant}" , column_values. len( ) ) ,
475+ |b| {
476+ b. iter_batched (
477+ || {
478+ column_values
479+ . iter ( )
480+ . map ( |values| {
481+ let mut acc = prepare_typed_groups_accumulator (
482+ true ,
483+ values. data_type ( ) . clone ( ) ,
484+ ) ;
500485 acc. update_batch (
501- & [ Arc :: clone ( values) , Arc :: clone ( & ord ) ] ,
502- & group_indices,
486+ & [ Arc :: clone ( values) , Arc :: clone ( worst_ord ) ] ,
487+ group_indices,
503488 None ,
504489 num_groups,
505490 )
506- . unwrap ( ) ,
507- ) ;
491+ . unwrap ( ) ;
492+ acc
493+ } )
494+ . collect :: < Vec < _ > > ( )
495+ } ,
496+ |mut accumulators| {
497+ for ord in iter_ords {
498+ for ( acc, values) in accumulators. iter_mut ( ) . zip ( column_values) {
499+ #[ expect( clippy:: unit_arg) ]
500+ black_box (
501+ acc. update_batch (
502+ & [ Arc :: clone ( values) , Arc :: clone ( ord) ] ,
503+ group_indices,
504+ None ,
505+ num_groups,
506+ )
507+ . unwrap ( ) ,
508+ ) ;
509+ }
508510 }
509- }
510- } ,
511- BatchSize :: SmallInput ,
512- )
513- } ) ;
511+ } ,
512+ BatchSize :: SmallInput ,
513+ )
514+ } ,
515+ ) ;
514516
515517 // Post-rewrite: a single struct-valued accumulator.
516- c. bench_function ( & format ! ( "{name} coalesced struct" ) , |b| {
518+ c. bench_function ( & format ! ( "{name} coalesced struct {variant} " ) , |b| {
517519 b. iter_batched (
518520 || {
519521 let mut acc = prepare_typed_groups_accumulator (
520522 true ,
521523 struct_values. data_type ( ) . clone ( ) ,
522524 ) ;
523525 acc. update_batch (
524- & [ Arc :: clone ( & struct_values) , Arc :: clone ( & worst_ord) ] ,
525- & group_indices,
526+ & [ Arc :: clone ( struct_values) , Arc :: clone ( worst_ord) ] ,
527+ group_indices,
526528 None ,
527529 num_groups,
528530 )
529531 . unwrap ( ) ;
530532 acc
531533 } ,
532534 |mut accumulator| {
533- for _ in 0 .. 100 {
535+ for ord in iter_ords {
534536 #[ expect( clippy:: unit_arg) ]
535537 black_box (
536538 accumulator
537539 . update_batch (
538- & [ Arc :: clone ( & struct_values) , Arc :: clone ( & ord) ] ,
539- & group_indices,
540+ & [ Arc :: clone ( struct_values) , Arc :: clone ( ord) ] ,
541+ group_indices,
540542 None ,
541543 num_groups,
542544 )
@@ -549,6 +551,73 @@ fn coalesce_comparison_bench(
549551 } ) ;
550552}
551553
554+ /// Head-to-head for the coalesce-peers rewrite: N independent primitive
555+ /// `first_value` accumulators (the pre-rewrite plan) vs one struct-valued
556+ /// accumulator carrying the same N columns (the post-rewrite plan). Runs two
557+ /// variants, since the two plans differ on two separable costs:
558+ ///
559+ /// - `(winner stable)` reuses one `ord` every iteration. After the first
560+ /// iteration the running winner already holds the best value in `ord`, so
561+ /// this is dominated by compare-and-reject — the per-row ordering comparison
562+ /// the rewrite collapses (N compares -> 1).
563+ /// - `(winner changes)` feeds a distinct, strictly-decreasing `ord` per
564+ /// iteration so every row becomes a new winner (smaller ord wins; `worst_ord`
565+ /// = i64::MAX primes the loser). This forces the running value to be
566+ /// replaced+copied on every row, exercising the update path the winner-stable
567+ /// case skips — where the struct plan copies one wider row vs N narrow ones.
568+ #[ expect( clippy:: needless_pass_by_value) ]
569+ fn coalesce_comparison_bench (
570+ c : & mut Criterion ,
571+ name : & str ,
572+ column_values : Vec < ArrayRef > ,
573+ struct_values : ArrayRef ,
574+ ord : ArrayRef ,
575+ num_groups : usize ,
576+ ) {
577+ const ITERS : usize = 100 ;
578+ let n = ord. len ( ) ;
579+ let group_indices: Vec < usize > = ( 0 ..n) . map ( |i| i % num_groups) . collect ( ) ;
580+ let worst_ord: ArrayRef = Arc :: new ( Int64Array :: from ( vec ! [ i64 :: MAX ; n] ) ) ;
581+
582+ // Winner-stable: the same `ord` every iteration.
583+ let stable_ords: Vec < ArrayRef > = ( 0 ..ITERS ) . map ( |_| Arc :: clone ( & ord) ) . collect ( ) ;
584+
585+ // Winner-changing: strictly decreasing across (iteration, row), and below
586+ // `worst_ord`, so every row wins and updates the running value. Built once,
587+ // outside the timed loop.
588+ let changing_ords: Vec < ArrayRef > = ( 0 ..ITERS )
589+ . map ( |k| {
590+ let base = i64:: MAX - 1 - ( k as i64 ) * ( n as i64 ) ;
591+ Arc :: new ( Int64Array :: from (
592+ ( 0 ..n as i64 ) . map ( |i| base - i) . collect :: < Vec < i64 > > ( ) ,
593+ ) ) as ArrayRef
594+ } )
595+ . collect ( ) ;
596+
597+ run_coalesce_variant (
598+ c,
599+ name,
600+ "(winner stable)" ,
601+ & column_values,
602+ & struct_values,
603+ & worst_ord,
604+ & stable_ords,
605+ & group_indices,
606+ num_groups,
607+ ) ;
608+ run_coalesce_variant (
609+ c,
610+ name,
611+ "(winner changes)" ,
612+ & column_values,
613+ & struct_values,
614+ & worst_ord,
615+ & changing_ords,
616+ & group_indices,
617+ num_groups,
618+ ) ;
619+ }
620+
552621fn first_last_nested_benchmark ( c : & mut Criterion ) {
553622 const N : usize = 65536 ;
554623 const NUM_GROUPS : usize = 1024 ;
0 commit comments