New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiment with lookupswitch based pattern matching on Strings or type tests #285
Comments
Implementation wise, we could create a dummy (or macro?) method like
|
First, we can try to manually perform this translation in a few of the hottest pattern matches ( |
I've tried experimenting with |
Yeah, that's not the easiest hierarchy to work with. I think this patch will do it: https://github.com/scala/scala/compare/2.12.x...retronym:topic/switcheroo?expand=1 |
That encoding would be a disaster for Scala.js. In fact it would even break code for people who use |
This is a far fetched experiment at this stage, but thanks for the heads up :) |
The change to |
I think scala\dotty are really bad benchmarks for this, they are memory bound and speedups through better local instructions can be hidden by this. |
I have a working-but-not-too-pretty implementation of generating a switch over strings in https://github.com/martijnhoekstra/scala/tree/switchstring - this is the first time I touched the compiler, so I probably did everything wrong and broke everything |
We should think about this in the context of the Java 10 pattern matching implementation. Eventually, we could share bootstrap methods that expand the switches. Even before then, we could copy the approach into our own bootstrap. An advantage of expanding the switch at link time is that you don't need to bake knowledge of string hashcodes or enum ordinals into the compiled code. The former is specced and can' |
@retronym Is there any implementation of the Java 10 pattern matching available yet? I saw JEP 305 (http://openjdk.java.net/jeps/305) but it didn't give me much to hold on to. Trying to generate the bodies of the cases of string hash matching - particularly the if/else chain needed to handle collisions - proved too hard for me. |
Development if happening in http://hg.openjdk.java.net/amber/amber/langtools/branches. I haven't built it myself yet. |
Landed in scala/scala#8451 / 2.13.2. |
Large pattern matches on string literals would benefit from the javac translation, which implements a switch on the hashcode.
We could extend this approach to type tests on final classes, by switching on the hascode of the class fully qualified name. @martijnhoekstra prototyped this in https://github.com/martijnhoekstra/switchbench (although at the time of writing I'm not sure he's pushed the latest commits)
In gittter, he states:
The text was updated successfully, but these errors were encountered: