Aussie AI
Data Member Optimizations
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Data Member Optimizations
These optimizations apply to C++ objects or structures. There are various ways to speed up the data accesses and writes to a data member in an object.
Avoid bit-fields. Bit-fields are a special C++ feature designed to reduce space in an object or structure.
struct node { unsigned int visited :1; // bit-field };
Avoid bit-fields if you want runtime speedup.
They are great at reducing memory size,
but often at the cost of extra run-time
overhead on any accesses to these fields.
Hence, for improved efficiency, at the cost of space
wastage, remove the “:1
” qualification
and change to a small data
type such as bool
, char
, or unsigned char
.
Memory alignment:
If there are mixed size data members, or there are some with “alignas
” alignment settings,
then memory alignment issues can needlessly create an oversize object.
This is more of a problem in terms of unnecessary space usage,
but adds inefficiencies in the need to initialize or copy the extra padding bytes for large arrays
of objects.
The general rules for minimizing size are to: (a) order members from large to small, and (b) group like size data types together.
Most used data member first. The machine code for an access to a structure or object's data fields usually involve a base address of the object, to which is added an offset that is specific to each field. References to the first field of a structure can often be more efficient because there is no need to add an offset (i.e., the offset is zero). Hence, the most used class data member or structure field should be placed first in the declarations.
Order data members by usage. It's not just the first data member whose order matters. Memory access issues such as data locality, predictive caching and memory access pipelining mean that all of the most-used data members should be close together in an object. In very large objects, there are some platforms where smaller offsets are more quickly calculated, such as data members with less than 128 or 256 as their offset. Hence, a simple optimization is to order the data member declarations according to their usage.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |