Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 156 additions & 70 deletions arrow/benches/merge_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ use rand::prelude::StdRng;
use rand::{RngExt, SeedableRng};
use std::hint;
use std::sync::Arc;
use std::time::Duration;

use arrow::array::*;
use arrow::datatypes::*;
use arrow::util::bench_util::*;
use arrow_select::merge::merge;

/// Each benchmark cycles through this many arrays and random masks, one per
/// iteration, so that the branch predictor cannot learn a mask.
const NUM_INPUTS: usize = 64;

trait InputGenerator {
fn name(&self) -> &str;

Expand Down Expand Up @@ -133,24 +138,83 @@ where
}
}

fn mask_cases(len: usize) -> Vec<(&'static str, BooleanArray)> {
/// A mask whose set bits come in runs averaging `run` bits, with the unset
/// runs sized to give `true_density`: a CASE over a clustered or sorted
/// column, where the output is long copies from either side.
fn create_clustered_array(len: usize, true_density: f64, run: f64, seed: u64) -> BooleanArray {
let mut rng = StdRng::seed_from_u64(seed);
let leave_set = 1.0 / run;
let leave_unset = true_density / (run * (1.0 - true_density));
let mut set = rng.random_bool(true_density);
(0..len)
.map(|_| {
let bit = set;
set = if set {
!rng.random_bool(leave_set)
} else {
rng.random_bool(leave_unset)
};
Some(bit)
})
.collect()
}

/// Returns the masks for each mask case: `NUM_INPUTS` of each random case, and
/// one of each constant case.
fn mask_cases(len: usize) -> Vec<(&'static str, Vec<BooleanArray>)> {
let random = |null_density, true_density| -> Vec<BooleanArray> {
(0..NUM_INPUTS as u64)
.map(|seed| create_boolean_array_with_seed(len, null_density, true_density, seed))
.collect()
};
vec![
("all_true", create_boolean_array(len, 0.0, 1.0)),
("99pct_true", create_boolean_array(len, 0.0, 0.99)),
("90pct_true", create_boolean_array(len, 0.0, 0.9)),
("50pct_true", create_boolean_array(len, 0.0, 0.5)),
("10pct_true", create_boolean_array(len, 0.0, 0.1)),
("1pct_true", create_boolean_array(len, 0.0, 0.01)),
("all_false", create_boolean_array(len, 0.0, 0.0)),
("50pct_nulls", create_boolean_array(len, 0.5, 0.5)),
("all_true", vec![create_boolean_array(len, 0.0, 1.0)]),
("99pct_true", random(0.0, 0.99)),
("90pct_true", random(0.0, 0.9)),
("50pct_true", random(0.0, 0.5)),
("10pct_true", random(0.0, 0.1)),
("1pct_true", random(0.0, 0.01)),
("all_false", vec![create_boolean_array(len, 0.0, 0.0)]),
("50pct_nulls", random(0.5, 0.5)),
]
}

fn bench_merge_on_input_generator(c: &mut Criterion, input_generator: &impl InputGenerator) {
fn run_mask_cases(len: usize) -> Vec<(&'static str, Vec<BooleanArray>)> {
[
("runs_of_64_50pct_true", 0.5, 64.0),
("runs_of_512_10pct_true", 0.1, 512.0),
]
.into_iter()
.map(|(name, density, run)| {
let masks = (0..NUM_INPUTS as u64)
.map(|seed| create_clustered_array(len, density, run, seed))
.collect();
(name, masks)
})
.collect()
}

/// Which input combinations to run for a type.
#[derive(PartialEq)]
enum Scenarios {
All,
/// Only an array against a scalar, the CASE WHEN ... THEN expr ELSE literal shape.
ScalarSide,
}

fn bench_merge_on_input_generator(
c: &mut Criterion,
input_generator: &impl InputGenerator,
scenarios: Scenarios,
) {
const ARRAY_LEN: usize = 8192;

let mut group =
c.benchmark_group(format!("merge_{ARRAY_LEN}_from_{}", input_generator.name()).as_str());
// One merge of 8192 rows takes microseconds, so the default 3 s warm up

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.

i wonder if we need to cut down the benches in this suite; similar to what we did for zip

and/or reduce from 8192 to a smaller batch size 🤔

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.

Yes hello, agreed. Cut it down the same way as #10919: one strings (3..20) type instead of short/long strings and bytes, dropped non_null_scalar_vs_null_scalar, and kept two of the four run masks (runs of 64 at 50 %, runs of 512 at 10 %). 448 -> 168 cases, under the 240 on main. I kept 8192 rows: at smaller batches the fixed per-call cost starts to dominate and the per-row difference this suite is meant to show gets harder to see.

// and 5 s of samples per case would make the whole run take over an hour.
group.warm_up_time(Duration::from_millis(500));
group.measurement_time(Duration::from_secs(1));

let null_scalar = input_generator.generate_scalar_with_null_value();
let [non_null_scalar_1, non_null_scalar_2]: [_; 2] = input_generator
Expand All @@ -159,68 +223,110 @@ fn bench_merge_on_input_generator(c: &mut Criterion, input_generator: &impl Inpu
.unwrap();

// For simplicity, we generate arrays with length ARRAY_LEN. Not all input values will be used.
let array_1_10pct_nulls = input_generator.generate_array(42, ARRAY_LEN, 0.1);
let array_2_10pct_nulls = input_generator.generate_array(18, ARRAY_LEN, 0.1);
let arrays = |first_seed: u64, null_percentage: f32| -> Vec<ArrayRef> {
(0..NUM_INPUTS as u64)
.map(|i| input_generator.generate_array(first_seed + i, ARRAY_LEN, null_percentage))
.collect()
};
let arrays_1_10pct_nulls = arrays(42, 0.1);
let arrays_2_10pct_nulls = arrays(42 + NUM_INPUTS as u64, 0.1);
let arrays_no_nulls = arrays(42, 0.0);

let masks = mask_cases(ARRAY_LEN);
// Runs only matter where one side is a scalar: they decide between
// copying runs of the array and writing it row by row.
let scalar_side_masks: Vec<_> = masks
.iter()
.cloned()
.chain(run_mask_cases(ARRAY_LEN))
.collect();

bench_merge_input_on_all_masks(
"array_vs_non_null_scalar",
&mut group,
&scalar_side_masks,
&arrays_1_10pct_nulls,
&[Scalar::new(non_null_scalar_1.clone())],
);

bench_merge_input_on_all_masks(
"array_no_nulls_vs_non_null_scalar",
&mut group,
&scalar_side_masks,
&arrays_no_nulls,
&[Scalar::new(non_null_scalar_1.clone())],
);

// CASE WHEN ... THEN array END: the ELSE side is NULL.
bench_merge_input_on_all_masks(
"array_vs_null_scalar",
&mut group,
&scalar_side_masks,
&arrays_1_10pct_nulls,
&[Scalar::new(null_scalar.clone())],
);

bench_merge_input_on_all_masks(
"non_null_scalar_vs_array",
&mut group,
&scalar_side_masks,
&[Scalar::new(non_null_scalar_1.clone())],
&arrays_1_10pct_nulls,
);

if scenarios == Scenarios::ScalarSide {
group.finish();
return;
}

// Benchmarks for different scalar combinations
for (description, truthy, falsy) in &[
("null_vs_non_null_scalar", &null_scalar, &non_null_scalar_1),
(
"non_null_scalar_vs_null_scalar",
&non_null_scalar_1,
&null_scalar,
),
("non_nulls_scalars", &non_null_scalar_1, &non_null_scalar_2),
] {
bench_merge_input_on_all_masks(
description,
&mut group,
&masks,
&Scalar::new(truthy),
&Scalar::new(falsy),
&[Scalar::new(truthy)],
&[Scalar::new(falsy)],
);
}

bench_merge_input_on_all_masks(
"array_vs_non_null_scalar",
&mut group,
&masks,
&array_1_10pct_nulls,
&Scalar::new(non_null_scalar_1.clone()),
);

bench_merge_input_on_all_masks(
"non_null_scalar_vs_array",
&mut group,
&masks,
&Scalar::new(non_null_scalar_1.clone()),
&array_1_10pct_nulls,
);

bench_merge_input_on_all_masks(
"array_vs_array",
&mut group,
&masks,
&array_1_10pct_nulls,
&array_2_10pct_nulls,
&arrays_1_10pct_nulls,
&arrays_2_10pct_nulls,
);

group.finish();
}

/// Benchmarks `merge` for each mask case. Each iteration takes the next of the
/// case's masks, of `truthy` and of `falsy`, cycling through each list on its
/// own, so a one-element list stays the same.
fn bench_merge_input_on_all_masks(
description: &str,
group: &mut BenchmarkGroup<WallTime>,
masks: &[(&str, BooleanArray)],
truthy: &impl Datum,
falsy: &impl Datum,
mask_cases: &[(&str, Vec<BooleanArray>)],
truthy: &[impl Datum],
falsy: &[impl Datum],
) {
for (mask_description, mask) in masks {
for (mask_description, masks) in mask_cases {
let id = BenchmarkId::new(description, mask_description);
group.bench_with_input(id, mask, |b, mask| {
b.iter(|| hint::black_box(merge(mask, truthy, falsy)))
let mut masks = masks.iter().cycle();
let mut truthy = truthy.iter().cycle();
let mut falsy = falsy.iter().cycle();
group.bench_function(id, |b| {
b.iter(|| {
hint::black_box(merge(
masks.next().unwrap(),
truthy.next().unwrap(),
falsy.next().unwrap(),
))
})
});
}
}
Expand All @@ -233,46 +339,26 @@ fn add_benchmark(c: &mut Criterion) {
description: "i32".to_string(),
_marker: std::marker::PhantomData,
},
Scenarios::All,
);

// Short strings
bench_merge_on_input_generator(
c,
&GenerateBytes::<GenericStringType<i32>> {
description: "short strings (3..10)".to_string(),
range_length: 3..10,
&GeneratePrimitive::<Decimal128Type> {
description: "decimal128".to_string(),
_marker: std::marker::PhantomData,
},
Scenarios::ScalarSide,
);

// Long strings
bench_merge_on_input_generator(
c,
&GenerateBytes::<GenericStringType<i32>> {
description: "long strings (100..400)".to_string(),
range_length: 100..400,
_marker: std::marker::PhantomData,
},
);

// Short Bytes
bench_merge_on_input_generator(
c,
&GenerateBytes::<GenericBinaryType<i32>> {
description: "short bytes (3..10)".to_string(),
range_length: 3..10,
_marker: std::marker::PhantomData,
},
);

// Long Bytes
bench_merge_on_input_generator(
c,
&GenerateBytes::<GenericBinaryType<i32>> {
description: "long bytes (100..400)".to_string(),
range_length: 100..400,
description: "strings (3..20)".to_string(),
range_length: 3..20,
_marker: std::marker::PhantomData,
},
Scenarios::All,
);
}

Expand Down
Loading