-
Notifications
You must be signed in to change notification settings - Fork 839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add lexsort benchmark (#2871) #2929
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::compute::{lexsort_to_indices, SortColumn}; | ||
use arrow::row::{RowConverter, SortField}; | ||
use arrow::util::bench_util::{ | ||
create_dict_from_values, create_primitive_array, create_string_array_with_len, | ||
}; | ||
use arrow_array::types::Int32Type; | ||
use arrow_array::{Array, ArrayRef, UInt32Array}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Copy, Clone)] | ||
enum Column { | ||
RequiredI32, | ||
OptionalI32, | ||
Required16CharString, | ||
Optional16CharString, | ||
Optional50CharString, | ||
Optional100Value50CharStringDict, | ||
} | ||
|
||
impl std::fmt::Debug for Column { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let s = match self { | ||
Column::RequiredI32 => "i32", | ||
Column::OptionalI32 => "i32_opt", | ||
Column::Required16CharString => "str(16)", | ||
Column::Optional16CharString => "str_opt(16)", | ||
Column::Optional50CharString => "str_opt(50)", | ||
Column::Optional100Value50CharStringDict => "dict(100,str_opt(50))", | ||
}; | ||
f.write_str(s) | ||
} | ||
} | ||
|
||
impl Column { | ||
fn generate(self, size: usize) -> ArrayRef { | ||
match self { | ||
Column::RequiredI32 => { | ||
Arc::new(create_primitive_array::<Int32Type>(size, 0.)) | ||
} | ||
Column::OptionalI32 => { | ||
Arc::new(create_primitive_array::<Int32Type>(size, 0.2)) | ||
} | ||
Column::Required16CharString => { | ||
Arc::new(create_string_array_with_len::<i32>(size, 0., 16)) | ||
} | ||
Column::Optional16CharString => { | ||
Arc::new(create_string_array_with_len::<i32>(size, 0.2, 16)) | ||
} | ||
Column::Optional50CharString => { | ||
Arc::new(create_string_array_with_len::<i32>(size, 0., 50)) | ||
} | ||
Column::Optional100Value50CharStringDict => { | ||
Arc::new(create_dict_from_values::<Int32Type>( | ||
size, | ||
0.1, | ||
&create_string_array_with_len::<i32>(100, 0., 50), | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn do_bench(c: &mut Criterion, columns: &[Column], len: usize) { | ||
let arrays: Vec<_> = columns.iter().map(|x| x.generate(len)).collect(); | ||
let sort_columns: Vec<_> = arrays | ||
.iter() | ||
.cloned() | ||
.map(|values| SortColumn { | ||
values, | ||
options: None, | ||
}) | ||
.collect(); | ||
|
||
c.bench_function( | ||
&format!("lexsort_to_indices({:?}): {}", columns, len), | ||
|b| { | ||
b.iter(|| { | ||
criterion::black_box(lexsort_to_indices(&sort_columns, None).unwrap()) | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_function(&format!("lexsort_rows({:?}): {}", columns, len), |b| { | ||
b.iter(|| { | ||
criterion::black_box({ | ||
let fields = arrays | ||
.iter() | ||
.map(|a| SortField::new(a.data_type().clone())) | ||
.collect(); | ||
let mut converter = RowConverter::new(fields); | ||
let rows = converter.convert_columns(&arrays).unwrap(); | ||
let mut sort: Vec<_> = rows.iter().enumerate().collect(); | ||
sort.sort_unstable_by(|(_, a), (_, b)| a.cmp(b)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still a little bit confused as to why lexsort_to_indices can use sort_unstable whilst claiming to be a stable sort, but perhaps I've missed some subtlety. I just do the same thing here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we changed lexsort_to_indices to be unstable in the name of performance. As in it shouldn't be claiming to be stable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if the lexsort to indices is correctly doing stable sort. However it's possible to use unstable sort for stable sorting as long as you also sort the indexes: https://rust-lang.github.io/rfcs/1884-unstable-sort.html
|
||
UInt32Array::from_iter_values(sort.iter().map(|(i, _)| *i as u32)) | ||
}) | ||
}) | ||
}); | ||
} | ||
|
||
fn add_benchmark(c: &mut Criterion) { | ||
let cases: &[&[Column]] = &[ | ||
&[Column::RequiredI32, Column::OptionalI32], | ||
&[Column::RequiredI32, Column::Optional16CharString], | ||
&[Column::RequiredI32, Column::Required16CharString], | ||
&[Column::Optional16CharString, Column::Required16CharString], | ||
&[ | ||
Column::Optional16CharString, | ||
Column::Optional50CharString, | ||
Column::Required16CharString, | ||
], | ||
&[ | ||
Column::Optional16CharString, | ||
Column::Required16CharString, | ||
Column::Optional16CharString, | ||
Column::Optional16CharString, | ||
Column::Optional16CharString, | ||
], | ||
&[ | ||
Column::OptionalI32, | ||
Column::Optional100Value50CharStringDict, | ||
], | ||
&[ | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
], | ||
&[ | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Required16CharString, | ||
], | ||
&[ | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional50CharString, | ||
], | ||
&[ | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional100Value50CharStringDict, | ||
Column::Optional50CharString, | ||
], | ||
]; | ||
|
||
for case in cases { | ||
do_bench(c, *case, 4096); | ||
do_bench(c, *case, 4096 * 8); | ||
} | ||
} | ||
|
||
criterion_group!(benches, add_benchmark); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -897,6 +897,10 @@ pub struct SortColumn { | |
/// assert_eq!(as_primitive_array::<Int64Type>(&sorted_columns[0]).value(1), -64); | ||
/// assert!(sorted_columns[0].is_null(0)); | ||
/// ``` | ||
/// | ||
/// Note: for multi-column sorts without a limit, using the [row format][crate::row] | ||
/// may be significantly faster | ||
/// | ||
pub fn lexsort(columns: &[SortColumn], limit: Option<usize>) -> Result<Vec<ArrayRef>> { | ||
let indices = lexsort_to_indices(columns, limit)?; | ||
columns | ||
|
@@ -907,6 +911,9 @@ pub fn lexsort(columns: &[SortColumn], limit: Option<usize>) -> Result<Vec<Array | |
|
||
/// Sort elements lexicographically from a list of `ArrayRef` into an unsigned integer | ||
/// (`UInt32Array`) of indices. | ||
/// | ||
/// Note: for multi-column sorts without a limit, using the [row format][crate::row] | ||
/// may be significantly faster | ||
pub fn lexsort_to_indices( | ||
columns: &[SortColumn], | ||
limit: Option<usize>, | ||
|
@@ -942,11 +949,8 @@ pub fn lexsort_to_indices( | |
lexicographical_comparator.compare(a, b) | ||
}); | ||
|
||
Ok(UInt32Array::from( | ||
(&value_indices)[0..len] | ||
.iter() | ||
.map(|i| *i as u32) | ||
.collect::<Vec<u32>>(), | ||
Ok(UInt32Array::from_iter_values( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drive by cleanup to avoid an intermediate array |
||
value_indices.iter().map(|i| *i as u32), | ||
)) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already an issue with the benchmark names being too long, so going to skip this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classic tradeoff between concision and verboseness 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wouldn't mind if there was some way to stop criterion truncating benchmark names, but I can't find such an option