Click here to Skip to main content
12,349,379 members (65,212 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

26.6K views
8 bookmarked
Posted

Optimizing object size by clustering

, 5 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Optimizing object size by clustering
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)

Share

About the Author

Lakamraju Raghuram
Software Developer
India India
_____________________________________________________________

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Abinash Bishoyi18-Apr-12 2:19
memberAbinash Bishoyi18-Apr-12 2:19 
QuestionPVS-Studio Pin
Karpov Andrey20-Mar-12 2:46
memberKarpov Andrey20-Mar-12 2:46 
GeneralRe: Nice explanation. I really liked it. Please find more argume... Pin
Lakamraju Raghuram12-Feb-12 17:00
memberLakamraju Raghuram12-Feb-12 17:00 
GeneralRe: The reason is the memory bus: modern computers usually have ... Pin
Stefan_Lang7-Feb-12 22:33
memberStefan_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
memberhakz.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
memberShmuelAKam13-Feb-12 20:48 
GeneralGood article and better if tell me why it need packing of 8 Pin
igurong6-Feb-12 19:26
memberigurong6-Feb-12 19:26 
GeneralReason for my vote of 3 If we set the Struct Member Alignmen... Pin
hakz.code6-Feb-12 17:11
memberhakz.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
memberStefan_Lang7-Feb-12 2:26 
GeneralReason for my vote of 5 Good observation Pin
Pranit Kothari6-Feb-12 15:18
memberPranit Kothari6-Feb-12 15:18 
GeneralNice point. I understand what you are pointing at. I will tr... Pin
Lakamraju Raghuram6-Feb-12 0:19
memberLakamraju Raghuram6-Feb-12 0:19 
GeneralWell observed. But in this rudimentary form this is so trivi... Pin
nv35-Feb-12 21:49
membernv35-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
memberStefan_Lang7-Feb-12 2:30 
GeneralReason for my vote of 4 helpful Pin
Mokhtar Ashour4-Feb-12 10:29
memberMokhtar 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.

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