3 ms·
I was wondering this morning, with regards to the pattern matching for switch: Is there a performance consideration here? When using sealed classes, can the J
by bcrosby95 4y ago
I was wondering this morning, with regards to the pattern matching for switch:
Is there a performance consideration here? When using sealed classes, can the JVM do a better job optimizing pattern matches for switch? Are there also considerations with how it interacts with Project Valhalla?
The team evolving Java seem pretty good at evolving the language with higher level goals in mind, so I wasn't sure if there were interrelated factors here.
- tadfisher 4y ago> When using sealed classes, can the JVM do a better job optimizing pattern matches for switch? Yes! In the Pair<I> example from the JEP, if the number of subclasses of I is large enough, it may be worth generating a perfect hash function [0] to match the pattern in O(1) time instead of the naive O(n^2). This is only possible in the general case for exhaustive switch statements, which sealed classes allow for with instanceof-style patterns. [0]: https://en.wikipedia.org/wiki/Perfect_hash_function https://en.wikipedia.org/wiki/Perfect_hash_function
- debug-desperado 4y agoSo, has perfect hashing dispatch like you're describing here actually been added (or planned) in the JIT?