Hot/cold splitting of structures

Posted: May 26, 2007 in C++ tips, Code Drinks

Another tip for optimization if you are working with C++. Hot/Cold splitting of structures is about splitting heavily accessed ( hot ) portions of data structure from rarely accessed cold portions. An example of the concept is as follows :-

Assume you have a class C, which is 400 bytes. 300 of those 400 bytes are needed only to contruct an exception object if C::foo() throws. Is it smart to keep these 300 bytes directly as members?

An alternative would be to put those 300 bytes in an C::C_exceptiondata class, and put a C_exceptiondata * in C. The total memory usage would increase ( by sizeof(C_exceptiondata*) ), but the C_exceptiondata objects may very well end up all swapped out, and certainly would not disrupt the CPU cache. The cache would only load the remaining 100 bytes.

The 300 bytes that are rarely used are called “cold”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s