RFR: JDK-8237528: Inefficient compilation of Pattern Matching for instanceof
Jan Lahoda
jan.lahoda at oracle.com
Fri Jan 24 08:45:43 UTC 2020
On 23. 01. 20 18:24, Remi Forax wrote:
>
>
> ----- Mail original -----
>> De: "jan lahoda" <jan.lahoda at oracle.com>
>> À: "compiler-dev" <compiler-dev at openjdk.java.net>
>> Envoyé: Jeudi 23 Janvier 2020 18:10:36
>> Objet: RFR: JDK-8237528: Inefficient compilation of Pattern Matching for instanceof
>
>> Hi,
>>
>> When javac compiles:
>> o instanceof String s
>>
>> it desugars this to:
>>
>> (let /*synthetic*/ final Object s$temp = o in s$temp instanceof String
>> && (s = (String)s$temp) == (String)s$temp)
>>
>> The "(s = (String)s$temp) == (String)s$temp)" is there to set the
>> variable "s" to the correct, and still return/have a boolean value. But
>> this produces several unnecessary instructions for the comparison -
>> second cast and unnecessary test/jump. This can be changed to:
>>
>> (let /*synthetic*/ final Object s$temp = o in s$temp instanceof String
>> && (let s = (String)s$temp; in true))
>>
>> I.e. the (s = (String)s$temp) == (String)s$temp) is replaced with
>>
>> (let s = (String)s$temp; in true)
>>
>> This is a let expression that will execute the assignment as a statement
>> and then return true without any tests. This eliminates the unnecessary
>> cast and test/jump, producing a smaller code (see bytecode before and
>> after below).
>>
>
> Nice !
> You can shave two more bytes (the first two instructions) by detecting that the left part of instanceof is already a local variable, so you don't need the first let.
Right, I was thinking of that, but inclined to not do that, as it is a
little tricky (esp. to test) and we don't do it on other places (like in
switch over string), so it might be a more general effort.
Thanks,
Jan
>
>> Proposed patch:
>> http://cr.openjdk.java.net/~jlahoda/8237528/webrev.00/index.html
>>
>> JBS:
>> https://bugs.openjdk.java.net/browse/JDK-8237528
>>
>> How does this look?
>>
>
> Rémi
>
>> Jan
More information about the compiler-dev
mailing list