tustvold commented on PR #4632:
URL: https://github.com/apache/arrow-rs/pull/4632#issuecomment-1662933471

   I did a quick and dirty experiment, and it would appear my hunch is broadly 
correct
   
   ```
   fn do_bench() {
      let cmp: &dyn Fn(&str, &str) -> bool = black_box(&|l, r| l == r);
       c.bench_function("eq_simple_dyn", |b| {
           b.iter(|| {
               let l = black_box(&l);
               let r = black_box(&r);
               l.iter()
                   .zip(r)
                   .map(|(l, r)| match (l, r) {
                       (Some(l), Some(r)) => Some(cmp(l, r)),
                       _ => None,
                   })
                   .collect::<Vec<_>>()
           })
       });
   
       let l = create_string_array::<i32>(1024, 0.);
       let needle = l.value(0);
       c.bench_function("eq_simple_scalar", |b| {
           b.iter(|| {
               let l = black_box(&l);
               let needle = black_box(needle);
               l.iter().map(|v| v.map(|x| x == needle)).collect::<Vec<_>>()
           })
       });
   
       let cmp = black_box(make_eq_scalar(needle));
       c.bench_function("eq_simple_dyn_scalar", |b| {
           b.iter(|| {
               let l = black_box(&l);
               l.iter().map(|v| v.map(|x| cmp(x))).collect::<Vec<_>>()
           })
       });
   
       dyn_cmp_dict_benchmarks(c);
   }
   
   fn make_eq_scalar(needle: &str) -> Box<dyn Fn(&str) -> bool + '_> {
       Box::new(move |a| a == needle)
   }
   ```
   
   And got
   
   ```
   eq_simple               time:   [5.4250 µs 5.4278 µs 5.4308 µs]
   
   eq_simple_dyn           time:   [6.5201 µs 6.5227 µs 6.5253 µs]
   
   eq_simple_scalar        time:   [4.4315 µs 4.4334 µs 4.4355 µs]
   
   eq_simple_dyn_scalar    time:   [4.9554 µs 4.9575 µs 4.9600 µs]
   ```
   
   Equality is the cheapest of the string operations and yet the dyn dispatch 
is barely registering. I suspect if you tweaked it to have a signature like 
`Fn([&str; 64, &str; 64)]) -> u64` the overheads would disappear entirely. 
Something worth thinking about possibly...


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to