Faisal Abid
Faisal Abid

Reputation: 9140

Cache in C accessed via JNI

Does it make sense in terms of better performance to build a LRU type cache in C/C++ and get java to access it via JNI ?

Upvotes: 3

Views: 241

Answers (4)

WhiteFang34
WhiteFang34

Reputation: 72049

You're almost certainly not optimizing the right thing here. The cache implementation is rarely what needs optimization in a large system. There are plenty of good and fast implementations for Java. You're unlikely to get much of anything better out of a native implementation. And even if you could, are you sure you really need it?

It's generally everything you do around the cache that actually matters. Database queries, blocking operations and CPU intensive tasks. You should certainly profile your application and optimize only the areas that need it. If you're optimizing something that's only taking 5% of overall time, then you're only going to get a 5% improvement at the very best (i.e. if you made it infinitely fast).

I've dealt with large systems with large caches and the overhead of the cache implementation is really low, single digit percentages at most. You should make sure you're spending your time on the right thing. I'm not saying you might not need to, I'm just saying it's doubtful and you should verify first. You haven't indicated one way or the other, so I am assuming a bit that you don't have a known problem with an existing cache implementation.

Upvotes: 4

thkala
thkala

Reputation: 86373

Doubtful

JVMs do quite a decent job these days, especially considering that any applications that need a cache would probably stay up long enough for the JVM to optimise it quite a bit. Whatever your cache code would gain in speed by avoiding the JVM, you would probably lose pushing objects back and forth through JNI.

Not to mention the inherent development, maintenance and deployment difficulties of C++ code when compared with Java. Or the dangers of reinventing the wheel by rolling out your own cache - there has to be something out there that you could use instead of rolling out your own.

Upvotes: 6

Vladimir Dyuzhev
Vladimir Dyuzhev

Reputation: 18336

Unlikely.

Cache works with the objects in heap. Heap memory allocation and deallocation in C++ are not better than those of Java. In fact, in a short-running test Java cache would probably even win due to postponed GC (C++ has to release memory explicitly and immediately).

Other operations (put, find) are expected to be on par with C++ due to JIT (those are hot-spots, and will be compiled into native code soon).

Note: as always with performance, the definitive answer can only be obtained via tests.

Upvotes: 1

corsiKa
corsiKa

Reputation: 82589

The JIT compiler is pretty darned fast. It rivals compiled code.

Your best bet is to implement it in whatever is the easiest to develop, and then profile it to identify performance issues. Then, see what you can do from there.

Upvotes: 1

Related Questions