views:

1374

answers:

11

I was reading a blog post by a game coder for Introversion and he is busily trying to squeeze every CPU tick he can out of the code. One trick he mentions off-hand is to

"re-order the member variables of a class into most used and least used."

I'm not familiar with C++, nor with how it compiles, but I was wondering if

  1. This statement is accurate?
  2. How/Why?
  3. Does it apply to other (compiled/scripting) languages?

I'm aware that the amount of (CPU) time saved by this trick would be minimal, it's not a deal-breaker. But on the other hand, in most functions it would be fairly easy to identify which variables are going to be the most commonly used, and just start coding this way by default.

+2  A: 

Well the first member doesn't need an offset added to the pointer to access it.

Lou Franco
Nice point. (Comments must have at least 15 characters...)
sharptooth
The offset is fixed, so I think the machine code instruction will contain that add anyway, and there will be one cpu cycle nevertheless.
Marcus Lindblom
Nice point.
paxdiablo
: -)
paxdiablo
@Pax: Why? First, if there's no vtable, the offset for the first member will be zero and will not be stored in the code and this will give smaller code. Then smaller code improves instruction cache usage.
sharptooth
Load address + small fixed offset instructions are so common that they are very efficiently represented already.
Marcus Lindblom
On x86 you have to store a 32-bit offset anyway if it's not zero. Doesn't matter if it fits into one byte.
sharptooth
@sharptooth, sorry, that's my weird (non-existent, my wife would say) sense of humor. I reposted your own comment in less than 15 chars and then a smiley (also short) to show it could be done. The trick is to put lots of spaces between "nice" and "point" - the SO renderer turns them into one space so you can enter short comments like ...
paxdiablo
... this.
paxdiablo
A: 

In theory, it could reduce cache misses if you have big objects. But it's usually better to group members of the same size together so you have tighter memory packing.

kotlinski
A: 

hmmm, this sounds like a highly dubious practice, why wouldn't the compiler take care of this?

Ash Kim
Only until a customer with a lot of cash comes in and asks you to make the code fast enough to execute with at least given speed on some relatively slow embedded system.
sharptooth
Because if the compiler would change variables around it would break polymorphism. The OP's quote is correct, reordering does help with performance.
Blindy
The compiler is not allowed to reorder member variables of a class unless they have access specifiers in between them (public: etc). And even then, it has no way of knowing how frequently a given field will be accessed. So it cannot perform the proposed optimisation, assuming that it really is an optimisation.
Steve Jessop
Ah - I see now!! Though, the taste of bile still lingers ;) I've been playing in the lofty clouds of interpreted languages for too long.
Ash Kim
this would only be feasible if the compiler is using profiler-based information so that it can actually know about internal dataflow.
none
Actually, I've just realised half of what I said was half wrong. The compiler isn't allowed to reorder members of a POD class unless they have access specifiers between them. Non-POD classes it could, but it still can't normally tell what's commonly-used. gcc can optimise based on profiler output, but I have no idea whether it looks at locality of data or just locality of code.
Steve Jessop
+8  A: 

Depending on the type of program you're running this advice may result in increased performance or it may slow things down drastically.

Doing this in a multi-threaded program means you're going to increase the chances of 'false-sharing'.

Check out Herb Sutters articles on the subject here

I've said it before and I'll keep saying it. The only real way to get a real performance increase is to measure your code, and use tools to identify the real bottle neck instead of arbitrarily changing stuff in your code base.

Glen
Couldn't agree more. Great article by Sutter on false sharing. Also profiling should absolutely be the first step to optimization.
luke
+1 This is a good point... however I don't see any mention in the question about the code being multi-threaded.
ceretullis
+6  A: 

It is one of the ways of optimizing the working set size. There is a good article by John Robbins on how you can speed up the application performance by optimizing the working set size. Of course it involves careful selection of most frequent use cases the end user is likely to perform with the application.

Canopus
That article is great, but it appears to only apply to C++. Do you know if the concepts cross-apply to C#?
I dont know abc of C#, but if there is a concept of dll it should help. Any comments from C# gurus?
Canopus
A: 

I highly doubt that would have any bearing in CPU improvements - maybe readability. You can optimize the executable code if the commonly executed basic blocks that are executed within a given frame are in the same set of pages. This is the same idea but would not know how create basic blocks within the code. My guess is the compiler puts the functions in the order it sees them with no optimization here so you could try and place common functionality together.

Try and run a profiler/optimizer. First you compile with some profiling option then run your program. Once the profiled exe is complete it will dump some profiled information. Take this dump and run it through the optimizer as input.

I have been away from this line of work for years but not much has changed how they work.

AndrewB
A: 

I'm focusing on performance, execution speed, not memory usage. The compiler, without any optimizing switch, will map the variable storage area using the same order of declarations in code. Imagine

 unsigned char a;
 unsigned char b;
 long c;

Big mess-up? without align switches, low-memory ops. et al, we're going to have an unsigned char using a 64bits word on your DDR3 dimm, and another 64bits word for the other, and yet the unavoidable one for the long.

So, that's a fetch per each variable.

However, packing it, or re-ordering it, will cause one fetch and one AND masking to be able to use the unsigned chars.

So speed-wise, on a current 64bits word-memory machine, aligns, reorderings, etc, are no-nos. I do microcontroller stuff, and there the differences in packed/non-packed are reallllly noticeable (talking about <10MIPS processors, 8bit word-memories)

On the side, it's long known that the engineering effort required to tweak code for performance other than what a good algorithm instructs you to do, and what the compiler is able to optimize, often results in burning rubber with no real effects. That and a write-only piece of syntaxically dubius code.

The last step-forward in optimization I saw (in uPs, don't think it's doable for PC apps) is to compile your program as a single module, have the compiler optimize it (much more general view of speed/pointer resolution/memory packing, etc), and have the linker trash non-called library functions, methods, etc.

jpinto3912
A: 

In C#, the order of the member is determined by the compiler unless you put the attribute [LayoutKind.Sequential/Explicit] which forces the compiler to lay out the structure/class the way you tell it to.

As far as I can tell, the compiler seems to minimize packing while aligning the data types on their natural order (i.e. 4 bytes int start on 4 byte addresses).

Remi Lemarchand
No one asked about C#. C++ compilers typically do NOT reorder the member variables because they don't try to do your thinking for you.
ceretullis
As a general discussion about memory layout impact on performance the comment does add value. CLR is a very commonly used environment.
Remus Rusanu
@ceretullis I asked in the question "How does it apply to other languages" and I am a C# programmer. So I am very interested in this answer.
+27  A: 

Two issues here:

  • Whether and when keeping certain fields together is an optimization.
  • How to do actually do it.

The reason that it might help, is that memory is loaded into the CPU cache in chunks called "cache lines". This takes time, and generally speaking the more cache lines loaded for your object, the longer it takes. Also, the more other stuff gets thrown out of the cache to make room, which slows down other code in an unpredictable way.

The size of a cache line depends on the processor. If it is large compared with the size of your objects, then very few objects are going to span a cache line boundary, so the whole optimization is pretty irrelevant. Otherwise, you might get away with sometimes only having part of your object in cache, and the rest in main memory (or L2 cache, perhaps). It's a good thing if your most common operations (the ones which access the commonly-used fields) use as little cache as possible for the object, so grouping those fields together gives you a better chance of this happening.

The general principle is called "locality of reference". The closer together the different memory addresses are that your program accesses, the better your chances of getting good cache behaviour. It's often difficult to predict performance in advance: different processor models of the same architecture can behave differently, multi-threading means you often don't know what's going to be in the cache, etc. But it's possible to talk about what's likely to happen, most of the time. If you want to know anything, you generally have to measure it.

Please note that there are some gotchas here. If you are using CPU-based atomic operations (which the atomic types in C++0x generally will), then you may find that the CPU locks the entire cache line in order to lock the field. Then, if you have several atomic fields close together, with different threads running on different cores and operating on different fields at the same time, you will find that all those atomic operations are serialised because they all lock the same memory location even though they're operating on different fields. Had they been operating on different cache lines then they would have worked in parallel, and run faster. In fact, as Glen (via Herb Sutter) points out in his answer, on a coherent-cache architecture this happens even without atomic operations, and can utterly ruin your day. So locality of reference is not necessarily a good thing where multiple cores are involved, even if they share cache. You can expect it to be, on grounds that cache misses usually are a source of lost speed, but be horribly wrong in your particular case.

Now, quite aside from distinguishing between commonly-used and less-used fields, the smaller an object is, the less memory (and hence less cache) it occupies. This is pretty much good news all around, at least where you don't have heavy contention. The size of an object depends on the fields in it, and on any padding which has to be inserted between fields in order to ensure they are correctly aligned for the architecture. C++ (sometimes) puts constraints on the order which fields must appear in an object, based on the order they are declared. This is to make low-level programming easier. So, if your object contains:

  • an int (4 bytes, 4-aligned)
  • followed by a char (1 byte, any alignment)
  • followed by an int (4 bytes, 4-aligned)
  • followed by a char (1 byte, any alignment)

then chances are this will occupy 16 bytes in memory. The size and alignment of int isn't the same on every platform, by the way, but 4 is very common and this is just an example.

In this case, the compiler will insert 3 bytes of padding before the second int, to correctly align it, and 3 bytes of padding at the end. An object's size has to be a multiple of its alignment, so that objects of the same type can be placed adjacent in memory. That's all an array is in C/C++, adjacent objects in memory. Had the struct been int, int, char, char, then the same object could have been 12 bytes, because char has no alignment requirement.

I said that whether int is 4-aligned is platform-dependent: on ARM it absolutely has to be, since unaligned access throws a hardware exception. On x86 you can access ints unaligned, but it's generally slower and IIRC non-atomic. So compilers usually (always?) 4-align ints on x86.

The rule of thumb when writing code, if you care about packing, is to look at the alignment requirement of each member of the struct. Then order the fields with the biggest-aligned types first, then the next smallest, and so on down to members with no aligment requirement. For example if I'm trying to write portable code I might come up with this:

struct some_stuff {
    double d;   // I expect double is 64bit IEEE, it might not be
    uint64_t l; // 8 bytes, could be 8-aligned or 4-aligned, I don't know
    uint32_t i; // 4 bytes, usually 4-aligned
    int32_t j;  // same
    short s;    // usually 2 bytes, could be 2-aligned or unaligned, I don't know
    char c[4];  // array 4 chars, 4 bytes big but "never" needs 4-alignment
    char d;     // 1 byte, any alignment
};

If you don't know the alignment of a field, or you're writing portable code but want to do the best you can without major trickery, then you assume that the alignment requirement is the largest requirement of any fundamental type in the structure, and that the alignment requirement of fundamental types is their size. So, if your struct contains a uint64_t, or a long long, then the best guess is it's 8-aligned. Sometimes you'll be wrong, but you'll be right a lot of the time.

Note that games programmers like your blogger often know everything about their processor and hardware, and thus they don't have to guess. They know the cache line size, they know the size and alignment of every type, and they know the struct layout rules used by their compiler (for POD and non-POD types). If they support multiple platforms, then they can special-case for each one if necessary. They also spend a lot of time thinking about which objects in their game will benefit from performance improvements, and using profilers to find out where the real bottlenecks are. But even so, it's not such a bad idea to have a few rules of thumb that you apply whether the object needs it or not. As long as it won't make the code unclear, "put commonly-used fields at the start of the object" and "sort by alignment requirement" are two good rules.

Steve Jessop
Don't forget about `#pramga pack` and its impact on member alignment
Remus Rusanu
Good point. Suffice to say that some/many compilers allow you to lay out your struct in a non-default way, if you know what you want in a particular situation and the default isn't it. Packing pragmas are vital in situations where your struct represents some sequence of bytes of I/O, as for example when you're reading or writing packets to a network. You can't afford unexpected, platform-specific padding.
Steve Jessop
"Far too long" you claim. I think it's an incredible response. If I could (+10) it I would.
If only all ARM CPUs raised a hardware exception on unaligned accesses, the world would be a better place. Many ARM7TDMI designs do not do this, they just rotate/smear the data, which is not a very fun thing to debug.
bk1e
@bk1e: yuck. One place I worked, the debug-mode x86 compiler inserted alignment checks before every memory access, so that kind of thing could often be caught before hitting the device. It helped.
Steve Jessop
+3  A: 

We have slightly different guidelines for members here (ARM architecture target, mostly THUMB 16-bit codegen for various reasons):

  • group by alignment requirements (or, for newbies, "group by size" usually does the trick)
  • smallest first

"group by alignment" is somewhat obvious, and outside the scope of this question; it avoids padding, uses less memory, etc.

The second bullet, though, derives from the small 5-bit "immediate" field size on the THUMB LDRB (Load Register Byte), LDRH (Load Register Halfword), and LDR (Load Register) instructions.

5 bits means offsets of 0-31 can be encoded. Effectively, assuming "this" is handy in a register (which it usually is):

  • 8-bit bytes can be loaded in one instruction if they exist at this+0 through this+31
  • 16-bit halfwords if they exist at this+0 through this+62;
  • 32-bit machine words if they exist at this+0 through this+124.

If they're outside this range, multiple instructions have to be generated: either a sequence of ADDs with immediates to accumulate the appropriate address in a register, or worse yet, a load from the literal pool at the end of the function.

If we do hit the literal pool, it hurts: the literal pool goes through the d-cache, not the i-cache; this means at least a cacheline worth of loads from main memory for the first literal pool access, and then a host of potential eviction and invalidation issues between the d-cache and i-cache if the literal pool doesn't start on its own cache line (i.e. if the actual code doesn't end at the end of a cache line).

(If I had a few wishes for the compiler we're working with, a way to force literal pools to start on cacheline boundaries would be one of them.)

(Unrelatedly, one of the things we do to avoid literal pool usage is keep all of our "globals" in a single table. This means one literal pool lookup for the "GlobalTable", rather than multiple lookups for each global. If you're really clever you might be able to keep your GlobalTable in some sort of memory that can be accessed without loading a literal pool entry -- was it .sbss?)

leander
+2  A: 

While locality of reference to improve the cache behavior of data accesses is often a relevant consideration, there are a couple other reasons for controlling layout when optimization is required - particularly in embedded systems, even though the CPUs used on many embedded systems do not even have a cache.

- Memory alignment of the fields in structures

Alignment considerations are pretty well understood by many programmers, so I won't go into too much detail here.

On most CPU architectures, fields in a structure must be accessed at a native alignment for efficiency. This means that if you mix various sized fields the compiler has to add padding between the fields to keep the alignment requirements correct. So to optimize the memory used by a structure it's important to keep this in mind and lay out the fields such that the largest fields are followed by smaller fields to keep the required padding to a minimum. If a structure is to be 'packed' to prevent padding, accessing unaligned fields comes at a high runtime cost as the compiler has to access unaligned fields using a series of accesses to smaller parts of the field along with shifts and masks to assemble the field value in a register.

- Offset of frequently used fields in a structure

Another consideration that can be important on many embedded systems is to have frequently accessed fields at the start of a structure.

Some architectures have a limited number of bits available in an instruction to encode an offset to a pointer access, so if you access a field whose offset exceeds that number of bits the compiler will have to use multiple instructions to form a pointer to the field. For example, the ARM's Thumb architecture has 5 bits to encode an offset, so it can access a word-sized field in a single instruction only if the field is within 124 bytes from the start. So if you have a large structure an optimization that an embedded engineer might want to keep in mind is to place frequently used fields at the beginning of a structure's layout.

Michael Burr