[
https://issues.apache.org/jira/browse/GROOVY-12405?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18114741#comment-18114741
]
ASF GitHub Bot commented on GROOVY-12405:
-----------------------------------------
paulk-asert opened a new pull request, #2929:
URL: https://github.com/apache/groovy/pull/2929
…ements
A statically compiled switch expression already emits tableswitch or
lookupswitch when the selector and labels are constants of a type javac would
switch on. A switch statement never has, in any Groovy release, so the colon
form compiles to a chain of isCase calls that is linear in the number of arms:
@CompileStatic
String m(int i) {
String r = 'many'
switch (i) {
case 1: r = 'one'; break
case 2: r = 'two'; break
case 3: r = 'three'; break
}
r
}
// was: 3 x ScriptBytecodeAdapter.isCase, now: tableswitch
javac emits a jump table for the statement form as readily as for the
expression form, so this is Java alignment as much as an optimisation.
The emitters are shared rather than copied: SwitchDispatchWriter now holds
the arm grouping, the int and string dispatch and the arm layout that
StaticTypesSwitchExpressionWriter used to own, and both writers drive it. Only
what differs stays with each caller, which for a statement is that nothing is
thrown when no arm matches.
Statement semantics are preserved. Arms are laid out in source order, so one
that does not jump falls into the next and then into the default, exactly as
the sequential chain does. Labelled and unlabelled break, continue, return and
throw are unaffected because they compile the same way in either layout. A null
selector takes the default.
The dispatch applies only where the type checker's inferred type and every
label agree; anything else keeps the sequential chain, including repeated
labels, which a jump table cannot express and which the type checker does not
reject for a statement.
> No tableswitch/lookupswitch dispatch for switch statements
> ----------------------------------------------------------
>
> Key: GROOVY-12405
> URL: https://issues.apache.org/jira/browse/GROOVY-12405
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Priority: Major
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)