4 ms·
It'd go against OOP, but you could take all the member variables and put in separate arrays. E.g instead of an array of 3-vectors, you'd have an x-array, a y-a
by im3w1l 12y ago
It'd go against OOP, but you could take all the member variables and put in separate arrays.
E.g instead of an array of 3-vectors, you'd have an x-array, a y-array and a z-array.
- pkolaczk 12y agoStill if you need all three together, that's potential 3-cache misses instead of 1.
- vardump 12y agoAnd if there are too many fields, this technique can confuse CPU hardware prefetcher not to see the sequential access pattern making prefetching less useful (or even completely useless). This will increase memory latency and make CPU stall longer while waiting for memory. If you're unlucky, physical memory read pointer distance in bytes is modulo of 64 * <number of memory channels>, all 3 reads might hit the same physical memory module, but in a different DRAM page. All accesses would buffer on just one memory controller and one memory module, significantly reducing available bandwidth. At physical memory level read pointer distance between different arrays could also cause cache aliasing and consequently extra cache invalidations. See http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf http://www.intel.com/content/dam/www/public/us/en/documents/... for details. Find "aliasing" from it in your favorite PDF viewer. If you are just sequentially and continuously streaming from one array, none of this should normally happen. You can of course still get conflicts leading to CPU stalls when writing data, but that's a different matter. Considering the above, you might want to store your vector x, y, z in a same array, interleaving the values: x = array[objIndex * 3 + 0]; y = array[objIndex * 3 + 1]; z = array[objIndex * 3 + 2]; Not pretty! In real code, you might want to use 4 as multiplier. Then a JVM with vectorization support might have (or might not) better opportunities to load whole vector together with a single SIMD (SSE/AVX) instruction.
- TheLoneWolfling 12y agoAnd now we're back to value structs.
- vardump 12y agoHigh performance cache friendly value struct emulating Java code can be unbelievably ugly. Just one object with elementary type array members.
- TheLoneWolfling 12y agoAnd here is where I wish Java actually had a struct type. Alas, like so many other things about the Java language (No operator/assignment overloading, no unsigned integer types, etc, etc), it seems to have been a deliberate, although potentially under-thought, design decision.
- pron 12y agoBut Java and the JVM are getting value types, which are immutable structs.
- TheLoneWolfling 12y agoWhat about for mutable structs?
- pron 12y agoMutable struct are a very bad idea.
- TheLoneWolfling 12y agoElaborate? What makes it a bad idea? Having to rewrite an entire struct to change one value within it seems... counterproductive, considering that the purpose of having structs is largely speed-based. I can see it being useful to be able to flag a struct as immutable for certain cases, but for there not being a mutable version?