cats-effect icon indicating copy to clipboard operation
cats-effect copied to clipboard

use `java.lang.ClassValue` instead of `IO.tag`

Open xuwei-k opened this issue 9 months ago • 2 comments
trafficstars

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

xuwei-k avatar Jan 29 '25 00:01 xuwei-k

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.

djspiewak avatar Feb 05 '25 14:02 djspiewak

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.

djspiewak avatar Feb 05 '25 14:02 djspiewak