From b566d670e5d0c0313c4b935ae8ec83f7355a55ed Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 3 Dec 2015 13:47:27 +0100 Subject: [PATCH] QMovie: fix quadratic behavior Repeatedly calling QList::erase(it) (via QMutableListIterator::remove()) results in quadratic runtime. Use std::remove_if, which does exactly what the old code tried to do, except in linear time. Change-Id: I682e5e05f04953ae1c8788e5d66335241de39fee Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/gui/image/qmovie.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/gui/image/qmovie.cpp b/src/gui/image/qmovie.cpp index 09cd788c61..ba27fb355b 100644 --- a/src/gui/image/qmovie.cpp +++ b/src/gui/image/qmovie.cpp @@ -965,14 +965,16 @@ void QMovie::setScaledSize(const QSize &size) QList QMovie::supportedFormats() { QList list = QImageReader::supportedImageFormats(); - QMutableListIterator it(list); + QBuffer buffer; buffer.open(QIODevice::ReadOnly); - while (it.hasNext()) { - QImageReader reader(&buffer, it.next()); - if (!reader.supportsAnimation()) - it.remove(); - } + + const auto doesntSupportAnimation = + [&buffer](const QByteArray &format) { + return !QImageReader(&buffer, format).supportsAnimation(); + }; + + list.erase(std::remove_if(list.begin(), list.end(), doesntSupportAnimation), list.end()); return list; }