-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: patches uses a map in some cases (#1626)
See [this sheet for the data from take_patches.rs](https://docs.google.com/spreadsheets/d/1D9vBZ1QJ6mwcIvV5wIL0hjGgVchcEnAyhvitqWu2ugU). I'm on an M3 Max with 96 GiB of RAM with macOS 14.4. This threshold likely depends on the ISA. Intuitively, repeated searching is `O(N_INDICES * lg N_PATCHES)` and repeated map lookups is `O(N_INDICES + N_PATCHES)`. It seems to me that the compiler & CPU would have trouble paralleling search (via SIMD or ILP) because of the branching, whereas map lookups are more obviously parallelized (e.g. SIMD hash computation). I'm not entirely sure why the cross over point seems to be around N_PATCHES / N_INDICES = 5. I believe the M3 Max has 128-bit registers, so if the indices are 32-bits then index arithmetic could be 4-way parallel.
- Loading branch information
Showing
5 changed files
with
236 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,7 @@ harness = false | |
[[bench]] | ||
name = "take_strings" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "take_patches" | ||
harness = false |
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,71 @@ | ||
#![allow(clippy::unwrap_used)] | ||
|
||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng as _}; | ||
use vortex_array::patches::Patches; | ||
use vortex_array::ArrayData; | ||
|
||
fn fixture(len: usize, sparsity: f64, rng: &mut StdRng) -> Patches { | ||
// NB: indices are always ordered | ||
let indices = (0..len) | ||
.filter(|_| rng.gen_bool(sparsity)) | ||
.map(|x| x as u64) | ||
.collect::<Vec<u64>>(); | ||
let sparse_len = indices.len(); | ||
let values = ArrayData::from((0..sparse_len).map(|x| x as u64).collect::<Vec<_>>()); | ||
Patches::new(len, ArrayData::from(indices), values) | ||
} | ||
|
||
fn indices(array_len: usize, n_indices: usize, rng: &mut StdRng) -> ArrayData { | ||
ArrayData::from( | ||
(0..n_indices) | ||
.map(|_| rng.gen_range(0..(array_len as u64))) | ||
.collect::<Vec<u64>>(), | ||
) | ||
} | ||
|
||
#[allow(clippy::cast_possible_truncation)] | ||
fn bench_take(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("bench_take"); | ||
let mut rng = StdRng::seed_from_u64(0); | ||
|
||
for patches_sparsity in [0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001] { | ||
let patches = fixture(65_535, patches_sparsity, &mut rng); | ||
for index_multiple in [1.0, 0.5, 0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001] { | ||
let indices = indices( | ||
patches.array_len(), | ||
(patches.array_len() as f64 * index_multiple) as usize, | ||
&mut rng, | ||
); | ||
group.bench_with_input( | ||
BenchmarkId::from_parameter(format!( | ||
"take_search: array_len={}, n_patches={} (~{}%), n_indices={} ({}%)", | ||
patches.array_len(), | ||
patches.num_patches(), | ||
patches_sparsity, | ||
indices.len(), | ||
index_multiple * 100.0 | ||
)), | ||
&(&patches, &indices), | ||
|b, (patches, indices)| b.iter(|| patches.take_search(indices)), | ||
); | ||
group.bench_with_input( | ||
BenchmarkId::from_parameter(format!( | ||
"take_map: array_len={}, n_patches={} (~{}%), n_indices={} ({}%)", | ||
patches.array_len(), | ||
patches.num_patches(), | ||
patches_sparsity, | ||
indices.len(), | ||
index_multiple * 100.0 | ||
)), | ||
&(&patches, &indices), | ||
|b, (patches, indices)| b.iter(|| patches.take_map(indices)), | ||
); | ||
} | ||
} | ||
group.finish() | ||
} | ||
|
||
criterion_group!(benches, bench_take); | ||
criterion_main!(benches); |
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
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