|
| 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 | +use datasketches::hll::HllSketch; |
| 19 | +use datasketches::hll::HllType; |
| 20 | +use datasketches::hll::HllUnion; |
| 21 | +use divan::Bencher; |
| 22 | +use divan::black_box; |
| 23 | + |
| 24 | +fn partials(lg_k: u8, count: usize, rows: usize) -> Vec<HllSketch> { |
| 25 | + (0..count) |
| 26 | + .map(|partial| { |
| 27 | + let mut sketch = HllSketch::new(lg_k, HllType::Hll8).unwrap(); |
| 28 | + for value in partial * rows..(partial + 1) * rows { |
| 29 | + sketch.update(value as u64); |
| 30 | + } |
| 31 | + sketch |
| 32 | + }) |
| 33 | + .collect() |
| 34 | +} |
| 35 | + |
| 36 | +#[divan::bench(args = [12, 18])] |
| 37 | +fn empty_from_dense(bencher: Bencher, lg_k: u8) { |
| 38 | + let source = partials(lg_k, 1, 1 << lg_k).pop().unwrap(); |
| 39 | + bencher.bench_local(|| { |
| 40 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 41 | + union.update(black_box(&source)); |
| 42 | + black_box(union) |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +#[divan::bench(args = [12, 18])] |
| 47 | +fn recreated_small_partials(bencher: Bencher, lg_k: u8) { |
| 48 | + let partials = partials(lg_k, 400, 200); |
| 49 | + bencher.bench_local(|| { |
| 50 | + let mut sketch = HllSketch::new(lg_k, HllType::Hll8).unwrap(); |
| 51 | + for partial in black_box(&partials) { |
| 52 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 53 | + union.update(&sketch); |
| 54 | + union.update(partial); |
| 55 | + sketch = union.to_sketch(HllType::Hll8); |
| 56 | + } |
| 57 | + black_box(sketch.estimate()); |
| 58 | + black_box(sketch) |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +#[divan::bench(args = [12, 18])] |
| 63 | +fn persistent_small_partials(bencher: Bencher, lg_k: u8) { |
| 64 | + let partials = partials(lg_k, 400, 200); |
| 65 | + bencher.bench_local(|| { |
| 66 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 67 | + for partial in black_box(&partials) { |
| 68 | + union.update(partial); |
| 69 | + } |
| 70 | + black_box(union.estimate()); |
| 71 | + black_box(union.to_sketch(HllType::Hll8)) |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +#[divan::bench(args = [12, 18])] |
| 76 | +fn dense_partials(bencher: Bencher, lg_k: u8) { |
| 77 | + let partials = partials(lg_k, 16, (1 << lg_k) / 4); |
| 78 | + bencher.bench_local(|| { |
| 79 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 80 | + for partial in black_box(&partials) { |
| 81 | + union.update(partial); |
| 82 | + } |
| 83 | + black_box(union.estimate()); |
| 84 | + black_box(union.to_sketch(HllType::Hll8)) |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +#[divan::bench(args = [12, 18])] |
| 89 | +fn dense_partials_with_queries(bencher: Bencher, lg_k: u8) { |
| 90 | + let partials = partials(lg_k, 16, (1 << lg_k) / 4); |
| 91 | + bencher.bench_local(|| { |
| 92 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 93 | + for partial in black_box(&partials) { |
| 94 | + union.update(partial); |
| 95 | + black_box(union.estimate()); |
| 96 | + } |
| 97 | + black_box(union.to_sketch(HllType::Hll8)) |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +#[divan::bench(args = [12, 18])] |
| 102 | +fn downsampled_dense_partials(bencher: Bencher, lg_k: u8) { |
| 103 | + let partials = partials(lg_k + 1, 16, (1 << lg_k) / 2); |
| 104 | + bencher.bench_local(|| { |
| 105 | + let mut union = HllUnion::new(lg_k).unwrap(); |
| 106 | + for partial in black_box(&partials) { |
| 107 | + union.update(partial); |
| 108 | + } |
| 109 | + black_box(union.estimate()); |
| 110 | + black_box(union.to_sketch(HllType::Hll8)) |
| 111 | + }); |
| 112 | +} |
0 commit comments