cats-effect
cats-effect copied to clipboard
use `java.lang.ClassValue` instead of `IO.tag`
I think java.lang.ClassValue is faster than current implementation.
- benchmark code https://github.com/xuwei-k/cats-effect/commit/f7c2dabfd974bcfb57cff683a360ef7c163a4487
- https://github.com/scala/scala/pull/9632
Jmh / run -i 10 -wi 10 -f 1 -t 1 cats.effect.benchmarks.TagBenchmark
[info] Benchmark Mode Cnt Score Error Units
[info] TagBenchmark.classValue thrpt 10 10348652.195 ± 116363.873 ops/s
[info] TagBenchmark.tag thrpt 10 7336411.108 ± 91988.686 ops/s
Oh this is incredible and I didn't know about it. What this allows us to do is eliminate a megamorphic virtual dispatch. The replacement looks to be a tableswitch. Without digging into the JIT output, I can only guess about how it gets inlined, but clearly this gives the runtime a lot more information to work with. Very nice.
The linked scala/scala PR is really interesting. I did a similar set of benchmarking (though more specialized to IOFiber and obviously not including ClassValue) and found that virtual dispatch fell behind tableswitch more or less across the board. I suspect there's a subtle interaction with the inliner here.