RFR: 8291966: SwitchBootstrap.typeSwitch could be faster [v3]

ExE Boss duke at openjdk.org
Mon Jun 5 21:04:10 UTC 2023


On Wed, 31 May 2023 14:05:56 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> Jan Lahoda has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains six commits:
>> 
>>  - Reflecting review feedback.
>>  - Merge branch 'master' into JDK-8291966
>>  - Adding comments
>>  - Improving performance
>>  - Merge branch 'master' into JDK-8291966
>>  - 8291966: SwitchBootstrap.typeSwitch could be faster
>
> This patch is intended to eliminate some consecutive unnecessary tests like in case like:
> 
> switch (o) {
>     case Runnable r when ... -> {}
>     case Runnable r when ... -> {}
>     case Runnable r when ... -> {}
>     case Object o -> {}
> }
> 
> 
> If `o` is not a `Runnable`, the `instanceof` will only happen for the first case, and the rest will be skipped, as these tests could not pass. But (as a current limitation), if it is not a consecutive run, the duplicate `instanceof` checks will still happen.
> 
> I am quite sure there are ways to improve the bootstrap further, but might be better to have some (more) real-world examples to know what to optimize for.

@lahodaj
> This patch is intended to eliminate some consecutive unnecessary tests like in case like:
> 
> ```java
> switch (o) {
>     case Runnable r when ... -> {}
>     case Runnable r when ... -> {}
>     case Runnable r when ... -> {}
>     case Object o -> {}
> }
> ```

I would expect that the above code would produce bytecode equivalent to:

loop: for (int _i = 0;;) {
	switch (invokedynamic typeSwitch(o, _i) { Runnable.class, Object.class }) {
		case 0 /* Runnable */ -> {
			_i++;
			Runnable r = (Runnable) o;
			if (...) {
				break loop;
			} else if (...) {
				break loop;
			} else if (...) {
				break loop;
			}
			continue loop;
		}
		case 1 /* Object */ -> {
			_i++;
			break loop;
		}
		case -1 -> throw new NullPointerException();
		default -> throw new MatchException();
	}
}

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

PR Comment: https://git.openjdk.org/jdk/pull/9779#issuecomment-1577468813


More information about the core-libs-dev mailing list