|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![expect(clippy::unwrap_used)] |
| 5 | + |
| 6 | +use std::sync::LazyLock; |
| 7 | + |
| 8 | +use divan::Bencher; |
| 9 | +use mimalloc::MiMalloc; |
| 10 | +use vortex_array::ArrayRef; |
| 11 | +use vortex_array::IntoArray; |
| 12 | +use vortex_array::VortexSessionExecute; |
| 13 | +use vortex_array::arrays::PrimitiveArray; |
| 14 | +use vortex_array::scalar::Scalar; |
| 15 | +use vortex_fastlanes::BitPacked; |
| 16 | +use vortex_fastlanes::BitPackedArrayExt; |
| 17 | +use vortex_fastlanes::BitPackedArraySlotsExt; |
| 18 | +use vortex_fastlanes::FoR; |
| 19 | +use vortex_fastlanes::bitpack_compress::bitpack_encode; |
| 20 | +use vortex_fastlanes::bitpack_compress::bitpack_encode_blocked; |
| 21 | +use vortex_session::VortexSession; |
| 22 | + |
| 23 | +#[global_allocator] |
| 24 | +static GLOBAL: MiMalloc = MiMalloc; |
| 25 | + |
| 26 | +fn main() { |
| 27 | + LazyLock::force(&SESSION); |
| 28 | + divan::main(); |
| 29 | +} |
| 30 | + |
| 31 | +const LEN: usize = 65_536; |
| 32 | +const ACCESSES: usize = 256; |
| 33 | +const FIXTURES: &[(&str, bool)] = &[ |
| 34 | + ("global", false), |
| 35 | + ("global", true), |
| 36 | + ("blocked_primitive", false), |
| 37 | + ("blocked_primitive", true), |
| 38 | + ("blocked_for", false), |
| 39 | + ("blocked_for", true), |
| 40 | +]; |
| 41 | +const PATCHED_FIXTURES: &[&str] = &["global", "blocked_primitive", "blocked_for"]; |
| 42 | + |
| 43 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 44 | + let session = vortex_array::array_session(); |
| 45 | + vortex_fastlanes::initialize(&session); |
| 46 | + session |
| 47 | +}); |
| 48 | + |
| 49 | +fn fixture(mode: &str, patched: bool) -> ArrayRef { |
| 50 | + let widths: Vec<u8> = (0..LEN / 1024).map(|block| 4 + (block % 4) as u8).collect(); |
| 51 | + let values = PrimitiveArray::from_iter((0..LEN).map(|index| { |
| 52 | + let width = widths[index / 1024]; |
| 53 | + if patched && index % 64 == 0 { |
| 54 | + 65_535u32 |
| 55 | + } else { |
| 56 | + ((index * 7919) as u32) & ((1u32 << width) - 1) |
| 57 | + } |
| 58 | + })); |
| 59 | + let mut ctx = SESSION.create_execution_ctx(); |
| 60 | + let encoded = if mode == "global" { |
| 61 | + bitpack_encode(&values, 7, None, &mut ctx).unwrap() |
| 62 | + } else { |
| 63 | + bitpack_encode_blocked(&values, &widths, None, &mut ctx).unwrap() |
| 64 | + }; |
| 65 | + assert_eq!(encoded.patches().is_some(), patched); |
| 66 | + if mode == "blocked_for" { |
| 67 | + let offsets = encoded.block_offsets().unwrap().clone(); |
| 68 | + let reference = Scalar::zero_value(offsets.dtype()); |
| 69 | + let offsets = FoR::try_new(offsets, reference).unwrap().into_array(); |
| 70 | + BitPacked::try_new_with_block_offsets( |
| 71 | + encoded.packed().clone(), |
| 72 | + encoded.dtype().as_ptype(), |
| 73 | + encoded.validity().unwrap(), |
| 74 | + encoded.patches(), |
| 75 | + offsets, |
| 76 | + encoded.len(), |
| 77 | + encoded.offset(), |
| 78 | + ) |
| 79 | + .unwrap() |
| 80 | + .into_array() |
| 81 | + } else { |
| 82 | + encoded.into_array() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn random_index(read: usize) -> usize { |
| 87 | + (read * 7919) % LEN |
| 88 | +} |
| 89 | + |
| 90 | +#[divan::bench(args = FIXTURES)] |
| 91 | +fn random_once(bencher: Bencher, (mode, patched): (&str, bool)) { |
| 92 | + let array = fixture(mode, patched); |
| 93 | + bencher |
| 94 | + .with_inputs(|| SESSION.create_execution_ctx()) |
| 95 | + .bench_refs(|ctx| { |
| 96 | + for read in 0..ACCESSES { |
| 97 | + divan::black_box(array.execute_scalar(random_index(read), ctx).unwrap()); |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +#[divan::bench(args = FIXTURES)] |
| 103 | +fn random_repeated(bencher: Bencher, (mode, patched): (&str, bool)) { |
| 104 | + let array = fixture(mode, patched); |
| 105 | + bencher |
| 106 | + .with_inputs(|| (SESSION.create_execution_ctx(), array.repeated_probe())) |
| 107 | + .bench_refs(|(ctx, probe)| { |
| 108 | + for read in 0..ACCESSES { |
| 109 | + divan::black_box(probe.execute_scalar(random_index(read), ctx).unwrap()); |
| 110 | + } |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +#[divan::bench(args = FIXTURES)] |
| 115 | +fn same_block_once(bencher: Bencher, (mode, patched): (&str, bool)) { |
| 116 | + let array = fixture(mode, patched); |
| 117 | + bencher |
| 118 | + .with_inputs(|| SESSION.create_execution_ctx()) |
| 119 | + .bench_refs(|ctx| { |
| 120 | + for read in 0..ACCESSES { |
| 121 | + divan::black_box(array.execute_scalar(1024 + read, ctx).unwrap()); |
| 122 | + } |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +#[divan::bench(args = FIXTURES)] |
| 127 | +fn same_block_repeated(bencher: Bencher, (mode, patched): (&str, bool)) { |
| 128 | + let array = fixture(mode, patched); |
| 129 | + bencher |
| 130 | + .with_inputs(|| (SESSION.create_execution_ctx(), array.repeated_probe())) |
| 131 | + .bench_refs(|(ctx, probe)| { |
| 132 | + for read in 0..ACCESSES { |
| 133 | + divan::black_box(probe.execute_scalar(1024 + read, ctx).unwrap()); |
| 134 | + } |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +#[divan::bench(args = PATCHED_FIXTURES)] |
| 139 | +fn patch_hit_repeated(bencher: Bencher, mode: &str) { |
| 140 | + let array = fixture(mode, true); |
| 141 | + bencher |
| 142 | + .with_inputs(|| (SESSION.create_execution_ctx(), array.repeated_probe())) |
| 143 | + .bench_refs(|(ctx, probe)| { |
| 144 | + for _ in 0..ACCESSES { |
| 145 | + divan::black_box(probe.execute_scalar(1024, ctx).unwrap()); |
| 146 | + } |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +#[divan::bench(args = PATCHED_FIXTURES)] |
| 151 | +fn patch_miss_repeated(bencher: Bencher, mode: &str) { |
| 152 | + let array = fixture(mode, true); |
| 153 | + bencher |
| 154 | + .with_inputs(|| (SESSION.create_execution_ctx(), array.repeated_probe())) |
| 155 | + .bench_refs(|(ctx, probe)| { |
| 156 | + for _ in 0..ACCESSES { |
| 157 | + divan::black_box(probe.execute_scalar(1025, ctx).unwrap()); |
| 158 | + } |
| 159 | + }); |
| 160 | +} |
0 commit comments