Hacker News new | past | comments | ask | show | jobs | submit login

I will take your word about java, having never actually worked with it myself!

My understanding with array performance is that if you need to frequently read and write to a large, fixed-length enumerable (or if you can rework your problem as such), then an array of structs is often much faster. It's certainly on my mental list of things to try when dealing with large sets of data in hot loops.




There is no "array of structs", at least not until project valhalla finally lands and I don't expect that to happen any time before its ten years anniversary (in 2024).

The intuitive expectation about performance difference between array and a generic collection is that it's about method call overhead, because in practice, unless "no duplicate entries" set semantics are required what you call "enumerable" will be an object wrapping an underlying array almost ten out of ten times. But that's something JIT optimization excels at dealing with. The real advantage of arrays is memory proximity and in absence of project valhalla future VM features, that advantage only exists for arrays of primitives like int, float, double. Everything else will be a reference to some arbitrary heap address anyways, even if it's reference in a bare array (instead of references in an array wrapped by an object).

What you can do is rework your data into a form where multiples of something have the primitives of their logical fields in shared arrays, but that's not array of structs but struct of arrays. And I've never seen code like that in the wild, not outside a hobby project that I never even got to the point where I could verify the approach by profiling. I do believe that the pattern must exist, I some codebases, but I can't even think of a name people would recognize.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: