r/programming • • Jul 05 '15

Fast as C: How to write really terrible Java

https://vimeo.com/131394615
1.1k Upvotes

394 comments sorted by

View all comments

165

u/[deleted] Jul 05 '15

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?

125

u/mike_hearn Jul 05 '15

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.

42

u/headius Jul 05 '15

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.

44

u/corysama Jul 05 '15

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."

20

u/headius Jul 05 '15

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.

0

u/[deleted] Jul 06 '15

[deleted]

6

u/serviscope_minor Jul 06 '15

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.

→ More replies (1)
→ More replies (4)
→ More replies (3)
→ More replies (15)

31

u/Modevs Jul 05 '15

Except Minecraft servers :(

32

u/Iggyhopper Jul 05 '15

As soon as 12 year olds write better plugin code no amount of JVM optimisation will help them.

40

u/josefx Jul 05 '15

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.

33

u/itz_skillz Jul 05 '15

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.

18

u/[deleted] Jul 05 '15

Jesus fuck. What the hell's wrong with their code?

29

u/itz_skillz Jul 05 '15 edited Jul 05 '15

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.

→ More replies (1)

8

u/Amelorate Jul 06 '15

Mostly using immutable objects once and then throwing them away many times a second. To be specific the BlockPos class.

7

u/EpikYummeh Jul 06 '15

This /r/minecraft post from 8 months ago contains all the nitty gritty, including responses from Minecraft devs.

→ More replies (1)
→ More replies (2)
→ More replies (1)

5

u/mike_hearn Jul 05 '15

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.

→ More replies (1)

11

u/SnowdensOfYesteryear Jul 06 '15

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.

→ More replies (2)

7

u/defenastrator Jul 06 '15

Or you just use MCServ the third party c/c++ minecraft server that blows the doors off the official server while hosting more players in less memory.

→ More replies (1)

37

u/njtrafficsignshopper Jul 05 '15

Android apps?

41

u/[deleted] Jul 05 '15

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.

0

u/[deleted] Jul 05 '15

[deleted]

21

u/bradmont Jul 05 '15

Yeah, it's outdated. Android 5+ no longer uses the dalvik vm. It's switched to a new one called art.

→ More replies (21)
→ More replies (7)

8

u/PsionSquared Jul 05 '15

Didn't the original Nokia phones run Java?

16

u/ActuallyTheOtherGuy Jul 05 '15

Symbian did, yep

7

u/[deleted] Jul 05 '15

I had a Nokia N73 which ran Symbian and the "apps" on it were quite possibly the most sluggish things ever.

→ More replies (7)
→ More replies (1)
→ More replies (90)