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. @kimbarrett TIL about that builtin, updated the PR description to mention that instead. Thanks very much. @AlanBateman Another potential usage I mentioned in the JBS issue is that `GlobalSession` is noncloseable, but there is no way for the accessor to know that without doing a checkcast. Using this we can eliminate the check if the session is statically known to be a global session without imposing additional checks on other kinds of memory sessions. ------------- PR Comment: https://git.openjdk.org/jdk/pull/17527#issuecomment-1906549618