简体   繁体   中英

Cache in C accessed via JNI

在C / C ++中构建LRU类型缓存并让Java通过JNI访问它是否有利于提高性能?

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.

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 (ie 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM