Reputation: 73
I was wondering which one is better, 2 Arrays or the Array of a structure with 2 data members. I want insights regarding:
Answers both in general context and language specific are welcome.
And please don't suggest having a look at SoA vs AoS questions, already done that.
Upvotes: 1
Views: 1493
Reputation: 2763
Don't forget one important reason to keep the data together: code readability. If you are planning to process each field independently a different thread. You may get performance improvements if you use arrays.
Upvotes: 0
Reputation: 343
It entirely depends on what you're trying to do, neither answer is always going to be "correct".
Outside of compiler-specific padding, structs do not take up any extra memory unless you make it virtual, in which case it'll get a vtable pointer but that's it.
As long as you're targeting a machine with a cache large enough to fit two pages (usually 4KB each, but check for your specific CPU), it doesn't matter and you should choose whichever is easier to work with and makes more sense in your code. The array of structs will use one page and cause a cache miss once for every 4KB of structs you load, while the array of values will load two pages that cause two cache misses half as often. If you do happen to be working with a dinky cache that only allows one cache for your program data, then yes, it'll be much faster to use an array of structs since the alternative would cause cache misses on every read.
Same answer as #1 - arrays will never have their own padding, but a struct might have padding built into it by your compiler.
Struct padding though depends entirely on your compiler, which probably has flags to turn it on or off or set the maximum pad size or whatever. Inspect the raw data of an array of your objects to see if they have padding, and if so, find out how to turn that off in your compiler if you need that memory.
What compiler are you using, and what are you trying to do with your project?
And perhaps more importantly: What stage is your project in, and are you running into speed issues already? Pre-optimization is the root of all evils, and you are likely wasting your time worrying about the issue.
Upvotes: 2