You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a multiphysics simulator and we switched from the standard VS allocator to mimalloc. That gave us for free an overall 10% decrease on wall time in our regression test suit! That was really impressive. Since we use some tagged pointers in the code, alignment guarantees are important for us and I want to fully understand what mimalloc is doing in order not to shoot myself in the foot!
In Visual Studio, malloc always returns an 8 byte aligned region for 32 bits code and a 16 bytes aligned region for 64 bits code.
When using mimalloc to override malloc and friends, the alignment guarantees are a bit different, and depend on the allocation bucket size, which is always a multiple of sizeof(uintptr_t) - 4 bytes on 32 bits and 8 bytes on 64 bits.
Available bucket sizes can be found on the MI_PAGE_QUEUES_EMPTY macro and multiples include 1, 3, 5, and 7 which gives bucket sizes that are NOT multiples of 8/16, so, for example, if an allocation fits in a 24 bytes bucket (but not in a 16 bytes bucket), the returned pointer will be 8 bytes aligned on a 64 bits machine. For bigger bucket sizes there are no differences since they will always be a multiple of 8/16.
By default, MI_MAX_ALIGN_SIZE is 16 bytes, but this doesn't really affect allocations smaller than 16 bytes. On 64-bit platforms, the used bins are 8-bytes, 16-bytes, 32-bytes, 48 bytes, ... On 32-bit platforms, there's also a bin for 4-byte allocations. As you noticed, these are expressed as multiples of the word size. I think the 3, 5, and 7 bins are not used unless you change the value of MI_MAX_ALIGN_SIZE.
For tagged pointers, the 1-word bin might be a problem. The alignment and choice of bin is controlled by _mi_bin. You can either modify _mi_bin or ensure that all your allocations are larger than 8 bytes.
Thanks for the comments. Looking at _mi_bin() clarifies a lot of things.
Since the odd bucket sizes (3, 5 and 7) are not used with MI_ALIGN2W defined, I should have no problems since my tagged pointers are always either double vectors or pointers to complex structures with sizes greater than 8 bytes.
Never the less, understanding what is going on is important for future changes. Thanks.
catmendesSam Gross (@colesbury) An object of size 16, can be created like so alignas ( 16 ) char, this creates a 16-byte object, containing 1 char of size 1 byte, the rest is padding (this requires to take care with memcpy, which will possibly copy a lot of rubbish.).
Activity
colesbury commentedon Feb 5, 2020
I had a similar issue. Here's what I found:
By default,
MI_MAX_ALIGN_SIZEis 16 bytes, but this doesn't really affect allocations smaller than 16 bytes. On 64-bit platforms, the used bins are 8-bytes, 16-bytes, 32-bytes, 48 bytes, ... On 32-bit platforms, there's also a bin for 4-byte allocations. As you noticed, these are expressed as multiples of the word size. I think the 3, 5, and 7 bins are not used unless you change the value ofMI_MAX_ALIGN_SIZE.For tagged pointers, the 1-word bin might be a problem. The alignment and choice of bin is controlled by
_mi_bin. You can either modify_mi_binor ensure that all your allocations are larger than 8 bytes.mimalloc/src/page-queue.c
Lines 100 to 137 in 6a744a8
catmendes commentedon Feb 6, 2020
Hi Sam Gross (@colesbury) ,
Thanks for the comments. Looking at _mi_bin() clarifies a lot of things.
Since the odd bucket sizes (3, 5 and 7) are not used with MI_ALIGN2W defined, I should have no problems since my tagged pointers are always either double vectors or pointers to complex structures with sizes greater than 8 bytes.
Never the less, understanding what is going on is important for future changes. Thanks.
degski commentedon Feb 10, 2020
catmendes Sam Gross (@colesbury) An object of size 16, can be created like so
alignas ( 16 ) char, this creates a 16-byte object, containing 1 char of size 1 byte, the rest is padding (this requires to take care withmemcpy, which will possibly copy a lot of rubbish.).daanx commentedon Mar 7, 2020
Apologies for not replying earlier, but Sam Gross (@colesbury) is right; Generally, on 64-bit:
Internally, but perhaps not guaranteed in the future, any power-of-2 size up to 4096 is also aligned to its own size.