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

QVector offers no functionality to remove all items without releasing memory

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Fixed
    • P5: Not important
    • 5.6
    • 5.0.2, 5.3.0
    • None
    • 9f591b3b6ca4d3c0de234f04bb9a2deb8d784170

    Description

      QVector, and possibly other containers, need the ability to remove all items without affecting the amount of memory that is reserved for the container.

      The clear() method on QVector currently releases all memory that was previously allocated for elements in the container. The same applies to a call of resize(0). This can be good for not wasting memory when it might be needed elsewhere, but this introduces a significant performance impact without providing any alternatives.

      Consider a scenario where the user wishes to repeatedly clear and fill a vector. Many precious cycles will be wasted in the process of repeatedly deallocating and reallocating the vector's internal memory from the heap, even if the exact same amount of memory is required for each iteration. These repeated reallocations also increase the likelihood of memory fragmentation, which can be counterproductive to the idea of not wasting memory.

      A better solution in the above scenario would be to not release the memory each time the container is cleared, but simply set the number of items to 0 and keep the memory reserved for future use. This avoids having to do anything with the memory heap until there is an actual need for more memory.

      If the user is concerned about the container reserving more memory than is needed at any point, a simple call to the squeeze() method will release any unused memory.

      Because there may be existing code that currently relies on the clear() method releasing the container's memory, it may be important to not change the existing functionality. But an alternative option really must be provided for when performance is important. An alternative option could be in the form of an optional parameter such as "clear(releaseMemory = true)", or a separate function like "removeAllItems()", or maybe even a flag on the container along the lines of "autoReleaseMemory = doNotRelease". There just needs to be some way of avoiding these unwanted deallocations when the user knows they want to keep the existing memory reserved.

      I have been focusing on the clear() method, but this concept should also apply in other similar cases, such as the resize() method, whether resizing down to 0, or resizing from 100 to 10. The current implementation of resize() appears to release memory if less than half of the reserved elements are used.

      This concept should also ideally be applied to all containers in Qt, not just QVector, but QVector is likely the first and most important container for this, because it is the most likely container to be used in places where speed efficiency is highly important.

      Attachments

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

        Activity

          People

            mmutz Marc Mutz
            sc-jeffrey SC-Jeffrey
            Votes:
            5 Vote for this issue
            Watchers:
            10 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes