4 ms·
I think a large part of the difference is because interfaces and polymorphism is effectively free in Java, whereas virtual methods in C++ comes at a cost.
by marginalia_nu 4y ago
I think a large part of the difference is because interfaces and polymorphism is effectively free in Java, whereas virtual methods in C++ comes at a cost.
- Transfinity 4y agoFree except for you've written 3x as much code and it's 10x harder to understand.
- marginalia_nu 4y agoEh, it's a bit boilerplaty, but much of that stuff is typically done through an IDE. Don't know about harder to understand, the entire point is to remove confusing implementation details from callers.
- achikin 4y agoThe issue is that when you are trying to understand/modify someone else code it always comes down to confusing implementation details rather than abstract architecture on top of them.
- deschutes 4y agoI'd rather see how it's implemented. In my experience enterprisy abstractions are a lot of motion without any progress. They impede change and stymie understanding. The cynical part of me thinks that is the whole point.
- Banana699 4y agoVirtual dispatch always has a cost, it's "free" in Java in the sense that it's always done at the VM level, so you might as well just use it, even final methods are just an agreement with the compiler, the VM doesn't care, it will dynamically look up the method in the class hierarchy like God intended. C++ makes it painful and obvious what you're getting yourself into. The JVM is off course very clever and is, I'm sure, doing tons of shenanigans to reduce the cost, but that's not free, that's someone else investing tons of time and effort and complexity to reduce the cost of a fundamentally expensive operation.
- ncmncm 4y agoDefault virtual was among the dumber design mistakes in Java, but it has lots of competition.
- NavinF 4y agoSee my reply to the GP. Default virtual is the only thing that makes sense given how the JVM works.
- ncmncm 4y agoYou understand the JVM was designed at the same time as the language? It could work any way they liked. And does.
- NavinF 4y agoSure, but if you have the same preferences (throughout over latency), you’ll find that there are no performance benefits to be gained from non-virtual functions in any JITed language. The “final” keyword is just there for documentation.
- ncmncm 4y agoVirtual or not isn't about performance, it is about system architecture. Virtual is structurally about implementation. Exactly to the degree that the public interface matches the inheritance interface, the abstraction is a failure. At least, if you are being object-oriented, which Java tries to force on you. Of course, you are free to violate that expectation, and sometimes must since Java offers no other means of organization; so if you do, more power to you.
- deleted 4y ago[deleted]
- imtringued 4y ago
- pkolaczk 4y agoOh yeah, I've heard this so many times - JVM can optimize all that dynamism out. Except in cases when it can't or just won't. The reality is, it is very far from free. Most Java developers are simply not aware of the real cost. Then they are surprised how the code gets 5x speedup and needs 20x less memory after rewriting it to Rust or C++.
- marginalia_nu 4y agoYou don't have the option of telling Java whether to use "that dynamism". All Java calls are virtual calls, in most simple cases the JIT optimizes it out (i.e. single implementer of an interface), sometimes it can't. This isn't just speculation about what the JVM does, you can examine the bytecode being generated by the JIT-process to verify whether this optimization happens. Do you have real-world examples of this 5x speedup from this decade? 20x memory I can sort of see because the GC overhead can be pretty nasty in some edge cases, but I'd expect closer to 1.5-2x speedup from C++ if the Java code is anywhere near well written.
- pkolaczk 4y ago> Do you have real-world examples of this 5x speedup from this decade? Does https://github.com/pkolaczk/latte https://github.com/pkolaczk/latte count? Or the Optional cost described on my blog here: https://pkolaczk.github.io/overhead-of-optional/ https://pkolaczk.github.io/overhead-of-optional/? (I have more such examples, but many I can't share). > you can examine the bytecode being generated by the JIT-process to verify whether this optimization happens. And what do I do with that knowledge if it turns out the optimization didn't happen?
- marginalia_nu 4y ago> Does https://github.com/pkolaczk/latte https://github.com/pkolaczk/latte count? This is just Rust code. Where is the equivalent Java code? Like the 50x-100x memory consumption is highly suspicious. If Java uses 50x the memory consumption compare to C++, how come I can allocate a long[SINT_MAX-10] on a system machine with 32 Gb of RAM? Shouldn't the process require of order 0.8 Tb of RAM if this statement is correct? Or can C++ allocate a 2 bn array of longs in 40 Mb of RAM? If so let me know, I would be very interested in using this novel compression technology. > Or the Optional cost described on my blog here: https://pkolaczk.github.io/overhead-of-optional/ https://pkolaczk.github.io/overhead-of-optional/? Why aren't you using OptionalLong[1]? You shouldn't use Optional<Long>, that's never a good choice. At any rate, nobody should be claiming Java optionals are are free, they're a high level abstraction and absolutely do not belong in hot codepaths. In general it's fairly easy to construct benchmarks that favor any particular language, which is why you constantly see these blog posts about how high level interpreted languages (JS, PHP, Haskell) are faster than C++. You can easily construct "comparisons" that make JVM languages look superior to C++ as well, just carelessly allocate throwaway objects of different sizes and lifetimes (like you can in Java), oh no, why is C++ slowing down? Surely there's no heap fragmentation! That's a bad faith benchmark though. It doesn't really demonstrate anything other than that C++ following Java idioms isn't very good. > And what do I do with that knowledge if it turns out the optimization didn't happen? The way the JIT works is by aggressively overassuming, and then recompiling with more generalized interpretations of the code when assumptions turn out to be false. But the wider problems of compilers occasionally generating suboptimal instructions isn't something that is Java specific. [1] https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/OptionalLong.html https://docs.oracle.com/en/java/javase/12/docs/api/java.base...