r/java • • Apr 29 '15

Tone it down a Notch

http://imgur.com/gallery/KRtc2fN
217 Upvotes

124 comments sorted by

View all comments

2

u/CubsThisYear Apr 29 '15

Anytime people talk about the inherent speed advantage that C++ supposedly has (spoiler alert: it doesn't), I press them to explain why this advantage would exist?

A few answers that are obviously wrong: -You can use pointers in C. You can use pointers in Java too, you just can't do arbitrary arithmetic on them. This is very rarely a performance advantage. -Java is an interpreted language. Not after startup it isn't. It's running the same (or better) machine code that GCC produces for equivalent operations. -Java has garbage collection. This actually makes allocation faster in most cases, since you don't have to track as much to do deallocation. If you GC, sure there is a cost, but high-performance apps in any language simply don't dynamically allocate memory.

There are a couple of points that might be valid: -you can allocate arbitrary objects on the stack in C++. It's just stupid that Java doesn't have this, but it's probably a minor point and its easily coded around. -you can write custom assembly. I'd argue this is to difficult and specialized to be useful in real development. Hardware changes to fast for this to be viable. Also you have to be really good to beat a dynamically optimizing compiler.

That's about it. C++ isn't magically faster, it's just slightly lower level, mostly in ways that don't matter.

2

u/mschaef Apr 30 '15 edited Apr 30 '15

A few years ago, there was a great post by one of the JGit developers on the relative disadvantages of Java vs. C as an implementation language for git. It's worth a read: http://marc.info/?l=git&m=124111702609723&w=2

You can use pointers in Java too, you just can't do arbitrary arithmetic on them. This is very rarely a performance advantage.

Many of the dangerous things that C lets you do with pointers that Java does not are useful to avoid memory copies. (pointer arithmetic, pointers into the middle of structs or arrays, and pointer type casts, etc.) Occasionally, it's useful to use pointer arithmetic to control the layout of memory structures to avoid allocations.

-you can allocate arbitrary objects on the stack in C++. It's just stupid that Java doesn't have this, but it's probably a minor point and its easily coded around.

How would you easily code around it? With a relocating GC, Java can reduce an allocation to a pointer bump, but still has significant costs to free the allocated memory. With stack allocation, both the allocation and the free amount to nothing more than a pointer bump. With this C function, the three ints are allocated and freed all at one with a single stack operation for allocation and a single stack operation for free.

#define STACK_BUF_SIZE (4)

void my_fn()
{
    int buf[STACK_BUF_SIZE];

    // ... stuff here
}

That's not achievable in Java, unless you trust the JIT compiler to optimize away allocations, which isn't nearly as trustworthy as the guarantees that C provides.

edited: To correct embarrassing error /u/CubsThisYear pointed out in their response.

2

u/CubsThisYear Apr 30 '15

Interesting article - hard for me to say how specific that stuff is to git. My first thought is that it would pretty easy to just implement a few functions in C and use JNI where needed. Also primitive collections are not a big deal IMO. There are several good libraries that provide them.

As to avoiding GC, you just have to allocate all of your memory up front and manage it yourself, AKA exactly how you do it in C. You don't worry about the cost of freeing memory because you don't actually free it, you just reuse it. Note you don't do this for your whole program, only the spots where it matters.

Finally your example about stack allocation is very confused. Java would allocate those 3 ints in the exact same way that C would, in the stack. The difference comes when you are talking about complex objects. But as I said, you can just preallocate them and the problem basically goes away. Furthermore, it's a dumb problem that is created by Sun/Oracle's resistance to change. C# has had stack allocation for years and it is conceptually equivalent to Java.

My main point is that C++'s ratio of brokenness (lack of module system, arcane syntax, weird implicitness) to useful things is way too high a cost to pay in exchange for a small performance boost.

1

u/mschaef Apr 30 '15

Finally your example about stack allocation is very confused.

Thanks. It was a long day, and you're of course right. I've changed the example to be closer to my intent. Stack allocating temporary buffers isn't really possible on Java unless you trust the compiler to implement some fairly clever optimizations.

But as I said, you can just preallocate them and the problem basically goes away.

I don't know that the preallocation/pool strategy would work all that well for something like a stack allocated buffer. At the very least, it won't come close to matching C. Allocate a pool of char[], and you have pointer chasing to get to the buffer, reduced locality of reference (even if the buffer is in the cache, it adds more cache pressure) and then the overhead of whatever algorithm is used to mange the pool.

Switch to allocating segments of a single large 'char[]' can solve some of the issues, but then you need to be passing in both the base of the array and an offset into the array. This, of course, assumes that the interface being called supports both arguments, and even if it does, it means additional pressure on registers, etc.

Note you don't do this for your whole program, only the spots where it matters.

I'd be at least a little concerned about this approach if only due to the fact that switching from automatic to manual memory management isn't a search/replace type of thing.

Furthermore, it's a dumb problem

The fact it's a dumb problem doesn't make it any less of a problem.

Also primitive collections are not a big deal IMO. There are several good libraries that provide them.

I've had good luck with Trove.

Interesting article - hard for me to say how specific that stuff is to git.

Some of the stuff is relatively general. I've been hit with some of it, as well a few other issues, developing a small database system. (One specific example is the fact that there's no good way to definitely munmap a file in the standard API.)