|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +extern crate arrow; |
18 | 19 | #[macro_use] |
19 | 20 | extern crate criterion; |
20 | 21 |
|
21 | 22 | use criterion::Criterion; |
22 | 23 |
|
23 | | -extern crate arrow; |
24 | | - |
25 | 24 | use arrow::array::*; |
26 | 25 | use arrow_buffer::i256; |
27 | 26 | use rand::Rng; |
@@ -207,40 +206,47 @@ fn array_from_vec_benchmark(c: &mut Criterion) { |
207 | 206 | }); |
208 | 207 | } |
209 | 208 |
|
210 | | -fn gen_option_vector<TItem: Copy>(item: TItem, len: usize) -> Vec<Option<TItem>> { |
211 | | - hint::black_box( |
212 | | - repeat_n(item, len) |
213 | | - .enumerate() |
214 | | - .map(|(idx, item)| if idx % 3 == 0 { None } else { Some(item) }) |
215 | | - .collect(), |
216 | | - ) |
| 209 | +fn gen_option_iter<TItem: Clone + 'static>( |
| 210 | + item: TItem, |
| 211 | + len: usize, |
| 212 | +) -> Box<dyn Iterator<Item = Option<TItem>>> { |
| 213 | + hint::black_box(Box::new(repeat_n(item, len).enumerate().map( |
| 214 | + |(idx, item)| { |
| 215 | + if idx % 3 == 0 { |
| 216 | + None |
| 217 | + } else { |
| 218 | + Some(item) |
| 219 | + } |
| 220 | + }, |
| 221 | + ))) |
217 | 222 | } |
218 | 223 |
|
219 | 224 | fn from_iter_benchmark(c: &mut Criterion) { |
220 | 225 | const ITER_LEN: usize = 16_384; |
221 | 226 |
|
222 | 227 | // All ArrowPrimitiveType use the same implementation |
223 | 228 | c.bench_function("Int64Array::from_iter", |b| { |
224 | | - let values = gen_option_vector(1, ITER_LEN); |
225 | | - b.iter(|| hint::black_box(Int64Array::from_iter(values.iter()))); |
| 229 | + b.iter(|| hint::black_box(Int64Array::from_iter(gen_option_iter(1, ITER_LEN)))); |
226 | 230 | }); |
227 | 231 | c.bench_function("Int64Array::from_trusted_len_iter", |b| { |
228 | | - let values = gen_option_vector(1, ITER_LEN); |
| 232 | + let values = gen_option_iter(1, ITER_LEN); |
229 | 233 | b.iter(|| unsafe { |
230 | | - // SAFETY: values.iter() is a TrustedLenIterator |
231 | | - hint::black_box(Int64Array::from_trusted_len_iter(values.iter())) |
| 234 | + // SAFETY: gen_option_iter is a TrustedLenIterator |
| 235 | + hint::black_box(Int64Array::from_trusted_len_iter(gen_option_iter( |
| 236 | + 1, ITER_LEN, |
| 237 | + ))) |
232 | 238 | }); |
233 | 239 | }); |
234 | 240 |
|
235 | 241 | c.bench_function("BooleanArray::from_iter", |b| { |
236 | | - let values = gen_option_vector(true, ITER_LEN); |
237 | | - b.iter(|| hint::black_box(BooleanArray::from_iter(values.iter()))); |
| 242 | + b.iter(|| hint::black_box(BooleanArray::from_iter(gen_option_iter(true, ITER_LEN)))); |
238 | 243 | }); |
239 | 244 | c.bench_function("BooleanArray::from_trusted_len_iter", |b| { |
240 | | - let values = gen_option_vector(true, ITER_LEN); |
241 | 245 | b.iter(|| unsafe { |
242 | | - // SAFETY: values.iter() is a TrustedLenIterator |
243 | | - hint::black_box(BooleanArray::from_trusted_len_iter(values.iter())) |
| 246 | + // SAFETY: gen_option_iter is a TrustedLenIterator |
| 247 | + hint::black_box(BooleanArray::from_trusted_len_iter(gen_option_iter( |
| 248 | + true, ITER_LEN, |
| 249 | + ))) |
244 | 250 | }); |
245 | 251 | }); |
246 | 252 | } |
|
0 commit comments