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

QList::reserve unconditionally copies elements

    XMLWordPrintable

Details

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

    Description

      void QList<T>::reserve(qsizetype asize)
      {
          // capacity() == 0 for immutable data, so this will force a detaching below
          if (asize <= capacity() - d.freeSpaceAtBegin()) {
              if (d->flags() & Data::CapacityReserved)
                  return;  // already reserved, don't shrink
              if (!d->isShared()) {
                  // accept current allocation, don't shrink
                  d->setFlag(Data::CapacityReserved);
                  return;
              }
          }
      
          DataPointer detached(qMax(asize, size()));
          detached->copyAppend(d->begin(), d->end());
          if (detached.d_ptr())
              detached->setFlag(Data::CapacityReserved);
          d.swap(detached);
      }
      

      If we're already detached, but we need to reallocate because we've been asked to grow, this doesn't even attempt to exploit a possible realloc() fast path (for trivially relocatable types). It also copies elements instead of moving them.

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            peppe Giuseppe D'Angelo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes