[Development] Important recent changes in QList/QString/QByteArray

Ville Voutilainen ville.voutilainen at gmail.com
Thu Sep 10 14:43:00 CEST 2020


On Thu, 10 Sep 2020 at 11:46, Andrei Golubev <andrei.golubev at qt.io> wrote:
>
> That's interesting. So the container remembers what sort of a reserve
> request I made on it, and uses
> that as the preferred size whenever the element count of the container changes?
>
> Yes. Calling reserve typically means that you do not want to have extra reallocations while updating the container's content (instead you allocate sufficient space upfront). In case of QList, we make sure there are no reallocations happening unless absolutely necessary, e.g. appending 1 element when size == capacity would allocate bigger memory chunk - reservation is a hint, but not an obligation. Consequently, QList's shrinking erase also does not shrink when capacity is reserved.

Well, yeah - calling reserve before growing a container avoids extra
allocations. Calling reserve before removing elements
doesn't have such a "typical" effect.

Should I expect to do a reserve call with a *smaller* value than my
current size is before removing elements from
a container, in order to set a "target size"? For any standard
container, such a reserve is a complete noop because
it can't be responded to by shrinking a capacity lower than the size,
but with QList it apparently records a preferred size?


More information about the Development mailing list