[Development] QVector reserve counterproductive?
Martins, Sérgio
sergio.martins at kdab.com
Sat Mar 3 23:22:09 CET 2018
On 2018-03-03 20:38, Christian Ehrlicher wrote:
> Hi,
>
> while digging through the bugreports I found
> https://bugreports.qt.io/browse/QTBUG-66677 which seems to show a
> downside of QVector::reserve().
Yes, reserve() shouldn't be called repeatedly inside loops. Instead use
it only once,
outside your outermost loop.
> QPainterPath::addPolygon() is calling reserve() to make sure to have
> enough space for the new positions. This is exactly what I would have
> done and what e.g. CLazy suggests.
Not really, see "Example where reserve shouldn't be used" in
https://github.com/KDE/clazy/blob/master/src/checks/level2/README-reserve-candidates.md
and also the pitfalls section where it talkes about using the growth
strategy instead
> But it looks like reserve()
> allocates *exactly* the amount of elements given.
Actually that qpainterpath code is off-by-one, it should be:
d_func()->elements.reserve(d_func()->elements.size() + polygon.size() -
1);
which also fixes the performance problem (by luck, in that specific
benchmark). Please test.
Ideas welcome on how we can catch more of these cases...
Regards,
--
Sérgio Martins | sergio.martins at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - The Qt, C++ and OpenGL Experts
More information about the Development
mailing list