On Tue, 23 Jan 2024 16:01:05 GMT, Aleksey Shipilev <sh...@openjdk.org> wrote:

>> Would it be possible to list further examples where this might be used? 
>> Asking because I'm wondering about the usability and maintainability of 
>> if-then-else code.
>
>> Would it be possible to list further examples where this might be used? 
>> Asking because I'm wondering about the usability and maintainability of 
>> if-then-else code.
> 
> A similar thing is already used in JDK: 
> https://github.com/openjdk/jdk/blob/2a01c798d346656a0ee3553c0964feab75b5dfb6/src/java.base/share/classes/java/lang/invoke/Invokers.java#L622-L624
> 
> Extending this for more common use allows doing things like optimizing 
> `Integer.toString(int)`:
> 
> 
>      @Stable
>      static final String[] CONST_STRINGS = {"-1", "0", "1"};
> 
>      @IntrinsicCandidate
>      public static String toString(int i) {
>         if (isCompileConstant(i) && (i >= -1) && (i <= 1)) {
>             return CONST_STRINGS[i + 1];
>         }
>         ...
> 
> 
> Note how this code would fold away to one of the paths, depending on whether 
> the compiler knows it is a constant or not. Generated-code-wise it is a 
> zero-cost thing :)

> @shipilev Thanks a lot for the detailed reviews and suggestions, I hope I 
> have addressed all of them. 

Sure thing, I just effectively merged my draft implementation into yours :)

-------------

PR Comment: https://git.openjdk.org/jdk/pull/17527#issuecomment-1906602556

Reply via email to