82 points · by emforce · 9y ago · 97 comments
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.
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.
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.
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.
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. $ ./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 $ ./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:13 $ go version
go version go1.9.1 linux/amd64Abstractions 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.