-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Unoptimized code generated for Enum dispatch #23314
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
Comments
A long-standing request, which is to say, an old request for a contribution. Is GSOC set for this year? |
The Java version is described here: #5537 (comment). It uses a table switch, but is a lot more complex than a simple switch on ordinal. What exactly does Kotlin do? As I understand it, there's a trade-off between code simplicity and speed vs separate compilation guarantees. |
@odersky @som-snytt Here is the same function in Kotlin and the code generated.
Generated code for
|
Thanks for reporting this! So Kotlin does use a simple table switch based on ordinal. Whereas Java jumps through a lot of hoops, involving one inner class for each enum switch, to ensure that reorderings of enums don't break binary compatibility. Personally, I am not convinced binary compatibility under re-orderings is necessary here. Reordering an enum changes the |
@odersky I agree with your reasoning. Doing this optimization is a good change. |
Kotlin does the same as Java. The The static construction of that table is laborious but happens once. My naive understanding is that it's a small price compared to a code comment
There is such a code comment to preserve "error ID" for compiler |
@odersky Here is the decompiled code for the Kotlin jar file. @som-snytt is correct that Kotlin does the same as Java, except that it's not a class per enum, it's one array per enum and one class per file (I had more enums in my kotlin file and all the arrays were constructed in the same class
|
That is there for a completely different reason. The authors of the file wanted to use an enum and ensure at the same time that the printed error number stays the same forever. It's certainly a dubious idiom, I was not a fan when it was introduced. I am also very skeptical about not using ordinal in enums. If that's so bad, why is it not deprecated? |
There is no construct for "deprecated if you don't know what you're doing." A lint could ask, "Did you intend to use notify and wait?" (as an example of API that is not deprecated but is not intended for casual usage). |
When generating code for a simple Enum, the compiler generates a huge chain of
if-else-if
statements instead of a tableswitch. The same problem exists for more general ADTs of course (and it's more complicated to fix this for those since we would have to generate a hidden tag etc), but for just plain enums I would expect this to work. Everything is there for you - just detect this case (of a simple Enum), call ordinal() on it and index into the table. Even Kotlin does this.Compiler version
"3.6.4"
Minimized code
Output
Expectation
The text was updated successfully, but these errors were encountered: