Click here to Skip to main content
Click here to Skip to main content

Optimizing object size by clustering

By , 5 Feb 2012
 
Consider the structure below:
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.
 
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:
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;
};
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)

About the Author

Lakamraju Raghuram
Software Developer Triad [ CADCAM-e ]
India India
Member
_____________________________________________________________
 
Did my masters from IIT-M in Advanced Manufacturing Technology and I am working mainly on C++ from 2004 onwards.
 
I like to play with my 16 month old daughter Pranavi [rather she plays with me]. By the way she pronounces C++ as "Chi pee-pee".
 
Hate traveling (but thanks to my wife I traveled to Hyd about 50 times in last 3 years).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberAbinash Bishoyi18 Apr '12 - 2:19 
QuestionPVS-StudiomemberKarpov Andrey20 Mar '12 - 2:46 
GeneralRe: Nice explanation. I really liked it. Please find more argume...memberLakamraju Raghuram12 Feb '12 - 17:00 
GeneralRe: The reason is the memory bus: modern computers usually have ...memberStefan_Lang7 Feb '12 - 22:33 
GeneralRe: Yeah,I didn't notice that at end.My mistake :),but why would...memberhakz.code7 Feb '12 - 16:56 
GeneralReason for my vote of 4 Wouldn't it be better to reverse the...memberShmuelAKam13 Feb '12 - 20:48 
GeneralGood article and better if tell me why it need packing of 8memberigurong6 Feb '12 - 19:26 
GeneralReason for my vote of 3 If we set the Struct Member Alignmen...memberhakz.code6 Feb '12 - 17:11 
GeneralRe: Maybe you didn't read to the end, but the author did mention...memberStefan_Lang7 Feb '12 - 2:26 
GeneralReason for my vote of 5 Good observationmemberPranit Kothari6 Feb '12 - 15:18 
GeneralNice point. I understand what you are pointing at. I will tr...memberLakamraju Raghuram6 Feb '12 - 0:19 
GeneralWell observed. But in this rudimentary form this is so trivi...membernv35 Feb '12 - 21:49 
GeneralRe: I agree. A bit more info on memory alignment and the pros an...memberStefan_Lang7 Feb '12 - 2:30 
GeneralReason for my vote of 4 helpfulmemberMokhtar Ashour4 Feb '12 - 10:29 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 5 Feb 2012
Article Copyright 2012 by Lakamraju Raghuram
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid