Julia Evans

Java isn't slow

This is probably obvious to many of you, but I wanted to write it down just in case.

I used to write Java at work, and my Java programs were really slow. I thought (and people sometimes told me) that this was because Java is a slow programming language.

This just isn’t true. This article describes the architecture of the LMAX Disruptor, a messaging library that they use to process 6 million events per second on a single machine. That article is also SUPER FASCINATING and really worth reading. Java is fast like C is fast (really fast). People write databases (Cassandra) in Java!

By “Java is fast”, I mean something pretty specific – that you can comfortably do millions of things a second in Java. This isn’t true for languages like Python or Ruby – in the computers-are-fast game I made, you can see that a pure-python HTTP request parser can parse 25,000 requests a second. I’d expect an optimized Java program to do dramatically better than that.

So if your Java code is doing something easier than processing 6 million events a second, and it’s slow, you can maybe make it faster! Processing 6 million events per second is actually extremely difficult, but I find it inspiring to think about when I’m having trouble processing like.. 10 things per second :)

My Java code was probably slow because I was creating like a bajillion objects all the time, and destroying them, and doing a bajillion allocations takes time, and also it puts pressure on the garbage collector, and… Well, you get the idea. But code like that is not the whole world!

There’s a culture of building high-performance Java code out there. I asked on twitter and got linked to this interesting-looking data structures library and this Java concurrency tools repo. And to Netty! Netty is a networking framework (to build webservers, and other things!) in Java. The Netty testimonials page says that trading firms (who we all know care a lot about performance!!) use Netty.

There’s this high performance database connection pool and a page explaining how they made it fast. Chronicle is a key-value store in Java. And all that’s just what I got in 10 minutes from reading a few tweets!

How to make your Java (or Scala, or Clojure) code fast

This is a super small thing, but some people I talked to didn’t know this!

Did you know the JVM ships with a free profiler that can tell you which part of your code is the slowest? It’s called VisualVM. It’s very easy to use and it’s an AWESOME first step to take. Here’s a screenshot of VisualVM profiling VisualVM. It’s spending most of its time on org.netbeans.swing.tabcontrol.TabbedContainer.paint. So it’s mostly working on drawing the screen to show me the results of profiling itself!

(YourKit is better, but VisualVM is free)

It won’t get you to LMAX disruptor speed (that’s much more serious wizardry), but it is a good first step!

I had some slow JVM code at work a while ago. We made it fast. I used VisualVM! So can you :) Computers are fast, and you should expect a lot out of your computer programs.