Skip to content

Micro-OP: reduce UserItem class 72 to 64 bytes using align for 64-bit platforms #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GermanAizek
Copy link

@GermanAizek GermanAizek commented May 20, 2025

@robert-ancell,

/* saved 8 bytes and 1 cacheline! */

The reduction allowed us to fit the cache line into one cache, this is an advantage, it is desirable to have structures and classes of exactly 64 bytes or less in size, now fewer CPU cycles are spent working with this structure.

This PR will decrease costs copying, moving, and creating object-structures only for common 64bit processors due to the 8-byte data alignment.

Smaller size structure or class, higher chance putting into CPU cache. Most processors are already 64 bit, so the change won't make it any worse.

Pahole example:

  • Comment /* XXX {n} bytes hole, try to pack */ shows where optimization is possible by rearranging the order of fields structures and classes

About Linux Kernel tool Pahole: https://linux.die.net/man/1/pahole

Master branch

class UserItem {
public:

        class QString             name;                  /*     0     8 */
        class QString             realName;              /*     8     8 */
        class QString             homeDirectory;         /*    16     8 */
        class QString             image;                 /*    24     8 */
        class QString             background;            /*    32     8 */
        class QString             session;               /*    40     8 */
        bool                       isLoggedIn;           /*    48     1 */
        bool                       hasMessages;          /*    49     1 */

        /* XXX 6 bytes hole, try to pack */

        quint64                    uid;                  /*    56     8 */
        class QString displayName(const class UserItem  *);

        /* --- cacheline 1 boundary (64 bytes) --- */
        bool                       isLocked;             /*    64     1 */
        void UserItem(class UserItem *, const class UserItem  &);

        void ~UserItem(class UserItem *, int);

        void UserItem(class UserItem *);


        /* size: 72, cachelines: 2, members: 10 */
        /* sum members: 59, holes: 1, sum holes: 6 */
        /* padding: 7 */
        /* last cacheline: 8 bytes */
};

This PR

class UserItem {
public:

        class QString             name;                  /*     0     8 */
        class QString             realName;              /*     8     8 */
        class QString             homeDirectory;         /*    16     8 */
        class QString             image;                 /*    24     8 */
        class QString             background;            /*    32     8 */
        class QString             session;               /*    40     8 */
        bool                       isLoggedIn;           /*    48     1 */
        bool                       hasMessages;          /*    49     1 */
        bool                       isLocked;             /*    50     1 */

        /* XXX 5 bytes hole, try to pack */

        quint64                    uid;                  /*    56     8 */

        /* size: 64, cachelines: 1, members: 10 */
        /* sum members: 59, holes: 1, sum holes: 5 */
};   /* saved 8 bytes and 1 cacheline! */

Info about technique:

https://hpc.rz.rptu.de/Tutorials/AVX/alignment.shtml

https://wr.informatik.uni-hamburg.de/_media/teaching/wintersemester_2013_2014/epc-14-haase-svenhendrik-alignmentinc-presentation.pdf

https://en.wikipedia.org/wiki/Data_structure_alignment

https://stackoverflow.com/a/20882083

https://zijishi.xyz/post/optimization-technique/learning-to-use-data-alignment/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant