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
102 changes: 102 additions & 0 deletions parquet/benches/arrow_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2070,11 +2070,113 @@ fn bench_ndv(c: &mut Criterion) {
}
}

/// Writes a wide nested struct (64 leaves under 2 levels of structs) in small batches.
///
/// Each write computes the levels of every column again, so this measures the per-batch
/// cost of building the levels, which grows with the number of nested nodes. The other
/// cases have few nodes and large batches, so they do not show this cost.
fn bench_wide_nested_small_batches(c: &mut Criterion) {
const ROWS: usize = 64 * 1024;
const BATCH_ROWS: usize = 64;

let leaves: Fields = (0..8)
.map(|i| Field::new(format!("leaf_{i}"), DataType::Int32, true))
.collect();
let children: Fields = (0..8)
.map(|i| Field::new_struct(format!("struct_{i}"), leaves.clone(), true))
.collect();
let schema = Arc::new(Schema::new(vec![Field::new_struct("root", children, true)]));

let mut group = c.benchmark_group("wide_nested_struct_small_batches");
group.throughput(Throughput::Elements(ROWS as u64));
// Built on first use, so a filter that skips this case does not build the input.
let batches = OnceCell::new();
group.bench_function("default", |b| {
let batches: &Vec<_> = batches.get_or_init(|| {
let batch = create_random_batch(schema.clone(), ROWS, 0.1, 0.75).unwrap();
(0..ROWS)
.step_by(BATCH_ROWS)
.map(|offset| batch.slice(offset, BATCH_ROWS))
.collect()
});
b.iter(|| {
let mut writer = ArrowWriter::try_new(Empty::default(), schema.clone(), None).unwrap();
for batch in batches {
writer.write(black_box(batch)).unwrap();
}
black_box(writer.close()).unwrap();
})
});
group.finish();
}

/// Fields of a struct tree with `fan_out` children per node and `depth` levels of structs
/// above the leaves. The leaves cycle through Int64, Utf8, Boolean and Int32.
fn wide_deep_struct_fields(fan_out: usize, depth: usize, next_leaf: &mut usize) -> Fields {
(0..fan_out)
.map(|i| {
if depth == 0 {
let data_type = match *next_leaf % 4 {
0 => DataType::Int64,
1 => DataType::Utf8,
2 => DataType::Boolean,
_ => DataType::Int32,
};
*next_leaf += 1;
Field::new(format!("leaf_{i}"), data_type, true)
} else {
let children = wide_deep_struct_fields(fan_out, depth - 1, next_leaf);
Field::new_struct(format!("struct_{i}"), children, true)
}
})
.collect()
}

/// Writes a struct column with 1024 leaves under 10 levels of nested structs (a binary tree),
/// in batches of 16 and 128 rows.
///
/// Each write builds the levels of every leaf again, and that cost grows with the depth of the
/// leaf. With many deep leaves and small batches it is a large part of the write, which the
/// shallower `wide_nested_struct_small_batches` does not show.
fn bench_wide_deep_nested_struct(c: &mut Criterion) {
const ROWS: usize = 2048;

let mut next_leaf = 0;
let children = wide_deep_struct_fields(2, 9, &mut next_leaf);
let schema = Arc::new(Schema::new(vec![Field::new_struct("root", children, true)]));

let mut group = c.benchmark_group("wide_deep_nested_struct");
group.throughput(Throughput::Elements(ROWS as u64));
// Built on first use, so a filter that skips both cases does not build the input.
let batch = OnceCell::new();
for batch_rows in [16, 128] {
group.bench_function(format!("{batch_rows}_rows"), |b| {
let batch =
batch.get_or_init(|| create_random_batch(schema.clone(), ROWS, 0.1, 0.75).unwrap());
let batches: Vec<_> = (0..ROWS)
.step_by(batch_rows)
.map(|offset| batch.slice(offset, batch_rows))
.collect();
b.iter(|| {
let mut writer =
ArrowWriter::try_new(Empty::default(), schema.clone(), None).unwrap();
for batch in &batches {
writer.write(black_box(batch)).unwrap();
}
black_box(writer.close()).unwrap();
})
});
}
group.finish();
}

criterion_group!(
benches,
bench_all_writers,
bench_small_delta_byte_array_writers,
bench_delta_byte_array_writers,
bench_ndv,
bench_wide_nested_small_batches,
bench_wide_deep_nested_struct,
);
criterion_main!(benches);