Strings in Switch
Mark Mahieu
markmahieu at googlemail.com
Wed Dec 9 07:37:04 PST 2009
On 9 Dec 2009, at 10:53, Reinier Zwitserloot wrote:
> As I understand it, switch-in-strings is handled during the "lower" phase of
> javac, which must desugar the string switch into legal java code.
Hmm, that's not quite how I understand it.
I picture the "lower" phase as a bridge between the side of javac which is concerned with the Java Language spec (parsing, flow analysis etc), and the side which deals with the VM spec (ie. bytecode generation). Its existence means that neither side need be unnecessarily complicated by details of the other.
So, its input is syntax trees which are valid as far as the language spec is concerned, and its output is a simpler set of trees which can be used by the "gen" phase to produce valid JVM classes - but that 'simpler' set is not necessarily an exact subset of the trees used by earlier phases; ie. the output need not be directly representable as valid Java *language* code (synthetics and some uses of "let" expressions for example).
> As an example, the above would desugar to (with optional
> switch on string's length during string-to-number conversion omitted. That
> may actually be a good idea; it's straight forward and does have an obvious
> performance benefit):
>
> int $unique;
> if ("Hello1".equals(someString)) $unique = 0;
> else if ("Hello2".equals(someString)) $unique = 1;
> else if ("Hello3".equals(someString)) $unique = 2;
> else $unique = 3;
I'm afraid a translation along these lines is likely to entice end users to attempt premature optimisation by messing with the order of the cases.
But I still don't see the problem with what Joe proposed (months ago) and implemented.
Regards,
Mark
More information about the coin-dev
mailing list