In this talk, we’ll explore the main reasons why Java code rarely runs as fast as C or C++
Because Java is rarely employed in domains where CPU cycles is the main constraining factor, so it seldom makes sense to put much effort into writing Java code to be fast?
It didn't actually explore that. It was a nice talk that covers what the JVM can and cannot optimise, but there weren't any direct comparisons with C/C++.
I think cache usage would be one of the primary differing factors.
The description turned out to not be super feasible because most benchmarks out there have already been gamed to death. Instead I focused on techniques to explore and manipulate the output of the JVM for fun and profit.
That's changing the topic. The topic is not "Why do Java programmer seldom put in the effort to make Java run fast?" The topic is "How much effort is required to get Java to run as fast as C?"
It's a bit annoying to see "Java can run as fast as C! >:(" spread around the net at an almost meme-like level. The statement is technically correct only because it is incomplete. Java can run as fast as C for certain types of long-running programs where the JIT can eventually hyper-optimize certain code paths --as long as the GC work is low enough to not cancel out that benefit.
GC is a godsend. But, if performance is a requirement, GC becomes an impediment that must be manually worked-around even today. I recently read the great article Roslyn code base – performance lessons that opens with "Generally the performance gains within Roslyn come down to one thing: Ensuring the garbage collector does the least possible amount of work."
GC is rarely the actual problem...the problem is allocation.
Generally GC-based systems allocate memory using pointer-bumping; they allocate a big slab early in execution and then carve pieces off as needed. Unfortunately this means sequential allocations, whether transient or not, will quickly walk out of a cache segment into another segment. So in order to access that memory, you're forcing the CPU to constantly load from main memory.
In an equivalent C program, if you allocate and free memory in a tight loop, chances are you'll stay in the same cache segment and most of that allocation will never need to leave the CPU.
Platforms and languages that complain about GC being the bottleneck are either doing MASSIVE levels of GC, or they're running on a non-JVM platform that has a less-than-stellar GC.
Not in the slightest true. The reality is good best fit allocators in c run an average of 17 instructions per call.
Yes that's true, but I think it misses another point. C and C++ both also do stack allocation which runs in zero cycles. The memory is pre-allocated and simply accessed by indexing via the stack pointer.
The code I write often makes heavy use of local fixed sized objects such as fixed dimensionality vectors. The C++ stdlib now pretty much specifies that strings use this trick for the "short string optimization", too.
Sadly it is not the plug-ins, /u/swamprunner7 summed up some of the problems Minecraft had in version 1.8. Unless they are hired by Microsoft your 12 year olds wont be able to fix these problems.
TL;DR: Since Notch stopped working on it directly the Minecraft internals create more and more objects per second that have to be cleaned up by garbage collection.
for everyone too lazy to read the article /u/josefx linked: minecraft 1.8 allocates (and immediately throws away) about 50MB/sec when standing still and up to 200MB/sec when moving around.
as a minecraft mod dev i can say, a lot. there are so many internal systems that are just a complete mess, the features coded after notch left are coded better (easier to understand and read, more logical, etc.) but most of these things are badly optimised.
Minecraft really needs value types, and/or much more aggressive escape analysis+scalarization+valueization from the JVM. Seems the Oracle guys concluded that they can't teach the JVM how to automatically convert things into value types so it's up to the programmer to do it.
From what I understand when Notch wrote minecraft he basically did scalar replacement by hand: e.g rather than introduce a wrapper type for a Point3D he just passed around x,y,z as separate variables and method parameters.
I disagree. When 12 year olds write code, there is no real need for JVM/compiler optimization. For instance, optimization techniques like loop unrolling or function inlining are done by the authors themselves.
While Android code is written in the Java language, the runtime is completely different at every level, from the very basics of how JITing works to memory management profiles to performance and so on. This talk would be mostly useless there. If there's anything that does still use J2ME, though, things might be different; I don't know.
165
u/[deleted] Jul 05 '15
Because Java is rarely employed in domains where CPU cycles is the main constraining factor, so it seldom makes sense to put much effort into writing Java code to be fast?