On Thu, 13 Apr 2023 10:59:46 GMT, ExE Boss <d...@openjdk.org> wrote: >> Hi Christian, >> >> Thanks for the comment. >> >> First, the "Dumbest possible strategy" comment should be interpreted as "we >> would like to do something better", not that using something slow is OK >> permanently. There's an attempt to do performance improvements here: >> https://github.com/openjdk/jdk/pull/9779 >> >> Talking of `enumSwitch` for now (some more on `typeSwitch` later), one thing >> what I would like allow for the long(er) term are fast(er) implementations. >> If we just used `String` comparison, it would be extremely difficult to get >> a really good performance, even for cases where e.g. all the input values >> are enum constants, where the method can be reduced to a map lookup (as 9779 >> does). >> >> I think we could probably still avoid the "eager" class initialization, at >> the cost of using the `MutableCallSite` - first, we would produce a >> temporary MethodHandle, and on the first non-`null` call (when the class >> apparently must be initialized), we would produce the real (possibly >> optimized) MethodHandle. >> >> I suspect (although I may be wrong) that the eager systems may not be able >> to work with such a call site, but those should be able to replace the >> bootstrap with a custom static version without changing the semantics in any >> way. >> >> A slightly bigger problem is `typeSwitch`: a new feature is that enum >> constants can be part of any pattern matching switch. Like: >> >> >> enum E {A} >> ... >> Object o = ...; >> switch (o) { >> case E.A -> ... >> ... >> } >> >> >> For this, even `typeSwitch` permits enum constants now, and >> `java.lang.invoke.ConstantBootstraps.enumConstant` is used for that >> currently. We would probably need a wrapper to describe the enum constant >> that could be used by `typeSwitch` to avoid initialization (+the >> `MutableCallSite`, of course). > > Note that currently, a `switch` over an enum will already trigger > initialisation of the enum class because of the need to compute the relevant > mapping array by the synthetic helper class, see [JDK‑7176515] for details. > - https://github.com/openjdk/jdk/pull/10797 > > [JDK‑7176515]: https://bugs.openjdk.org/browse/JDK-7176515 "[JDK‑7176515] > ExceptionInInitializerError for an enum with multiple switch statements"
FWIW, a sketch on how avoiding the enum initialization might look like is here: https://github.com/lahodaj/jdk/compare/JDK-8300543...lahodaj:jdk:JDK-8300543-lazy-enum?expand=1 more work needed to make that work for `typeSwitch`, and to combine that with #9779. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/13074#discussion_r1165427916