scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Enum ordinals -> tableswitch

Open tpolecat opened this issue 7 years ago • 8 comments

Just following up on a conversation @odersky and I had at SBTB … I was wondering if we could use enum ordinals to emit tableswitches for eligible match expressions. I have had cases where I had to reorder cases as a performance optimization and something like this would have been really helpful.

tpolecat avatar Nov 28 '18 19:11 tpolecat

We don't necessarily need to do the same, but for reference here's how Java does it: http://www.benf.org/other/cfr/switch-on-enum.html, it's complicated because it's designed to survive reordering and addition of fields to the enum.

smarter avatar Nov 29 '18 13:11 smarter

This is a must done,take a look at :

https://github.com/akka/akka/pull/25982/files#diff-ac929e6fd84743df4b6e214b5499d644R423

for why...

He-Pin avatar Dec 01 '18 22:12 He-Pin

@hepin1989 without benchmark results you won't convince us that it's a "must done"

smarter avatar Dec 02 '18 11:12 smarter

Same patterns in scala future,zio, etc

He-Pin avatar Dec 02 '18 21:12 He-Pin

and we can't rely on mima because definitions are order sensitive

bishabosha avatar Oct 09 '20 12:10 bishabosha

There is some discussion this issue that may help whoever picks this up in the now closed-as-duplicate ticket: https://github.com/scala/scala3/issues/23314

nmichael44 avatar Jun 09 '25 20:06 nmichael44

Even Kotlin does this.

Most damning of all.

som-snytt avatar Jun 09 '25 21:06 som-snytt

@som-snytt 😃 I didn't really mean it as a slight for the Kotlyn compiler. I just meant, it's a newer language so you wouldn't expect the compiler to be that mature and contain this kinds of optimizations.

nmichael44 avatar Jun 10 '25 08:06 nmichael44