Home > Enterprise >  Is there a speed problem when using pragma pack?
Is there a speed problem when using pragma pack?

Time:02-02

struct window_data
{
    window_props win_props;
    bool VSync;

    Window::event_callback_fn EventCallback;
};

I have this struct in my program. sizeof(window_data) equals 120.

#pragma pack(push, 2)
struct window_data
{
    window_props win_props;
    bool VSync;

    Window::event_callback_fn EventCallback;
};
#pragma pack(pop)

If i use #pragma pack(push, 2), sizeof(window_data) equals 114.

#pragma pack(push, 1)
struct window_data
{
    window_props win_props;
    bool VSync;

    Window::event_callback_fn EventCallback;
};
#pragma pack(pop)

And in this case, sizeof(window_data) equals 113.

So, is there a problem with using the latest case?

CodePudding user response:

In the normal case, without the pragma, the compiler is going to lay out the structure with padding so that the fields are correctly aligned. For example EventCallback is presumably a 64 bit pointer, so its address should be lined up on a 8-byte boundary.

When you use the pragma, fields may wind up not being aligned. Depending on the CPU, this may mean using different load/store instructions to access the field, or using the normal instructions and getting degraded performance on them.

In exchange for this degraded CPU performance accessing the structure, there are a few benefits. Packed structures have a more predictable layout and are sometimes used directly for serializing data, or accessing hardware using memory mapped IO. They also take up less space, which may help your program fit into ram, or help your working set fit into cache. In some cases this tradeoff is worth the CPU penalty.

You should not pack unless you have a specific reason to do so. If you are doing it to achieve a performance benefit, then measure to confirm it's actually better.

  •  Tags:  
  • c
  • Related