Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Performance of unaligned memory-accesses Date: Mon, 12 Aug 2019 08:49:17 -0700 Organization: None to speak of Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="2f44d05b13bc2f3f5103065553a9c589"; logging-data="30522"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VU233x0Uv1LMmostDvEk7" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:TtEpMNn0j2z9+WnnqmN+VEgnTqY= sha1:tn1BRMygpAKIsUg72EygxcCauT8= Xref: csiph.com comp.lang.c++:67599 Bart writes: > On 12/08/2019 01:07, Keith Thompson wrote: >> Bart writes: >>> Actually, most of the time when pack(1) is used, elements will be >>> properly aligned anyway. Because they will be sized and arranged >>> manually to get the most efficient packing. >>> >>> When done properly, then I should be able to comment out the pack(1), >>> and it ought to still work, and with the same sizes of structs, and the >>> same member offsets. >> >> Then there's no point in using pack(1), is there? > > It isn't always equivalent. I also like to be aware of the layout of my > structs. I just don't like padding and prefer to make use of that space; > pack(1) provides an incentive to do that to avoid misalignment. Do you have a case where pack(1) makes a difference but *doesn't* create the possibility of unaligned members? Normally compilers insert the miminal padding necessary to satsify alignment requirement (though this isn't required). > But I asked >> specifically about a case where it causes a member to be aligned >> differently than it would without the pack(1). > > The example that you snipped showed how that could result in faster > performance, likely due to the significantly reduced memory > requirements, when the hardware complies. > > >>> But pack(1) can also be used to define a particular layout, in instances >>> where I don't need to access individual misaligned members, just to get >>> the right overall size. >> >> If you have >> struct foo { char c; int i; } >> (assume int is 4 bytes), and #pragma pack(1) gives the struct a size of >> 5 bytes, then an array of struct foo will result in misaligned members. > > Doesn't matter, as the i member is misaligned anyway. (Actually, around > every 4th array element, i will be aligned properly.) Sorry, my mistake. I meant that if you have struct foo { int i; char c; } then an array of struct foo will result in misaligned members, even though there likely won't be for a single object. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */