Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:29:30+00:00 2026-05-28T18:29:30+00:00

In the book Game Coding Complete, 3rd Edition, the author mentions a technique to

  • 0

In the book Game Coding Complete, 3rd Edition, the author mentions a technique to both reduce data structure size and increase access performance. In essence it relies on the fact that you gain performance when member variables are memory aligned. This is an obvious potential optimization that compilers would take advantage of, but by making sure each variable is aligned they end up bloating the size of the data structure.

Or that was his claim at least.

The real performance increase, he states, is by using your brain and ensuring that your structure is properly designed to take take advantage of speed increases while preventing the compiler bloat. He provides the following code snippet:

#pragma pack( push, 1 )

struct SlowStruct
{
    char c;
    __int64 a;
    int b;
    char d;
};

struct FastStruct
{
    __int64 a;
    int b;
    char c;
    char d;  
    char unused[ 2 ]; // fill to 8-byte boundary for array use
};

#pragma pack( pop )

Using the above struct objects in an unspecified test he reports a performance increase of 15.6% (222ms compared to 192ms) and a smaller size for the FastStruct. This all makes sense on paper to me, but it fails to hold up under my testing:

enter image description here

Same time results and size (counting for the char unused[ 2 ])!

Now if the #pragma pack( push, 1 ) is isolated only to FastStruct (or removed completely) we do see a difference:

enter image description here

So, finally, here lies the question: Do modern compilers (VS2010 specifically) already optimize for the bit alignment, hence the lack of performance increase (but increase the structure size as a side-affect, like Mike Mcshaffry stated)? Or is my test not intensive enough/inconclusive to return any significant results?

For the tests I did a variety of tasks from math operations, column-major multi-dimensional array traversing/checking, matrix operations, etc. on the unaligned __int64 member. None of which produced different results for either structure.

In the end, even if their was no performance increase, this is still a useful tidbit to keep in mind for keeping memory usage to a minimum. But I would love it if there was a performance boost (no matter how minor) that I am just not seeing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:29:31+00:00Added an answer on May 28, 2026 at 6:29 pm

    It is highly dependent on the hardware.

    Let me demonstrate:

    #pragma pack( push, 1 )
    
    struct SlowStruct
    {
        char c;
        __int64 a;
        int b;
        char d;
    };
    
    struct FastStruct
    {
        __int64 a;
        int b;
        char c;
        char d;  
        char unused[ 2 ]; // fill to 8-byte boundary for array use
    };
    
    #pragma pack( pop )
    
    int main (void){
    
        int x = 1000;
        int iterations = 10000000;
    
        SlowStruct *slow = new SlowStruct[x];
        FastStruct *fast = new FastStruct[x];
    
    
    
        //  Warm the cache.
        memset(slow,0,x * sizeof(SlowStruct));
        clock_t time0 = clock();
        for (int c = 0; c < iterations; c++){
            for (int i = 0; i < x; i++){
                slow[i].a += c;
            }
        }
        clock_t time1 = clock();
        cout << "slow = " << (double)(time1 - time0) / CLOCKS_PER_SEC << endl;
        
        //  Warm the cache.
        memset(fast,0,x * sizeof(FastStruct));
        time1 = clock();
        for (int c = 0; c < iterations; c++){
            for (int i = 0; i < x; i++){
                fast[i].a += c;
            }
        }
        clock_t time2 = clock();
        cout << "fast = " << (double)(time2 - time1) / CLOCKS_PER_SEC << endl;
    
    
    
        //  Print to avoid Dead Code Elimination
        __int64 sum = 0;
        for (int c = 0; c < x; c++){
            sum += slow[c].a;
            sum += fast[c].a;
        }
        cout << "sum = " << sum << endl;
    
    
        return 0;
    }
    

    Core i7 920 @ 3.5 GHz

    slow = 4.578
    fast = 4.434
    sum = 99999990000000000
    

    Okay, not much difference. But it’s still consistent over multiple runs.
    So the alignment makes a small difference on Nehalem Core i7.


    Intel Xeon X5482 Harpertown @ 3.2 GHz (Core 2 – generation Xeon)

    slow = 22.803
    fast = 3.669
    sum = 99999990000000000
    

    Now take a look…

    6.2x faster!!!


    Conclusion:

    You see the results. You decide whether or not it’s worth your time to do these optimizations.


    EDIT :

    Same benchmarks but without the #pragma pack:

    Core i7 920 @ 3.5 GHz

    slow = 4.49
    fast = 4.442
    sum = 99999990000000000
    

    Intel Xeon X5482 Harpertown @ 3.2 GHz

    slow = 3.684
    fast = 3.717
    sum = 99999990000000000
    
    • The Core i7 numbers didn’t change. Apparently it can handle
      misalignment without trouble for this benchmark.
    • The Core 2 Xeon now shows the same times for both versions. This confirms that misalignment is a problem on the Core 2 architecture.

    Taken from my comment:

    If you leave out the #pragma pack, the compiler will keep everything aligned so you don’t see this issue. So this is actually an example of what could happen if you misuse #pragma pack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know of a good book on Game Interface Design (not game play mechanics;
I'm following the XNA 3.0 Game Studio Unleashed Book by Chad Carter. I'm on
Each book can have many authors. And each author can author many books. class
I was programming the example code from Frank Luna's book Introduction to 3D Game
I have read a book and am trying to make a game of tic-tac-toe
I'm trying to write a singleton class for maintain game data, It's called GameManager,
I'm learning DirectX from a book about game programming, and it uses the following
I'm following Ron Penton's Beggining C# Game Programming book and in Part 2 it
I have the following script that is taken from a python game development book.
I am working on a Cocos2d iPad game that includes a magical book. From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.