Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Tip/Trick

Optimizing object size by clustering

Rate me:
Please Sign up or sign in to vote.
4.67/5 (16 votes)
5 Feb 2012CPOL 37.5K   8   14
Optimizing object size by clustering
Consider the structure below:
C++
struct DrawingBoard
{
    bool m_bIsEditable;
    float m_fXPos;
    float m_fYPos;
    int m_iOpacity;
    bool m_bResizable;
    short m_sSchema;
    bool m_bCanExport;
    float m_fBorderThickness;
    bool m_bApplyCustomColor;
    int m_iTransparency;
};

Its members are randomly organized (or rather organized basing on their functionality) without taking into account the way they packed into memory.

If we query the size of an object of this structure on a 32-bit OS (with packing of 8), then we will get 36 bytes.

C++
DrawingBoard structDrawingBoard;
size_t n = sizeof( structDrawingBoard );
// n is 36


But if we group them based on their type and arrange them in increasing order of their size, then we COULD reduce the size:
C++
struct DrawingBoard
{
    bool m_bIsEditable;
    bool m_bResizable;
    bool m_bApplyCustomColor;
    bool m_bCanExport;
    short m_sSchema;
    int m_iOpacity;
    int m_iTransparency;
    float m_fXPos;
    float m_fYPos;
    float m_fBorderThickness;
};

C++
DrawingBoard structDrawingBoard;
size_t n = sizeof( structDrawingBoard );
// n is 28


Now the size of structDrawingBoard is only 28 bytes.

[We can still optimize by using bit fields or playing with pragma packing, BUT performace will be AFFECTED.]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
_____________________________________________________________

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.
Working on web technologies using Angular 7.0 and above, HTML5, CSS3 from 2015.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Abinash Bishoyi18-Apr-12 2:19
Abinash Bishoyi18-Apr-12 2:19 
QuestionPVS-Studio Pin
Karpov Andrey20-Mar-12 2:46
Karpov Andrey20-Mar-12 2:46 
GeneralRe: Nice explanation. I really liked it. Please find more argume... Pin
Lakamraju Raghuram12-Feb-12 17:00
Lakamraju Raghuram12-Feb-12 17:00 
GeneralRe: The reason is the memory bus: modern computers usually have ... Pin
Stefan_Lang7-Feb-12 22:33
Stefan_Lang7-Feb-12 22:33 
GeneralRe: Yeah,I didn't notice that at end.My mistake :),but why would... Pin
hakz.code7-Feb-12 16:56
hakz.code7-Feb-12 16:56 
GeneralReason for my vote of 4 Wouldn't it be better to reverse the... Pin
ShmuelAKam13-Feb-12 20:48
ShmuelAKam13-Feb-12 20:48 
GeneralGood article and better if tell me why it need packing of 8 Pin
igurong6-Feb-12 19:26
igurong6-Feb-12 19:26 
GeneralReason for my vote of 3 If we set the Struct Member Alignmen... Pin
hakz.code6-Feb-12 17:11
hakz.code6-Feb-12 17:11 
GeneralRe: Maybe you didn't read to the end, but the author did mention... Pin
Stefan_Lang7-Feb-12 2:26
Stefan_Lang7-Feb-12 2:26 
GeneralReason for my vote of 5 Good observation Pin
Pranit Kothari6-Feb-12 15:18
Pranit Kothari6-Feb-12 15:18 
GeneralNice point. I understand what you are pointing at. I will tr... Pin
Lakamraju Raghuram6-Feb-12 0:19
Lakamraju Raghuram6-Feb-12 0:19 
GeneralWell observed. But in this rudimentary form this is so trivi... Pin
nv35-Feb-12 21:49
nv35-Feb-12 21:49 
GeneralRe: I agree. A bit more info on memory alignment and the pros an... Pin
Stefan_Lang7-Feb-12 2:30
Stefan_Lang7-Feb-12 2:30 
GeneralReason for my vote of 4 helpful Pin
Mokhtar Ashour4-Feb-12 10:29
Mokhtar Ashour4-Feb-12 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.