#pragma pack specifics

Started by
4 comments, last by CyberSlag5k 19 years, 1 month ago
I'm using #pragma pack to ensure my structures have no extra space at the end of them for optimization purposes. Does specifying the highest possible value for n (pack's only variable) increase optimization? For example, my structures are 50 bytes in size, so pack's default 4 isn't an option (50 isn't evenly divisible by 4). Presently I'm specifying 1 for n, but 2 is entirely doable. I've been thinking it over, and does specifying the pack size specify what size memory blocks the struct should be read in as? And if so, does that mean me using 2 instead of 1 would increase efficiency? I'll probly just try it tomorrow at work to see if it works, but I thought I'd ask here tonight to see if anyone can offer some in depth knowledge on the subject. Thanks as always, guys.
Without order nothing can exist - without chaos nothing can evolve.
Advertisement
You are talking about "optimization purposes" in terms of size optimization, right? Tightly packing a structure shouldn't gain you anything in terms of speed, and in fact may hurt you in that respect, since there is usually a penalty for such unaligned reads on the x86 (or most other processors, but I'll assume x86 here).

You can reasonably get size optimization from packing, if you are working on an x86 and your structure contains lots of members that are not multiples of four in size. To do this, specify the lowest allowable pack size (1), and your data will be completely packed.

Specifying that the structure has a non-multiple-of-four packing will probably only hurt your performance, speed-wise. Packing should not affect how the structure is read in from files. fread() or similar will eventually call down to the disk driver, or something, which will just read the file in chunks of whatever size is most optimal for its purposes, and then spit the whole thing back to you when its done.

In any case, I think the speed differences we're talking about here are negligible, and I'm not sure its worth doing if speed is your only concern. More information would be helpful (f.e.x, if you are using SSE types like __m128, enforcing packing and alignment can be useful because there are SSE instructions that are faster if the data they operate on is aligned properly, and so on).
I'm sorry, I was referring to load time optimization. I'm loading in a 3MB file using Microsoft's ReadFile function. The data is organized into 12 floats and a 2 byte space holder (which I wish wasn't there). I store the data in one large, dynamically allocated array of a structure I've made, using a single call of ReadFile. It takes like 5 or 6 seconds to load everything in, so I'm trying to cut down on the time. Should it not be much lower? I figured maybe since the blocks of data are all 50 bytes long, using pack(2) would be more efficient than pack(1) (and if they're not, what is the point of ever using anything greater than pack(1)).
Without order nothing can exist - without chaos nothing can evolve.
I see. It should be loading much quicker than that (but I also see in your other thread related to this that the problem seems to have corrected itself, so...).

2-byte packing versus 1-byte packing wouldn't have made a different in this case, size-wise, as I said before I doubt the disk driver cares anything at all for the packing of your data (it can't, in fact, know what the packing is -- only how big the structure ultimately is).

Messing with packing is generally done in situations where you need data to be aligned a certain way (although there is a __declspec(align,n) option in Visual Studio and similar functions in other compilers to do this more elegantly)) or possibly interleaved or arranged in a certain way for the purposes of dealing with certain hardware (usually that on embedded systems or device controllers).

I have also seen particularly disgusting hacks where people set their structure packing to, say, 8 and then fill a structure with 32-bit values and then store "secret" values in the pad bytes. I've seen some of these people claim this technique is a good way to obfuscate data in, f.e.x., MMOs to make it harder for players to hack (but this is of course blatantly untrue since the accesses to the real fields of the structure versus the "secret" fields looks the same in the dissassembly). But I'm sort of rambling now.

Good luck with your project.
Thanks :)
Without order nothing can exist - without chaos nothing can evolve.
Can I use #pragma pack(1) on a class to ensure the private member variables are stored contiguously in memory? If so, would I do something like this:

#pragma pack(1)class myClass{public://functionsprivate://variables};#pragma pack()
Without order nothing can exist - without chaos nothing can evolve.

This topic is closed to new replies.

Advertisement