r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Feb 22 '15

[deleted]

22

u/[deleted] Feb 22 '15

It's definitely no C, but it's a hell of a lot closer than most of the languages that go into prod these days (excluding the MLs, which despite reddit's hope, get very little industry love).

13

u/slavik262 Feb 22 '15

Yep. Cache misses are a huge and "hidden" cost on modern CPU architectures. Everything being a heap-allocated object in Java means that unless you're iterating over an array of primitives, you're going to be doing a lot of bouncing around in memory. See: http://gameprogrammingpatterns.com/data-locality.html

1

u/nqd26 Feb 22 '15

Everything being a heap-allocated object in Java

But this is not really true anymore. Java does escape analysis (IIRC from Java 6 with some option, from Java 7 by default) and puts short-lived (i.e. those which don't escape method scope) objects on stack.

2

u/[deleted] Feb 22 '15

[removed] — view removed comment

1

u/nqd26 Feb 22 '15

Are you talking about "growability" of collections? If so, it can be solved by preallocating the necessary number of elements (if known), e.g. new ArrayList(1000) or even new ArrayList(otherColl.size())

2

u/[deleted] Feb 22 '15

[removed] — view removed comment

0

u/nqd26 Feb 22 '15

It can't restructure an array so that it can hold the elements directly instead of just pointers to the objects.

I'm not sure why you'd want it to.

public void something() {
	List list = new ArrayList();
	list.add(new Member());
	
	// do something with the objects
}

In this example both collection and member can be put on the stack.

public Member something() {
	Member member = new Member();
	
	List list = new ArrayList();
	list.add(member);
	
	// do something with the objects
	
	return member;
}

In this example member obviously escapes so needs to be on the heap. But list can be on the stack. Everything seems to be fine.

Inheritance is the problem here, as you don't know ahead of time how large each object is going to be.

Can you elaborate on this? When JVM start creating an object it already has all the classes from which the instantiated class is inheriting, so JVM also knows the size of the object (it must know so that it can allocate proper amount of memory).

1

u/[deleted] Feb 22 '15

[removed] — view removed comment

1

u/nqd26 Feb 22 '15

That's a separate problem though. EA and stack vs. heap is about cost of allocation and deallocation (GC) of memory, not about extra cost of chasing pointers and CPU cache ...

5

u/bcash Feb 22 '15

except for a few unlikely scenarios where the JIT works perfectly

This isn't even a strawman. JIT will never "work perfectly", and it doesn't have to. This is very much a case of 50% of the effort delivering 90% of the benefit; most cases are "good enough" rather than all-but one being bad.

It's also wrong to call what Java does JIT. Compilation occurs during run-time, the same piece of code can and will be recompiled multiple times as it's running.

All this means that Java's performance is non-deterministic, which is a showstopper for a lot of applications, but not all. Being non-deterministic doesn't mean "bad", the average, and even 90th percentile I'd say, case is still very good.

Good enough that the difference between C and Java is within an order of magnitude in "real-world" cases.

Java's memory usage, however, will be much higher.

5

u/[deleted] Feb 22 '15

All this means that Java's performance is non-deterministic, which is a showstopper for a lot of applications, but not all. Being non-deterministic doesn't mean "bad", the average, and even 90th percentile I'd say, case is still very good.

Well, you can always -XComp, but that curiously enough tends to degrade performance.

5

u/The_Doculope Feb 22 '15

Good enough that the difference between C and Java is within an order of magnitude in "real-world" cases.

We can be within an order of magnitude and I'm paying $110k for my server infrastructure while you're paying $1M. That's 6 or 7 devs yearly salary difference there.

Java is probably a lot better than 9x slower than C most of the time, but the point still stands. Of course, if your devs are a lot more productive in Java and the difference is only 3x, maybe Java's the right choice.

4

u/seventeenletters Feb 22 '15

But JIT can't fix your data structures (at least Java's can't), and heap fragmentation does terrible things to performance on modern CPUs.

4

u/bcash Feb 22 '15

No, but it's a similar principal. You can't force data structures to have a specific layout in a memory in Java in much the same way you can't force it to have deterministic performance.

But at the same time the work the JVM does do makes it less-bad than if you naively heap-allocating everything in C++ or other such languages. One of the tiers of compilation the JVM does, once the code has been run a few times, is inlining function calls followed by escape analysis; this means that most short-lived objects will be stack-allocated anyway. Also, by virtue of the garbage collector, there's little fragmentation in the heap; things allocated at the same time will almost certainly be near each other, the heap is compacted (although how, when, etc. depends on which version of Java and which GC; they can take significantly different approaches).

Of course, as an application developer, you cannot control any of these things; and as it runs on the bytecode after-inlining it can be surprising when it does (e.g. just because an object is returned from a method to a method in a different class, it doesn't have to be heap-allocated). But it does mean the average performance is no-where near as bad as the worst-case described whenever this issue comes up.

1

u/seventeenletters Feb 22 '15

Yeah, stack allocation can make a difference. I was thinking of the very specific case where you have arrays that are iterated over in order. At least in practice you can usually just use an array of some primitive type.

1

u/Dragdu Feb 22 '15

So, where are the Java based linear algebra libraries? Yes, this is very much tongue in cheek, but it is important to remember, that there are many kinds of "real world cases".

4

u/Radmonger Feb 22 '15

https://code.google.com/p/efficient-java-matrix-library/

provides links to all the ones it is faster than, and also some sparse matrix libraries that beat it in appropriate cases.

Java is actually a pretty good match for that kind of thing, as unlike quite a few languages it does provide true primitive arrays, and any per-object overhead goes away when everything is 10,000x10,000.

1

u/Dragdu Feb 22 '15

Ah yes, relative speed benchmarks of Java libraries. For when the actual numbers don't paint a good picture.

Where is comparison to MKL and other state of the art libraries?

1

u/frugalmail Feb 23 '15

Java's done more for the analysis communities than any other language has. No other language has as many production horizontally scaling solutions as the JVM does.

Any conventional solution places a far higher priority on horizontal vs vertical scaling.

0

u/[deleted] Feb 22 '15

[deleted]

1

u/joequin Feb 22 '15

Yet c# is still slower in practice.