|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Benchmarks for spark_map_sort. |
| 19 | +
|
| 20 | +use arrow::array::builder::{Int32Builder, MapBuilder, StringBuilder}; |
| 21 | +use arrow::array::{ArrayRef, MapArray, MapFieldNames}; |
| 22 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 23 | +use datafusion::physical_plan::ColumnarValue; |
| 24 | +use datafusion_comet_spark_expr::spark_map_sort; |
| 25 | +use std::hint::black_box; |
| 26 | +use std::sync::Arc; |
| 27 | + |
| 28 | +const BATCH_SIZE: usize = 8192; |
| 29 | + |
| 30 | +fn map_field_names() -> MapFieldNames { |
| 31 | + MapFieldNames { |
| 32 | + entry: "entries".into(), |
| 33 | + key: "key".into(), |
| 34 | + value: "value".into(), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Build a MapArray with `BATCH_SIZE` rows where each map has `entries_per_map` entries. |
| 39 | +/// Keys are integers in reverse order so every map needs a real sort. |
| 40 | +fn build_int_key_map(entries_per_map: usize) -> MapArray { |
| 41 | + let mut builder = MapBuilder::new( |
| 42 | + Some(map_field_names()), |
| 43 | + Int32Builder::new(), |
| 44 | + Int32Builder::new(), |
| 45 | + ); |
| 46 | + for row in 0..BATCH_SIZE { |
| 47 | + for entry_idx in 0..entries_per_map { |
| 48 | + // Reverse order so input is unsorted; vary across rows so different maps differ. |
| 49 | + let key = (entries_per_map - entry_idx) as i32 + (row % 7) as i32; |
| 50 | + let value = entry_idx as i32; |
| 51 | + builder.keys().append_value(key); |
| 52 | + builder.values().append_value(value); |
| 53 | + } |
| 54 | + builder.append(true).unwrap(); |
| 55 | + } |
| 56 | + builder.finish() |
| 57 | +} |
| 58 | + |
| 59 | +/// Same shape as `build_int_key_map` but with string keys. |
| 60 | +fn build_string_key_map(entries_per_map: usize) -> MapArray { |
| 61 | + let mut builder = MapBuilder::new( |
| 62 | + Some(map_field_names()), |
| 63 | + StringBuilder::new(), |
| 64 | + Int32Builder::new(), |
| 65 | + ); |
| 66 | + for row in 0..BATCH_SIZE { |
| 67 | + for entry_idx in 0..entries_per_map { |
| 68 | + let key = format!("key_{:04}", entries_per_map - entry_idx + (row % 7)); |
| 69 | + let value = entry_idx as i32; |
| 70 | + builder.keys().append_value(&key); |
| 71 | + builder.values().append_value(value); |
| 72 | + } |
| 73 | + builder.append(true).unwrap(); |
| 74 | + } |
| 75 | + builder.finish() |
| 76 | +} |
| 77 | + |
| 78 | +fn bench_map_sort(c: &mut Criterion) { |
| 79 | + let mut group = c.benchmark_group("spark_map_sort"); |
| 80 | + |
| 81 | + for entries in [4usize, 16, 64] { |
| 82 | + let int_map: ArrayRef = Arc::new(build_int_key_map(entries)); |
| 83 | + group.bench_with_input( |
| 84 | + BenchmarkId::new("int_keys", entries), |
| 85 | + &int_map, |
| 86 | + |b, array| { |
| 87 | + let args = vec![ColumnarValue::Array(Arc::clone(array))]; |
| 88 | + b.iter(|| black_box(spark_map_sort(black_box(&args)).unwrap())); |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + let string_map: ArrayRef = Arc::new(build_string_key_map(entries)); |
| 93 | + group.bench_with_input( |
| 94 | + BenchmarkId::new("string_keys", entries), |
| 95 | + &string_map, |
| 96 | + |b, array| { |
| 97 | + let args = vec![ColumnarValue::Array(Arc::clone(array))]; |
| 98 | + b.iter(|| black_box(spark_map_sort(black_box(&args)).unwrap())); |
| 99 | + }, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + group.finish(); |
| 104 | +} |
| 105 | + |
| 106 | +criterion_group!(benches, bench_map_sort); |
| 107 | +criterion_main!(benches); |
0 commit comments