-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Faster StringDictionaryBuilder (#1851)
- Loading branch information
Showing
4 changed files
with
427 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use arrow::array::{Int32Builder, StringBuilder, StringDictionaryBuilder}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use rand::{thread_rng, Rng}; | ||
|
||
/// Note: this is best effort, not all keys are necessarily present or unique | ||
fn build_strings(dict_size: usize, total_size: usize, key_len: usize) -> Vec<String> { | ||
let mut rng = thread_rng(); | ||
let values: Vec<String> = (0..dict_size) | ||
.map(|_| (0..key_len).map(|_| rng.gen::<char>()).collect()) | ||
.collect(); | ||
|
||
(0..total_size) | ||
.map(|_| values[rng.gen_range(0..dict_size)].clone()) | ||
.collect() | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("string_dictionary_builder"); | ||
|
||
let mut do_bench = |dict_size: usize, total_size: usize, key_len: usize| { | ||
group.bench_function( | ||
format!( | ||
"(dict_size:{}, len:{}, key_len: {})", | ||
dict_size, total_size, key_len | ||
), | ||
|b| { | ||
let strings = build_strings(dict_size, total_size, key_len); | ||
b.iter(|| { | ||
let keys = Int32Builder::new(strings.len()); | ||
let values = StringBuilder::new((key_len + 1) * dict_size); | ||
let mut builder = StringDictionaryBuilder::new(keys, values); | ||
|
||
for val in &strings { | ||
builder.append(val).unwrap(); | ||
} | ||
|
||
builder.finish(); | ||
}) | ||
}, | ||
); | ||
}; | ||
|
||
do_bench(20, 1000, 5); | ||
do_bench(100, 1000, 5); | ||
do_bench(100, 1000, 10); | ||
do_bench(100, 10000, 10); | ||
do_bench(100, 10000, 100); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Oops, something went wrong.