VINAY CHAUHAN
VINAY CHAUHAN

Reputation: 73

2 Arrays vs Array of Structures with 2 data members

I was wondering which one is better, 2 Arrays or the Array of a structure with 2 data members. I want insights regarding:

  1. Is struct a kind of wrapper which takes extra memory? I am aware of padding in structures to fit it into word limits.
  2. Which one is faster if I want to access both the data members together? I think its array of structures.
  3. And does an array of a structure may occupy more memory than 2 arrays due to the possibility of structure padding.

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

Answers (2)

Chris
Chris

Reputation: 2763

  1. Structures are padded to allow optimal access to the members by the CPU so they may take more memory. The fields may be already aligned so no padding needed. So they are not a wrapper in the sense that they are wrapped with data always. Think of structure padding/adjustment of optimizations by the compiler.
  2. Structures will be faster together as the entire structure is likely to fit in cache together. If you have separate lists, they may fall out of the cache.
  3. If padded, yes.

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

Tasgall
Tasgall

Reputation: 343

It entirely depends on what you're trying to do, neither answer is always going to be "correct".

  1. 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.

  2. 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.

  3. 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

Related Questions