Skip to content

Alignment guarantees #199

Closed
Closed

Description

@catmendes

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.

Is this understanding correct?

Thanks,
catmendes

Activity

  1. colesbury commented on Feb 5, 2020

    @colesbury
    Contributor

    I had a similar issue. Here's what I found:

    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.

    mimalloc/src/page-queue.c

    Lines 100 to 137 in 6a744a8

    extern inline uint8_t _mi_bin(size_t size) {
    size_t wsize = _mi_wsize_from_size(size);
    uint8_t bin;
    if (wsize <= 1) {
    bin = 1;
    }
    #if defined(MI_ALIGN4W)
    else if (wsize <= 4) {
    bin = (uint8_t)((wsize+1)&~1); // round to double word sizes
    }
    #elif defined(MI_ALIGN2W)
    else if (wsize <= 8) {
    bin = (uint8_t)((wsize+1)&~1); // round to double word sizes
    }
    #else
    else if (wsize <= 8) {
    bin = (uint8_t)wsize;
    }
    #endif
    else if (wsize > MI_LARGE_OBJ_WSIZE_MAX) {
    bin = MI_BIN_HUGE;
    }
    else {
    #if defined(MI_ALIGN4W)
    if (wsize <= 16) { wsize = (wsize+3)&~3; } // round to 4x word sizes
    #endif
    wsize--;
    // find the highest bit
    uint8_t b = mi_bsr32((uint32_t)wsize);
    // and use the top 3 bits to determine the bin (~12.5% worst internal fragmentation).
    // - adjust with 3 because we use do not round the first 8 sizes
    // which each get an exact bin
    bin = ((b << 2) + (uint8_t)((wsize >> (b - 2)) & 0x03)) - 3;
    mi_assert_internal(bin < MI_BIN_HUGE);
    }
    mi_assert_internal(bin > 0 && bin <= MI_BIN_HUGE);
    return bin;
    }

  2. catmendes commented on Feb 6, 2020

    @catmendes
    Author

    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.

  3. degski commented on Feb 10, 2020

    @degski

    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 with memcpy, which will possibly copy a lot of rubbish.).

  4. daanx commented on Mar 7, 2020

    @daanx
    Collaborator

    Apologies for not replying earlier, but Sam Gross (@colesbury) is right; Generally, on 64-bit:

    • up to 8 bytes is 8 byte aligned
    • anything larger is at least 16-byte aligned
    • If you use the aligned allocation API the alignment is always respected (recently).

    Internally, but perhaps not guaranteed in the future, any power-of-2 size up to 4096 is also aligned to its own size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions