When writing a Java program, can I influence how the processor uses its cache to store my data? For example, if I have an array that is accessed a lot, does it help if it is small enough to fit in one cache line (usually 128 bytes on a 64-bit machine)? What if I save a lot of used object within this limit, can I expect that the memory used by it will be close and remain in the cache?
Background: I am creating a compressed digital tree that is heavily inspired by Judy arrays that are in C. While I mainly use node compression methods, Judy has optimized the CPU cache as a central design target and node types, as well as heuristics for switching between them depends heavily on this. I was wondering if I have a chance to get these benefits?
Change The general advice of answers so far is not to try to micro-optimize machine-level details when you are so far from the machine, as in Java. I completely agree, so I felt that I should add some (hopefully) clarifying comments in order to better explain why I think this question still makes sense. They are listed below:
There are a few things that are usually easier to process computers because of how they are created. I saw that Java code is much faster when compressing data (from memory), although decompression had to use additional CPU cycles. If the data was saved on disk, it is obvious why this is so, but, of course, in RAM the same principle.
Now, computer science has a lot of possibilities to say about what it is, for example, link locality is great for C, and I suppose it's still great in Java, perhaps even more so if it helps optimize runtime, to do more smart things. But how you do it may be quite different. In C, I can write code that manages large chunks of memory and uses contiguous pointers for related data.
In Java, I can't (and don't want to) know much about how memory will be managed by a particular runtime. Therefore, I have to take optimization to a higher level of abstraction. My question is mainly, how do I do this? As for link locality, what does it mean "close together" at the level of abstraction I'm working on in Java? The same object? The same type? The same array?
In general, I do not think that layers of abstraction change the "laws of physics", metaphorically. Doubling your array in size every time you finish space is a good strategy in Java, even if you no longer call malloc() .