[External] : Re: RFR: 8291769: Translation of switch with record patterns could be improved
Jan Lahoda
jan.lahoda at oracle.com
Sun Aug 7 09:17:16 UTC 2022
On 07. 08. 22 10:02, forax at univ-mlv.fr wrote:
>
>
> ------------------------------------------------------------------------
>
> *From: *"jan lahoda" <jan.lahoda at oracle.com>
> *To: *"Remi Forax" <forax at univ-mlv.fr>
> *Cc: *"Jan Lahoda" <jlahoda at openjdk.org>, "Brian Goetz"
> <brian.goetz at oracle.com>, "compiler-dev" <compiler-dev at openjdk.org>
> *Sent: *Friday, August 5, 2022 11:54:05 PM
> *Subject: *Re: [External] : Re: RFR: 8291769: Translation of
> switch with record patterns could be improved
>
> On 05. 08. 22 22:52, forax at univ-mlv.fr wrote:
>
> [...]
>
> In any case, I'm a bit afraid that we could enhance a translation patch
> forever, always adding new cases that can be optimized. So, I think we
> need to draw a line somewhere where the patch provides a benefit while
> still being testable and reviewable. With a possible separate
> improvements, if they prove to be useful. It is possible I haven't drawn
> the line far enough, but looking at the patch, it seems complex enough.
>
> I think the issue is that a list of pattern is not the right intermediary
> structure to try to optimize the pattern matching, a decision tree is a better
> intermediary representation (this is how pattern matching is usually optimized,
> i've not invented something new :) )
>
> Here is an example of such decision tree:
> https://urldefense.com/v3/__https://github.com/forax/switch-pattern-combinator__;!!ACWV5N9M2RV99hQ!JevAQE8IkRf4n-BMKA2sMTNFfJ5sAyJ1xopR2O9rq_45-xqaHOgirecR8E4zIGB_RgMb27G3tlcHEuGADjk$
>
> I am not really opposed to having an intermediate structure, but I think
> the overall cost of the structure is likely to be significant, and it
> should provide significantly better code to offset that.
>
> I agree.
>
> Looking at the page, I see:
>
> The advantage of a decision tree is that if two patterns have a common
> prefix, the code for the common prefix is generated only once.
>
>
> I wonder if there's a *conceptual* difference between what this PR is
> doing and what your code is doing. The PR here is trying to take common
> prefixes (of viable cases) and generate code for them only once. I don't
> think that not optimizing
>
> case A(String s) -> {}
>
> case A a -> {}
>
> is a conceptual difference, but if this is something that is seen as
> critical, I am fairly sure I can implement that without creating a brand
> new structure and code to convert the AST into it and back.
>
> You need the intermediary structure when the common prefix are not subsequent
>
> case A(B b) ->
> case B(A a) ->
> case A(C c) ->
>
> Here the first and the last case shares a common prefix.
>
>
> Sorry, but I don't see why I can't do this particular thing
> without a specialized intermediate representation.
>
>
> The patterns are a partial orders, because we can optimize the ones
> with a common prefix, we need a sort, a decision tree is equivalent to
> a sort.
> There are several advantages to use a decision tree instead of a sort,
> - it's easier to visualize
> - it mimics the the usual hand-written code (you don't repeat the
> instanceofs, you enclose them)
> - i believe that for the code generation, doing local decisions on
> each node is good enough, we do not need to propagate information in
> between nodes.
Yes, the current PR does not do reordering of cases. It could, I just
didn't think of the rules too much in principle, and didn't want to make
the patch overly complex or break something.
I guess I can visualize the AST nodes quite fine.
>
> A bigger question, I think, is when it is safe to do so, and when not.
>
>
> yes, not all re-ordering are safe, we need to keep some constraints.
> Fortunately, the ordering problems only arise when there are guards.
I think it is much more than (complex) guards. Like, for example:
record R(Object o) {}
interface I {}
case R(String s) -> {}
case I i -> {}
case R(Integer i) -> {}
Now, can we change the order of "I" and "R(Integer i)"? If we do and if
we don't, the runtime behavior can differ in case of separate
compilation when "R" is changed to implement "I".
But, even for:
final class A {}
final class B {}
case A a when ... -> {}
case B b -> {}
case A a -> {}
Can we change the order of the cases? I am not sure, because, again,
with separate compilation, this can lead to a different outcome.
And maybe the answer is that we can do reordering in these cases. Or
maybe we can in some of these cases, and can't in orders. I don't think
this was ever discussed in depth.
Some of these decisions would be easier if we did the decision at
runtime, of course. Which brings its own problems, as discussed elsewhere.
> The idea is that for each node of the decision tree, you should
> generate the code in that order, first for the record pattern, then
> the code for the guards then the code for the type pattern, in that order.
The current PR first generates (an analog of) the instanceof of the
record type, then code for the nested patterns, and then the guard of
the case. Which allows to join the cases into nested switches (or ifs,
if we go there). Not sure what is wrong with that?
Jan
> If the guards are recorded in the same order as in the pattern
> matching (this is equivalent to say that the sort should be stable in
> term of guards), we should be ok.
>
> As you said, the current spec allows to freely mix type patterns and
> type pattern + guard pattern with the same type, this has to be fixed
> at the spec level.
> I think it's reasonable to introduce an order between the type pattern
> + guard pattern and the type pattern on the same type, because it's
> more readable and because we do not want users to care to much about
> the order of the patterns to get good performance. You should get good
> performance by default, and not have to crawle StackOverflow to learn
> the arcane of finding the "right order" for patterns.
>
> Rémi
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20220807/2a71ec82/attachment-0001.htm>
More information about the compiler-dev
mailing list