4 ms·
Instead of repurposing top bits you can also repurpose the Bits beyond alignment. E.g 32 bit integers are aligned to 4 bytes, so you can use the lower two bit
by legulere 7y ago
Instead of repurposing top bits you can also repurpose the Bits beyond alignment. E.g 32 bit integers are aligned to 4 bytes, so you can use the lower two bits of pointers to them instead.
- eschneider 7y agoAs someone who's worked on old Macs and has also done lots of 32 -> 64-bit porting, this is the sort of trick that works wonderfully...until it doesn't. And then you've got a nightmare on your hands. I'm not saying never do that (ok, maybe I am...) But definitely think long and hard about how long your code will be around before you do it.
- astrobe_ 7y agoI'm curious, when does it fail?
- jcelerier 7y ago> As someone who's worked on old Macs and has also done lots of 32 -> 64-bit porting, this is the sort of trick that works wonderfully...until it doesn't. And then you've got a nightmare on your hands. That's why you hide the trick behind a zero-cost abstraction which checks at compile-time if the platform supports this
- BeeOnRope 7y agoThose failures were all about top bits, not low bits though weren't they? What problems did lower bit use cause? You always had to mask it away even on the old boxes, no?
- notacoward 7y agoOne of my favorite system programming tricks is to never believe that a "zero cost abstraction" lives up to the name.
- hinkley 7y agoSometimes programmer advice needs disclaimers like prescription medicines. † Offer only valid in the contiguous United States. Offer cannot be used in combination with any other offers. See stores for details. ‡ When not in use, return Happy Fun Ball™ to its protective lead case.
- jjuhl 7y agoModern optimizing C++ compilers (especially with Link Time Optimization enabled) are pretty amazing and can very often actually achieve that abstraction collapsing.. But, of course, always measure.
- notacoward 7y agoWhile it's true that modern compilers are wondrous things, checking whether they're clever enough to optimize away a particular construct - and to do so correctly, and to continue doing so in the next release - still takes time. If the same optimization can be done at a higher level, such that it will apply for any correct (but not necessarily clever) compiler, that's preferable. In my experience that's practically all the time. The best compiler optimizations IMO are the ones that can't be easily done at the source level.
- vardump 7y agoYeah, don't believe in dogmas, measure. Zero cost abstractions sometimes are anything but.
- hinkley 7y agoAnd sometimes the surprises can be in the other direction. Cache thrashing is a thing. And perf analysis tools have bookkeeping errors.
- saagarjha 7y agoThe abstraction necessary to make tagged pointers platform-dependent is quite small and trivially removed by any optimizing compiler.
- groby_b 7y agoThat does not hold for Intel x86 architecture chips, which are perfectly happy to have unaligned integers. As for struct members, the alignment is (of course) "implementation defined", which is the fancy way of throwing up your hands and saying "whatever". (Since C++11 we actually have alignas(), which at least gives manual control)
- astrobe_ 7y agoOne can construct an aligned pointer on a chunk of allocated memory by asking for the needed quantity plus the size of the alignment and the "nudge" the pointer you get to the next word boundary (and you can do something with the "wasted" byte before it, like storing the size of the object). But that sort of trick is only worth it if you are writing a compiler and its runtime, an interpreter, a memory allocator (in particular GCs) or at the very last some sort of high performance library (you would return your "featured pointer" as an opaque type to the user).
- tynorf 7y agoMany (most? all?) allocators are guaranteed to give you back aligned pointers. For instance, jemalloc(3) on amd64 platforms will give back 16-byte aligned pointers: [...] The allocated space is suitably aligned (after possible pointer coercion) for storage of any type of object.