3 ms·
If you know that a method isn't virtual, calling that method is just a function call with an extra argument for 'this'. If it's virtual, you have to look it up
by koenigdavidmj 6y ago
If you know that a method isn't virtual, calling that method is just a function call with an extra argument for 'this'. If it's virtual, you have to look it up in a vtable, probably dereferencing two pointers in the process and making it harder on your instruction fetcher.
Java makes a lot of common classes final (e.g. String and Optional) so that it can avoid this. As I understand it, it's also got a lot of pixie dust to predict this some of the time, but then you can't rely on it.
- hinkley 6y agoWhen the JIT was finally smart enough to handle lookups better, they didn’t change the types. Final classes encourage the creation of utility classes, which can often become a problem.