Jonathan Dunlap
Jonathan Dunlap

Reputation: 2631

Optimal Struct size for modern systems

I've read that the ideal size of a structure for performance, that's going to be used in a large collection, is 32 bytes. Is this true and why? Does this effect 64bit processors or is it not applicable?

This is in context of modern (2008+) home Intel-based systems.

Upvotes: 2

Views: 914

Answers (4)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

The optimal size for a struct is usually the minimum size needed to store whatever data it's supposed to contain without requiring any hacks like bit twiddling/misaligned accesses to make it fit.

Upvotes: 6

Keith Thompson
Keith Thompson

Reputation: 263307

The ideal size of a struct is enough to hold the information it needs to contain.

Upvotes: 10

markgz
markgz

Reputation: 6266

The ideal size of a structure is likely to be one cache line (or a sub-multiple thereof). Level one cache lines are typically 32 or 64 bytes. Splitting an element of a data structure across a cache line boundary will require two main memory accesses to read or write it instead of one.

Upvotes: 3

Perry
Perry

Reputation: 4495

I don't think there is a reasonable answer to your question. Without any information on the context of the application, the "ideal size of a structure" is way, way underspecified.

As an aside, 32 bits is the space of one modern integer -- it isn't large enough for a "struct" except of a couple of characters or bitfields.

Upvotes: 0

Related Questions