Debunking the Java Performance Myth
medium.com
medium.com
So the claim that Java is slow is true. That's why so many people hate it. The fact that Java is fast is also true.
It's the rise of these languages that confuses me.
They somehow got away with the 'fast enough' argument whilst Java is constantly being compared to languages that are designed for fine tuning for the target architecture.
In my mind it is Java that is 'fast enough' and,with Java 8, very expressive. The continued success of these incredibly slow interpreted languages in enterprises where you need to buy servers feels like an anti-pattern.
It was easy to find benchmarks showing that Java was fast. But Java turned out to be slower in every situation we might consider using it.
Command-line applications were slower because the JVM took ages to start.
GUI applications were slower, apparently because Python would use C-implemented widgets while Java would do much of the drawing itself.
Web applications were slower without any good excuse as far as I could see, but something in the application server that was the mainstream way of doing things added more per-request overhead than starting a whole new Python interpreter via CGI.
CPU-intensive operations (I remember image resizing in particular) were slower because, again, you ended up comparing the JVM against a well-written C extension.
Java marketing was a disaster. Remember when it was going to be the replacement for browsers? It was not a great way to engender goodwill.
Random anecdote which led me to stop calling Java slow: I'll never forget the time I took a 3000ish line Python program which had been heavily performance tuned and thus completely impenetrable. I converted it to a dumbest-scheme-possible naive implementation in Java weighing in at roughly 100 lines , expecting that to be a first pass. It turned out that the Java implementation was many times faster so we called it good enough and moved on with life. Obviously YMMV and all of that
CherryPy fits nicely into this window of opportunity, but as soon as you have to go back to your boss and say "I think we need a bigger boat" someone should be asking questions about library and language choices, at the very least there should be a conversation about prototypes vs production architectures.
Similarly, Python and the dynamic languages are 'fast enough' because in the 90s the bottleneck was IO, not CPU. That is still true, but is becoming less true. SSDs rapidly closed that gap, and it looks like memristor storage could mean that non-volatile RAM is as large as SSD and as fast as DRAM is today.
The dynamic language decision to focus on single threaded performance is also smart, but that will stop being true when CPU have hundreds of cores, instead of 10s. Synchronized access to cache is 100x slower than regular access. So to see benefit for work loads other than the "embarrassingly parallel" with multi-core you need around 100 threads. The new competition in the CPU market might see the first 100 core die on the market in 5 years.
The trend towards 'serverless' could be even worse for dynamic/interpreted languages. Smaller slices of billing will show that the same service written in Java vs Python cost 5-10x less. That was harder to show when you had to pay for at least two servers for every deployed service, now it will be very clear to CFOs how much language choice affects their bottom line.
What this means is that the perceived difference in performance between Java and Python can only grow over the next 5-10 years. If Java continues to gross you out, I'd be looking to pad your resume with something like Clojure / Go / Rust. High productivity, high performance languages are here and their relevance is growing.
"A JVM does that???" by Cliff Click is a good introduction on what can go in the background.
C++ can always win!
JVM has hotspot? use profile guided optmization in C!
There is no escape!
1) Swing. I have no idea what this does on startup but it is bad. This is what killed most java GUI apps. A simple AWT or SWT app will pop open while a simple swing app will take a few seconds to get started
2) Jars on systems with virus scanners. Jars are just zip files. Many virus scanners will unzip and scan a zip on every open which leads to each jar being unziped at least 2 times and often more if the app has to load more things from the jar at a later time.
3) Spring. Really spring is just a bad idea all the way through and most of the reason people hate java is Spring. Huge XML configuration files instead of code? Spring (mostly annotations now). Stupid names like AbstractSingletonProxyFactoryBean? Spring. That aside, on startup Spring will auto generate classes which really slows things down. This could have been done during the last build but spring does it at each startup which is really slow. Spring Boot has helped with startup times but it is still slower.
After startup Spring continues to kill performance. The underling code is full of maps to control request lifecycle so instead of just calling a method it has to ask "who all is configured to do something at step PreParseRequestEncoding?" then again at ParseRequestEncoding, PostParseRequestEncoding, PrePareRequestBody, ParseRequestBody.... Not real names but the idea and number of steps is. Most of the time the answer is "nobody" but it has to ask for every step that might ever need something done.
Add on top of this JPA which is super easy but quickly breaks down to a lot of DB calls. JPA is great for people who don't want to learn SQL and simple CRUD apps but anything with any amount of data will quickly slow down to thousands of DB round trips.
I'd like to see some reallife-ish comparisons with Jooq
That's like saying Java is for people who don't want to learn C...or C is for people who don't want to learn assembly.
It's the right tool for the right job. If you use it for everything, it's no so good.
> Huge XML configuration files instead of code? Spring (mostly annotations now).
Spring 3, yes. Spring 4 and 5, no. XML has not been the blessed configuration approach for ~4 years.
> Stupid names like AbstractSingletonProxyFactoryBean? Spring.
Java is a language that has evolved enormously in expressiveness since Spring began, meaning heavyweight Go4 patterns were necessary early on to maintain a flexible substrate with a uniform interface serving very many use cases? Spring.
> That aside, on startup Spring will auto generate classes which really slows things down.
I believed this urban legend too.
Dave Syer, who is understandably interested in Spring criticism, has done more empirical investigation than anyone on this topic[0].
The tl;dr is that boot time is proportional to total classes loaded. That's all, that's it.
In-memory reflection operations are hilariously, stupidly, insanely faster than I/O. The JVM fetches and loads classes individually. So if you have a lot of classes, it takes a longer time to load.
The other thing to bear in mind is that Spring relies on this much less than it used to. As the language and JVM have evolved, so has Spring.
> The underling code is full of maps to control request lifecycle ... Not real names but the idea and number of steps is.
Are you talking about servlet filters? Because any web framework is going to have a few of these. Spring MVC adds a handful and all of them can be removed, replaced or added to. Spring Security adds a bunch and all of them can be removed, replaced or added to. But the defaults are chosen because of feedback, not just for the heck of it.
> Add on top of this JPA which is super easy but quickly breaks down to a lot of DB calls. JPA is great for people who don't want to learn SQL and simple CRUD apps but anything with any amount of data will quickly slow down to thousands of DB round trips.
In general, yes, I agree that JPA is a PITA. For a small to medium system I would try to avoid it where possible. For a sufficiently large codebase -- thousands of domain objects, dozens or hundreds of programmers -- it might be a necessary evil.
Mind you, if you want to see a world where the database goes from abused to outright mocked and ignored, pay a visit to Rails land. It drives me batty.
[0] https://github.com/dsyer/spring-boot-startup-bench, particularly https://github.com/dsyer/spring-boot-startup-bench/tree/mast...
The biggest issue I have with spring isn't performance but is the huge internal state that is the basis of Spring's DI.
After a few versions you end up with code that depends on bean named 'Abd" to preform a task, but in the next version is renamed to "Abc" which fixes the typo, but makes all the documentation off. And the 3 other classes that also needed that task still look for it under the name "Abd". And then the next major version replaces this entire module and the bean name is now "Xyz". To me this IS spring programming. Googling the DI names and trying them one by until you get the desired run time functionality.
I've had apps in production with unused beans defined (It only had 1 function that only throw an exception and was never called) but had to be defined or something would break. Multiple team members wasted some free time trying to locate the code that was requiring that bean but nobody could ever figure it out. At some point everybody gave up, it was easier to just let it be.
Most developers have little to no idea what is actually going on inside their spring apps (or even what half their dependencies are). When a struts like vulnerability comes along in the spring world it is going to be a massive PITA to fix. With how complex this all is I have little doubt that a vulnerability exist somewhere in there.
Which Spring Boot ameliorates by providing starter POMs. Curated, levelled, updated collections of dependencies for common cases. No need to play whack-a-dep with Maven or Gradle. No need to track 50 different dependencies yourself.
The thing is: I don't care how Spring does the magic. I care that I don't have to care.
I came to Spring and Java-for-real development relatively late -- by fluke I was on what is almost certainly the first Boot production app ever deployed, back in early 2014.
Later I got a chance to see the primordial world of Spring 3. I understand the residual hate.
One thing that I have noticed is that with all these annotation, the Java compiler is not doing much any more. Java has effectively became a dynamic language (as in an interpreted language) where errors are only visible at runtime when hitting some specific method.
Coupled with the really slow startup time (again, I talk about Spring Boot--I get a steady 10s vs 0.01s for Go, same functionality), it makes developing a simple CRUD a pain compared to both Go or Python/Ruby...
For Ruby I see RubyMine doing what amounts to a fulltext search. It's next to useless.
For startup time, see my notes on the JVM and the link to Dave Syer's notes.
There's also the devtools starter to ease the JVM reload pain. Which to me is by far the biggest suckiness of the thing.
Even ignoring filters, it seems to me that Spring brings a lot of overhead. Adding an annotation to a method sometimes means that would appear to be a direct call between two of your beans is in fact separated by 10 method calls on a stack trace. In many cases this is not an issue (expecially in "enterprise" applications), but it is clear to me that performance is not really one of the main concerns of spring developers.
I generally try to stay away from strong opinions and assume that when something has an opinion there may be a reason behind it, but in the case of Spring and Java, I have concluded that those not using or familiar with Spring are simply clueless jokers.
If you really believe that JPA means "you don't have to understand SQL", then you definitely don't know what you're doing.
As for Spring, I get so tired of reading lengthy critiques that boil down to:
1. "It uses XML! (or at least it did 10 years ago, when my personal experience with it was last up-to-date)"
2. "It has some classes with long names! Named after design patterns that I don't like!"
By all means, don't use Spring if you don't want to. But know that Spring is very modular, letting you include or exclude whatever you like, and the core is rather light. If you're not using at least some of it, then you're probably re-writing it... and doing a worse job than they did. Either way, stop ranting about it on web forums if your knowledge is from 2007.
Is "the server" the only place where code is run today? That is a sad image for privacy and user sovereignty. I certainly still run a lot of code on my local system, and I wish more people did the same.
I've heard the "you're probably re-writing it... and doing a worse job than they did" argument so many times, is this on the spring forums or something?
But these days, Java VMs are vastly better and Java is competing with languages like Python and Ruby. The way things are today, the assumption is that Java is faster than its competition.
For desktop applications you suffer some more from innate disadvantages of JITs and the common graphics libs. People experience Java when using eclipse and minecraft and so perceive it as slow. When a java server provided data fast to a wevsite, people don't know that is java.
(#1 mentioned since it's one of the two languages compared to Java in TFA).
I guess one way would be to implement the things I actually use myself with less abstractions but that honsetly feels very daunting to me. The Java ecosystem is so vast and between Handling the Requests, DB access (with or without ORM), persistance, caching, security, I really have no clue on where to even start doing something like this myself. If someone more seasoned here has some input, I would be highly interested!
Where Spring is an "everything but the kitchen sink" framework, Dropwizard is just a collection of best-in-class libraries that are easily swapped out if you prefer something else.
I don't get to do so often professionally, but if given my choice of tools ill generally opt for a combination of Dropwizard in Kotlin.
This is nice because it makes it easier to treat your servers like disposable cattle when (almost) all of your configuration and dependencies live in your easily deployed Jar.
Spring boot feels like it was from a room full of people where there's a guy in the corner with a form of tourettes that makes him keep yelling "Spring! Take the Spring option!"
It just isn't always the best option and sure, you can often switch out for something else but that defeats the point of an opinionated collection of the best available choices.
And if they don't qualify as "experienced Java guys" then I don't know who can, short of Gosling.
You should add the Jackson Kotlin module so you can pass Kotlin data objects around: https://github.com/FasterXML/jackson-module-kotlin
This combination has made me as productive as I've ever been.
A long-standing difficulty with the Java Buildpack was working out how much memory to give it -- the Garden container engine is particularly ruthless to any process going above the allocated memory limit. I believe it got a lot of attention in v4 (edit: see [0] and [1]).
My personal pet peeve is that the memory quota is identical for staging and runtime. Which means that memory-intensive staging operations force you to have wasteful runtime memory allocations. I mostly noticed this when using Rails apps that pull in Nokogiri -- it gets compiled during staging and often that causes a lot of memory usage.
Disclosure: I work for Pivotal, we do the majority of Cloud Foundry engineering. We also sponsor Spring.
[0] https://www.cloudfoundry.org/just-released-java-buildpack-4-...
[1] https://github.com/cloudfoundry/java-buildpack-memory-calcul...
It would still be nice to be have a Spring Boot app work on a 512MB container but even pretty simple ones don't seem to.
I've recently toyed with the idea of taking libbuildpack and OpenJ9 and just ... seeing what I can do. But I'm ill-qualified to tinker with either and it's in the pile of fifty kerjillion other sideprojects I want to do.
Even if some requests needs crunchy CPU power, it's better to offload them into something asynchronous than try really hard to make them perform well enough to be synchronous. Typically, hefty CPU jobs come in varying sizes, they're rarely guaranteed to be runnable under the 200ms or so latency bar for synchronous requests.
IMO this is a sign that you should expand the company you keep rather than being indicative of any flaw with Java.
Fact is, in the Enterprise you'll have to interface with many of the aforementioned technologies, and Java still has the most support for them. We find that even in .net we sometimes have to implement some things on our own from the horribly convoluted "Enterprise" technologies because a specific feature isn't supported.
In Go on the other hand, doing simple 'mistakes' like declaring a variable inside a loop can bring you to performance hell:
Declaration in a loop:
var sum int
for i := 0; i < n; i++ {
x := i + i
sum = x - i
}
Declaration outside the loop: var sum int
var x int
for i := 0; i < n; i++ {
x = i + i
sum = x - i
}
Benchmark results: BenchmarkInLoop-8 2000000000 0.40 ns/op
BenchmarkOutLoop-8 2000000000 0.47 ns/op
And the same is certainly true for C/C++.Src: https://nopaste.xyz/?68c6800acd9200d6#mDstI36uBPBU4Td8k//GNC...
int f1(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
int x = i + i;
sum = x - i;
}
return sum;
}
int f2(int n) {
int sum = 0;
int x;
for (int i = 0; i < n; i++) {
x = i + i;
sum = x - i;
}
return sum;
}
when compiled with 'gcc -O -S' gives: .file "loop.c"
.text
.globl f1
.type f1, @function
f1:
.LFB0:
.cfi_startproc
testl %edi, %edi
jle .L4
movl $0, %eax
.L3:
addl $1, %eax
cmpl %eax, %edi
jne .L3
subl $1, %eax
ret
.L4:
movl $0, %eax
ret
.cfi_endproc
.LFE0:
.size f1, .-f1
.globl f2
.type f2, @function
f2:
.LFB1:
.cfi_startproc
testl %edi, %edi
jle .L9
movl $0, %eax
.L8:
addl $1, %eax
cmpl %eax, %edi
jne .L8
subl $1, %eax
ret
.L9:
movl $0, %eax
ret
.cfi_endproc
.LFE1:
.size f2, .-f2
.ident "GCC: (GNU) 7.2.0"
.section .note.GNU-stack,"",@progbits
Even when compiled without optimization, the generated assembly was the same, though it was messier so I don't show it.Can someone please call Rob Pike? We are discussing Golang 2.0 already and still don't have 'common' compiler optimizations... ;-) joke
$ ./tmp.test -test.bench .
goos: linux
goarch: amd64
BenchmarkInLoop-4 2000000000 0.46 ns/op
BenchmarkOutLoop-4 2000000000 0.46 ns/op
PASS
(pprof) disasm fast
Total: 1.95s
ROUTINE ======================== _/tmp.fast
970ms 970ms (flat, cum) 49.74% of Total
. . 4ed7c0: MOVQ 0x8(SP), AX ;main_test.go:23
. . 4ed7c5: XORL CX, CX
. . 4ed7c7: MOVQ CX, DX
. . 4ed7ca: JMP 0x4ed7d6 ;main_test.go:26
660ms 660ms 4ed7cc: LEAQ 0x1(CX), BX ;_/tmp.fast main_test.go:26
110ms 110ms 4ed7d0: MOVQ CX, DX
170ms 170ms 4ed7d3: MOVQ BX, CX
30ms 30ms 4ed7d6: CMPQ AX, CX
. . 4ed7d9: JL 0x4ed7cc ;main_test.go:26
. . 4ed7db: MOVQ DX, 0x10(SP) ;main_test.go:30
(pprof) disasm slow
Total: 1.95s
ROUTINE ======================== _/tmp.slow
960ms 960ms (flat, cum) 49.23% of Total
. . 4ed790: MOVQ 0x8(SP), AX ;main_test.go:14
. . 4ed795: XORL CX, CX
. . 4ed797: MOVQ CX, DX
. . 4ed79a: JMP 0x4ed7a6 ;main_test.go:16
690ms 690ms 4ed79c: LEAQ 0x1(CX), BX ;_/tmp.slow main_test.go:16
100ms 100ms 4ed7a0: MOVQ CX, DX
100ms 100ms 4ed7a3: MOVQ BX, CX
70ms 70ms 4ed7a6: CMPQ AX, CX
. . 4ed7a9: JL 0x4ed79c ;main_test.go:16
. . 4ed7ab: MOVQ DX, 0x10(SP) ;main_test.go:20 $ go version
go version go1.9.1 linux/amd64 $ ./main.test -test.bench . -test.cpuprofile=test.profile
goos: linux
goarch: amd64
BenchmarkInLoop-8 2000000000 0.42 ns/op
BenchmarkOutLoop-8 2000000000 0.47 ns/op
PASS
(pprof) disasm fast
Total: 1.86s
ROUTINE ======================== command-line-arguments.fast
990ms 990ms (flat, cum) 53.23% of Total
. . 4ed7b0: MOVQ 0x8(SP), AX ;mem_test.go:16
. . 4ed7b5: XORL CX, CX
. . 4ed7b7: MOVQ CX, DX
. . 4ed7ba: JMP 0x4ed7c6 ;mem_test.go:19
650ms 650ms 4ed7bc: LEAQ 0x1(CX), BX ;command-line-arguments.fast mem_test.go:19
. . 4ed7c0: MOVQ CX, DX ;mem_test.go:19
10ms 10ms 4ed7c3: MOVQ BX, CX ;command-line-arguments.fast mem_test.go:19
330ms 330ms 4ed7c6: CMPQ AX, CX
. . 4ed7c9: JL 0x4ed7bc ;mem_test.go:19
. . 4ed7cb: MOVQ DX, 0x10(SP) ;mem_test.go:23
(pprof) disasm slow
Total: 1.86s
ROUTINE ======================== command-line-arguments.slow
870ms 870ms (flat, cum) 46.77% of Total
. . 4ed780: MOVQ 0x8(SP), AX ;mem_test.go:7
. . 4ed785: XORL CX, CX
. . 4ed787: MOVQ CX, DX
. . 4ed78a: JMP 0x4ed796 ;mem_test.go:9
560ms 560ms 4ed78c: LEAQ 0x1(CX), BX ;command-line-arguments.slow mem_test.go:9
. . 4ed790: MOVQ CX, DX ;mem_test.go:9
20ms 20ms 4ed793: MOVQ BX, CX ;command-line-arguments.slow mem_test.go:9
290ms 290ms 4ed796: CMPQ AX, CX
. . 4ed799: JL 0x4ed78c ;mem_test.go:9
. . 4ed79b: MOVQ DX, 0x10(SP) ;mem_test.go:13Abstractions yes, expensive, perhaps not. One of the nice things about Java is that the JIT can collapse a lot of abstraction at runtime. Not all of it, but a lot of it. I am skeptical about the idea that Java naturally guides programmers towards expensive abstractions.
> and makes parallelism difficult
Presumably, you mean something like "makes lightweight concurrency difficult, because all the backend APIs for database access etc are blocking", which is unfortunately true.
The compiler isn't always going to be able to collapse or elide these.
Go is still garbage collected language, so in a sense it combines the most problematic features of C and Java.
That said, it's still pretty heavyweight (both the language and the runtime).
It also wouldn't be my first choice for compute intensive tasks (for example, a non-toy ray tracer). It would do the job eventually, but at a cost. Of course, I wouldn't pick Go or Python either.
Things aren't straightforward, we need to go deep before concluding. Profilers, profilers, profilers.
I appreciate the feedback comments! I'm very much still playing around with different technologies and writing about them as I go, this is more a learning experience for me that I've documented and I would take what I'm saying with a pinch of salt.
I thought I'd also clarify that I'm using the code/docker images that I created in the previous article as the base from which I'm running my tests as that seems to have gotten missed by a few peeps!
Edit: I see the code using the `com.sun.net.httpserver` package, but not the Spring Boot version.
For posterity sake, throw in Ruby on Rails as well.
You’ll also get natural clustering ability and a level of fault tolerance that’s almost silly.
I’ve been trying to find the time to demonstrate this with the tech empower benchmarks honestly. I want to see what will happen if you run all of them at the same time for a particular platform.
Wasn’t trying to misappropriate the credit.
I am curious to what a "normal" Java microservice look like.
It might even take fewer lines.
You gain in other things, like abstraction, but the penalty is enormous. We use very high level languages in house, but when we do it performance is not the priority.
Doing "Hello worlds" is not a valid debunking of anything because , guess what? printing a Hello world in a screen is a very simple operation.
We have millions of lines of code and we have done tests on just converting 20-30Ks of our c,c++ code to java and the result has been disaster: Hundreds or thousands of times slower. That's right.
In my opinion if other companies are deluded wanting to be lazy, much better for us as competitors, but children should not be intoxicated with bad advice.
My advice is do not believe me or anyone else, when in doubt just test on your own.
Hundreds or thousands of times slower Java? Have you considered that the conversion was done with a weak mastery of the language or runtime?
The HFT folks working in Java would be taken aback to learn the language doesn't work.
Bumping it up would have made the comparison less interesting.
Perhaps it was just a small VPS?
It’s hard to be sure what’s happening given the lack of source code, but 1000 requests per second where the request doesn’t do much is a fairly minor load for a well-written java service on modest hardware. I would expect the ‘lightweight’ java example to do better, assuming it’s just jetty or something.