4 ms·
Getting good performance out of Java is not always easy. One often has to structure code imperfectly to get efficient branching (Java offers only vtable dispatc
by willtim 8y ago
Getting good performance out of Java is not always easy. One often has to structure code imperfectly to get efficient branching (Java offers only vtable dispatch or switch on integers/enums). It also boxes nearly all values leading to cache inefficient pointer chasing. These problems can be avoided, but you'll be working against the language.
- pron 8y agoI am not sure where you're getting these things. Unlike C++, Java can and does inline virtual calls. Also, it boxes nearly all values?
- willtim 8y agoI am "getting these things" from over a decade of working with Java. There are many good things about Java, especially when compared to C++. But the bad things will never get fixed if we pretend everything is perfect. I did say vtable dispatch was efficient, but I might not want to split my logic across classes. A chain of if-else is a linear scan and not efficient. Switch in its current form is too limited. It is well known that Java has no value types (there is a JSR). Even for a primitive type, the collection libraries need to box all values in order to abstract the element type. Every HFT user on Wall Street has had to write their own collections library using custom code generation to work around this.
- atulatul 8y ago-- switch on integers/enums Strings in switch Statements: https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html https://docs.oracle.com/javase/7/docs/technotes/guides/langu...
- willtim 8y agoNot sure that really makes up for the lack of sum types! Supporting a switch on static final references would be a good first step.
- imtringued 8y ago>Java offers only vtable dispatch or switch on integers/enums The JVM has complete knowledge of the class hierarchy. If a method has not been replaced in a subclass then no vtable entry has to be generated and the JVM just uses a static call.
- willtim 8y agoMy point was that often one has to structure code across classes to get efficient branching. Any details regarding optimisation for virtual dispatch doesn't change this fact.