Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122305

Allow polymorphic allocators to be used for some (container) types

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.0.0
    • None
    • All

    Description

      For example to avoid memory fragmentation has C++17 introduced the possibility to use polymorphic allocators.

      A very useful such allocator is for example std::pmr::synchrononized_pool_resource.

      A std:pmr::(un)synchronized_pool_resource allocates chunks to fulfill requests for objects, and tries to reuse opened areas in those chunks when objects are released. When destroyed the allocator deletes all the chunks entirely

      • This is a bit like g_slice_alloc (which is also sometimes called a magazine allocator for equal sized objects).
      • For different sized objects it tries to manage the least amount of chunks needed (each chunk is for objects of all in that chunk being the same size)
        • So it's a bit like g_slice_alloc but with some management layer above it that arranges new 'magazines' (~ equivalent to chunks) for you
      • Allows you to configure the maximum amount of equal sized objects (called blocks in the API docs) per chunk
        • This is useful to tune how big the chunks will be. You want a large maximum in most cases.
      • If I understand it correctly the (un)synchronized_pool_resource uses the upstream allocator (that you pass at the constructor, or the default memory_resource if not) to (de)allocate its internal chunks

      Admittedly should modern code probably be written to use types from std. An example type that should be easy to deal with is the QVector<T> (to be replaced with std::pmr::vector<T>).

      However, for existing code this is often not so simple. Plus Qt of course has its implicit sharing mechanism. This is also very useful to avoid memory fragmentation. Especially for for example the QString type (avoiding copies).

      For example QByteArray and QString would benefit a lot, I think, from making it possible to pass the allocator at construction.

      Note that I think it is for QByteArray already possible if you use QByteArray::fromRawData. ie.

      std::array<char, 1024> buffer;
      std::pmr::monotomic_buffer_resource memory_resource( &buffer, 1024 );
      std::pmr::vector<char> data { &memory_resource };
      data.emplace_back(....);
      QByteArray byteArray = QByteArray::fromRawData(data.data(), sizeof(data));
      

       
      ps. Looks like QString has such an API too.

      ps. This training video explains very well how to adapt your types to make using such allocators possible. Here is another document that explains this quite well.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            pvanhoof Philip Van Hoof
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes