Search code examples
c++allocatorhint

Example uses of hint parameter in the implementation of an allocator


Standard allocators can optionally hints as default parameters https://en.cppreference.com/w/cpp/memory/allocator/allocate

T* allocate( std::size_t n, const void * hint);

Leaving aside that this is formally deprecated in C++20 (which apparently doesn't mean that an allocator cannot have the hint argument):

Do you know of good uses of the hint in standard or non-standard allocator implementations in existing code or theoretical code? Or is it a plain historical relic?

I am trying to understand if the current hint can help with allocating when you have more than one device (e.g. gpu).

Note 1: I am not asking how to allocate memory in cpu or gpus, I am trying to see good or proven code that used this hint parameter internally, presumably efficiency and for particular types of memory. Even if it is some exotic system.

Note 2: I am not asking how/if/what to pass as argument of hint (i.e. "just pass the current pointer of your container"), like in the linked question. I am asking from the point of view of someone implementing a custom allocator.


Solution

  • Work by Alin Jula and Lawrence Rauchwerger

    The paper "Two memory allocators that use hints to improve locality" by Alin Jula and Lawrence Rauchwerger from 2009 introduces

    Two locality improving allocators that can use allocation hints provided from the C++ STL library and outperform state-of- the-art allocators, such as dlmalloc and PHKmalloc, by an average of 7%, and 17% respectively, while yielding memory fragmentation as low as dlmalloc’s.

    They call the allocators "TP" and "Medius" and they use use the allocate()'s hint parameter to try and allocate the memory near the hint to improve data locality. To this end, they modified the containers in gcc's stdlibc++ to actually pass hints to the allocators. The corresponding dissertation by Alin Jula is available for free.

    The work is preceded by "Custom Memory Allocation for Free" from 2006 by the same authors, which introduces the allocator "Defero". It apparently uses the same interface (i.e. the hint parameter of the allocate() function).

    Unfortunately, the link to the source code (http://parasol.tamu.edu/resources/downloads.php) is dead and I was not able to find them online. However, L. Rauchwerger seems to be still around and maybe he might be willing to share the source if you contact him.

    Using the hint for mmap

    The mmap() function allows to provide a hint address for the memory. I was able to find some source (author's website) that actually does this:

    namespace leimyalloc {
      template <typename T>
      class mmap_allocator {
        // ...
        pointer allocate (size_type num, void *  hint = 0) {
          pointer p = (pointer) ::mmap(hint, num, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
          int * val = (int *)p;
          if(val == MAP_FAILED)
            p = 0;
          return p;
        }
        // ...
      };
    }
    

    However, I could not find if anyone actually uses it.

    Searching for "mmap allocator" gives for example this and this github project, but they simply ignore the hint parameter.