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

Optimizing object size by clustering

, 5 Feb 2012
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)

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.

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 5 Feb 2012
Article Copyright 2012 by Lakamraju Raghuram
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid