4 ms·
> 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 sub
by 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?