Reduce allocations by using reserve()
Change-Id: If34fa53402985f6b3c5e7217bce4a1177af835b6 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
af499fd7eb
commit
01859cc121
@ -417,6 +417,7 @@ QList<QByteArray> QIcuCodec::availableCodecs()
|
|||||||
QList<int> QIcuCodec::availableMibs()
|
QList<int> QIcuCodec::availableMibs()
|
||||||
{
|
{
|
||||||
QList<int> mibs;
|
QList<int> mibs;
|
||||||
|
mibs.reserve(mibToNameSize + 1);
|
||||||
for (int i = 0; i < mibToNameSize; ++i)
|
for (int i = 0; i < mibToNameSize; ++i)
|
||||||
mibs += mibToName[i].mib;
|
mibs += mibToName[i].mib;
|
||||||
|
|
||||||
|
@ -276,7 +276,9 @@ void QStringListModel::sort(int, Qt::SortOrder order)
|
|||||||
|
|
||||||
QModelIndexList oldList = persistentIndexList();
|
QModelIndexList oldList = persistentIndexList();
|
||||||
QModelIndexList newList;
|
QModelIndexList newList;
|
||||||
for (int i = 0; i < oldList.count(); ++i)
|
const int numOldIndexes = oldList.count();
|
||||||
|
newList.reserve(numOldIndexes);
|
||||||
|
for (int i = 0; i < numOldIndexes; ++i)
|
||||||
newList.append(index(forwarding.at(oldList.at(i).row()), 0));
|
newList.append(index(forwarding.at(oldList.at(i).row()), 0));
|
||||||
changePersistentIndexList(oldList, newList);
|
changePersistentIndexList(oldList, newList);
|
||||||
|
|
||||||
|
@ -1623,6 +1623,7 @@ void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
|
|||||||
QList<QByteArray> QImageReader::supportedImageFormats()
|
QList<QByteArray> QImageReader::supportedImageFormats()
|
||||||
{
|
{
|
||||||
QList<QByteArray> formats;
|
QList<QByteArray> formats;
|
||||||
|
formats.reserve(_qt_NumFormats);
|
||||||
for (int i = 0; i < _qt_NumFormats; ++i)
|
for (int i = 0; i < _qt_NumFormats; ++i)
|
||||||
formats << _qt_BuiltInFormats[i].extension;
|
formats << _qt_BuiltInFormats[i].extension;
|
||||||
|
|
||||||
@ -1647,6 +1648,7 @@ QList<QByteArray> QImageReader::supportedImageFormats()
|
|||||||
QList<QByteArray> QImageReader::supportedMimeTypes()
|
QList<QByteArray> QImageReader::supportedMimeTypes()
|
||||||
{
|
{
|
||||||
QList<QByteArray> mimeTypes;
|
QList<QByteArray> mimeTypes;
|
||||||
|
mimeTypes.reserve(_qt_NumFormats);
|
||||||
for (int i = 0; i < _qt_NumFormats; ++i)
|
for (int i = 0; i < _qt_NumFormats; ++i)
|
||||||
mimeTypes << _qt_BuiltInFormats[i].mimeType;
|
mimeTypes << _qt_BuiltInFormats[i].mimeType;
|
||||||
|
|
||||||
|
@ -1003,7 +1003,8 @@ void QPdfEngine::drawHyperlink(const QRectF &r, const QUrl &url)
|
|||||||
const uint annot = d->addXrefEntry(-1);
|
const uint annot = d->addXrefEntry(-1);
|
||||||
const QByteArray urlascii = url.toEncoded();
|
const QByteArray urlascii = url.toEncoded();
|
||||||
int len = urlascii.size();
|
int len = urlascii.size();
|
||||||
QVarLengthArray<char> url_esc(0);
|
QVarLengthArray<char> url_esc;
|
||||||
|
url_esc.reserve(len + 1);
|
||||||
for (int j = 0; j < len; j++) {
|
for (int j = 0; j < len; j++) {
|
||||||
if (urlascii[j] == '(' || urlascii[j] == ')' || urlascii[j] == '\\')
|
if (urlascii[j] == '(' || urlascii[j] == ')' || urlascii[j] == '\\')
|
||||||
url_esc.append('\\');
|
url_esc.append('\\');
|
||||||
@ -2007,10 +2008,11 @@ int QPdfEnginePrivate::createShadingFunction(const QGradient *gradient, int from
|
|||||||
}
|
}
|
||||||
|
|
||||||
QVector<QGradientBound> gradientBounds;
|
QVector<QGradientBound> gradientBounds;
|
||||||
|
gradientBounds.reserve((to - from) * (numStops - 1));
|
||||||
|
|
||||||
for (int step = from; step < to; ++step) {
|
for (int step = from; step < to; ++step) {
|
||||||
if (reflect && step % 2) {
|
if (reflect && step % 2) {
|
||||||
for (int i = stops.size() - 1; i > 0; --i) {
|
for (int i = numStops - 1; i > 0; --i) {
|
||||||
QGradientBound b;
|
QGradientBound b;
|
||||||
b.start = step + 1 - qBound(qreal(0.), stops.at(i).first, qreal(1.));
|
b.start = step + 1 - qBound(qreal(0.), stops.at(i).first, qreal(1.));
|
||||||
b.stop = step + 1 - qBound(qreal(0.), stops.at(i - 1).first, qreal(1.));
|
b.stop = step + 1 - qBound(qreal(0.), stops.at(i - 1).first, qreal(1.));
|
||||||
@ -2019,7 +2021,7 @@ int QPdfEnginePrivate::createShadingFunction(const QGradient *gradient, int from
|
|||||||
gradientBounds << b;
|
gradientBounds << b;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < stops.size() - 1; ++i) {
|
for (int i = 0; i < numStops - 1; ++i) {
|
||||||
QGradientBound b;
|
QGradientBound b;
|
||||||
b.start = step + qBound(qreal(0.), stops.at(i).first, qreal(1.));
|
b.start = step + qBound(qreal(0.), stops.at(i).first, qreal(1.));
|
||||||
b.stop = step + qBound(qreal(0.), stops.at(i + 1).first, qreal(1.));
|
b.stop = step + qBound(qreal(0.), stops.at(i + 1).first, qreal(1.));
|
||||||
|
@ -691,7 +691,9 @@ static QStringList familyList(const QFontDef &req)
|
|||||||
return family_list;
|
return family_list;
|
||||||
|
|
||||||
QStringList list = req.family.split(QLatin1Char(','));
|
QStringList list = req.family.split(QLatin1Char(','));
|
||||||
for (int i = 0; i < list.size(); ++i) {
|
const int numFamilies = list.size();
|
||||||
|
family_list.reserve(numFamilies);
|
||||||
|
for (int i = 0; i < numFamilies; ++i) {
|
||||||
QString str = list.at(i).trimmed();
|
QString str = list.at(i).trimmed();
|
||||||
if ((str.startsWith(QLatin1Char('"')) && str.endsWith(QLatin1Char('"')))
|
if ((str.startsWith(QLatin1Char('"')) && str.endsWith(QLatin1Char('"')))
|
||||||
|| (str.startsWith(QLatin1Char('\'')) && str.endsWith(QLatin1Char('\''))))
|
|| (str.startsWith(QLatin1Char('\'')) && str.endsWith(QLatin1Char('\''))))
|
||||||
@ -1607,6 +1609,7 @@ QStringList QFontDatabase::styles(const QString &family) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.reserve(allStyles.count);
|
||||||
for (int i = 0; i < allStyles.count; i++) {
|
for (int i = 0; i < allStyles.count; i++) {
|
||||||
l.append(allStyles.styles[i]->styleName.isEmpty() ?
|
l.append(allStyles.styles[i]->styleName.isEmpty() ?
|
||||||
styleStringHelper(allStyles.styles[i]->key.weight,
|
styleStringHelper(allStyles.styles[i]->key.weight,
|
||||||
|
@ -1159,7 +1159,6 @@ QByteArray QFontSubset::toTruetype() const
|
|||||||
|
|
||||||
qreal ppem = fontEngine->fontDef.pixelSize;
|
qreal ppem = fontEngine->fontDef.pixelSize;
|
||||||
#define TO_TTF(x) qRound(x * 2048. / ppem)
|
#define TO_TTF(x) qRound(x * 2048. / ppem)
|
||||||
QVector<QTtfGlyph> glyphs;
|
|
||||||
|
|
||||||
QFontEngine::Properties properties = fontEngine->properties();
|
QFontEngine::Properties properties = fontEngine->properties();
|
||||||
// initialize some stuff needed in createWidthArray
|
// initialize some stuff needed in createWidthArray
|
||||||
@ -1194,12 +1193,13 @@ QByteArray QFontSubset::toTruetype() const
|
|||||||
font.maxp.maxCompositeContours = 0;
|
font.maxp.maxCompositeContours = 0;
|
||||||
font.maxp.maxComponentElements = 0;
|
font.maxp.maxComponentElements = 0;
|
||||||
font.maxp.maxComponentDepth = 0;
|
font.maxp.maxComponentDepth = 0;
|
||||||
font.maxp.numGlyphs = nGlyphs();
|
const int numGlyphs = nGlyphs();
|
||||||
|
font.maxp.numGlyphs = numGlyphs;
|
||||||
|
QVector<QTtfGlyph> glyphs;
|
||||||
|
glyphs.reserve(numGlyphs);
|
||||||
|
|
||||||
uint sumAdvances = 0;
|
uint sumAdvances = 0;
|
||||||
for (int i = 0; i < nGlyphs(); ++i) {
|
for (int i = 0; i < numGlyphs; ++i) {
|
||||||
glyph_t g = glyph_indices.at(i);
|
glyph_t g = glyph_indices.at(i);
|
||||||
QPainterPath path;
|
QPainterPath path;
|
||||||
glyph_metrics_t metric;
|
glyph_metrics_t metric;
|
||||||
|
@ -174,9 +174,11 @@ QXcbIntegration::QXcbIntegration(const QStringList ¶meters, int &argc, char
|
|||||||
if (canNotGrabEnv)
|
if (canNotGrabEnv)
|
||||||
m_canGrab = false;
|
m_canGrab = false;
|
||||||
|
|
||||||
|
const int numParameters = parameters.size();
|
||||||
|
m_connections.reserve(1 + numParameters / 2);
|
||||||
m_connections << new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, displayName);
|
m_connections << new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, displayName);
|
||||||
|
|
||||||
for (int i = 0; i < parameters.size() - 1; i += 2) {
|
for (int i = 0; i < numParameters - 1; i += 2) {
|
||||||
qCDebug(lcQpaScreen) << "connecting to additional display: " << parameters.at(i) << parameters.at(i+1);
|
qCDebug(lcQpaScreen) << "connecting to additional display: " << parameters.at(i) << parameters.at(i+1);
|
||||||
QString display = parameters.at(i) + QLatin1Char(':') + parameters.at(i+1);
|
QString display = parameters.at(i) + QLatin1Char(':') + parameters.at(i+1);
|
||||||
m_connections << new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, display.toLatin1().constData());
|
m_connections << new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, display.toLatin1().constData());
|
||||||
|
@ -1563,7 +1563,9 @@ static void qPrintDataTags(FILE *stream)
|
|||||||
member.resize(qstrlen(slot) + qstrlen("_data()") + 1);
|
member.resize(qstrlen(slot) + qstrlen("_data()") + 1);
|
||||||
qsnprintf(member.data(), member.size(), "%s_data()", slot);
|
qsnprintf(member.data(), member.size(), "%s_data()", slot);
|
||||||
invokeMethod(QTest::currentTestObject, member.constData());
|
invokeMethod(QTest::currentTestObject, member.constData());
|
||||||
for (int j = 0; j < table.dataCount(); ++j)
|
const int dataCount = table.dataCount();
|
||||||
|
localTags.reserve(dataCount);
|
||||||
|
for (int j = 0; j < dataCount; ++j)
|
||||||
localTags << QLatin1String(table.testData(j)->dataTag());
|
localTags << QLatin1String(table.testData(j)->dataTag());
|
||||||
|
|
||||||
// Print all tag combinations:
|
// Print all tag combinations:
|
||||||
|
@ -396,6 +396,7 @@ static void parseCmdLine(QStringList &arguments)
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
QStringList args;
|
QStringList args;
|
||||||
|
args.reserve(argc - 1);
|
||||||
for (int n = 1; n < argc; ++n)
|
for (int n = 1; n < argc; ++n)
|
||||||
args.append(QString::fromLocal8Bit(argv[n]));
|
args.append(QString::fromLocal8Bit(argv[n]));
|
||||||
parseCmdLine(args);
|
parseCmdLine(args);
|
||||||
|
@ -389,7 +389,10 @@ static QStringList makeArgNames(const QDBusIntrospection::Arguments &inputArgs,
|
|||||||
QDBusIntrospection::Arguments())
|
QDBusIntrospection::Arguments())
|
||||||
{
|
{
|
||||||
QStringList retval;
|
QStringList retval;
|
||||||
for (int i = 0; i < inputArgs.count(); ++i) {
|
const int numInputArgs = inputArgs.count();
|
||||||
|
const int numOutputArgs = outputArgs.count();
|
||||||
|
retval.reserve(numInputArgs + numOutputArgs);
|
||||||
|
for (int i = 0; i < numInputArgs; ++i) {
|
||||||
const QDBusIntrospection::Argument &arg = inputArgs.at(i);
|
const QDBusIntrospection::Argument &arg = inputArgs.at(i);
|
||||||
QString name = arg.name;
|
QString name = arg.name;
|
||||||
if (name.isEmpty())
|
if (name.isEmpty())
|
||||||
@ -400,7 +403,7 @@ static QStringList makeArgNames(const QDBusIntrospection::Arguments &inputArgs,
|
|||||||
name += QLatin1String("_");
|
name += QLatin1String("_");
|
||||||
retval << name;
|
retval << name;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < outputArgs.count(); ++i) {
|
for (int i = 0; i < numOutputArgs; ++i) {
|
||||||
const QDBusIntrospection::Argument &arg = outputArgs.at(i);
|
const QDBusIntrospection::Argument &arg = outputArgs.at(i);
|
||||||
QString name = arg.name;
|
QString name = arg.name;
|
||||||
if (name.isEmpty())
|
if (name.isEmpty())
|
||||||
@ -1145,7 +1148,7 @@ static void writeAdaptor(const QString &filename, const QDBusIntrospection::Inte
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
|
arguments.reserve(argc);
|
||||||
for (int i = 0; i < argc; ++i) {
|
for (int i = 0; i < argc; ++i) {
|
||||||
arguments.append(QString::fromLocal8Bit(argv[i]));
|
arguments.append(QString::fromLocal8Bit(argv[i]));
|
||||||
}
|
}
|
||||||
|
@ -2200,8 +2200,10 @@ QList<WriteInitialization::Item *> WriteInitialization::initializeTreeWidgetItem
|
|||||||
{
|
{
|
||||||
// items
|
// items
|
||||||
QList<Item *> items;
|
QList<Item *> items;
|
||||||
|
const int numDomItems = domItems.size();
|
||||||
|
items.reserve(numDomItems);
|
||||||
|
|
||||||
for (int i = 0; i < domItems.size(); ++i) {
|
for (int i = 0; i < numDomItems; ++i) {
|
||||||
const DomItem *domItem = domItems.at(i);
|
const DomItem *domItem = domItems.at(i);
|
||||||
|
|
||||||
Item *item = new Item(QLatin1String("QTreeWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
|
Item *item = new Item(QLatin1String("QTreeWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
|
||||||
|
@ -460,6 +460,7 @@ void QSidebar::removeEntry()
|
|||||||
QList<QModelIndex> idxs = selectionModel()->selectedIndexes();
|
QList<QModelIndex> idxs = selectionModel()->selectedIndexes();
|
||||||
QList<QPersistentModelIndex> indexes;
|
QList<QPersistentModelIndex> indexes;
|
||||||
const int numIndexes = idxs.count();
|
const int numIndexes = idxs.count();
|
||||||
|
indexes.reserve(numIndexes);
|
||||||
for (int i = 0; i < numIndexes; i++)
|
for (int i = 0; i < numIndexes; i++)
|
||||||
indexes.append(idxs.at(i));
|
indexes.append(idxs.at(i));
|
||||||
|
|
||||||
|
@ -1242,13 +1242,15 @@ void QWidgetBackingStore::doSync()
|
|||||||
// OpenGL content changes. Check if we have such widgets in the special
|
// OpenGL content changes. Check if we have such widgets in the special
|
||||||
// dirty list.
|
// dirty list.
|
||||||
QVarLengthArray<QWidget *, 16> paintPending;
|
QVarLengthArray<QWidget *, 16> paintPending;
|
||||||
for (int i = 0; i < dirtyRenderToTextureWidgets.count(); ++i) {
|
const int numPaintPending = dirtyRenderToTextureWidgets.count();
|
||||||
|
paintPending.reserve(numPaintPending);
|
||||||
|
for (int i = 0; i < numPaintPending; ++i) {
|
||||||
QWidget *w = dirtyRenderToTextureWidgets.at(i);
|
QWidget *w = dirtyRenderToTextureWidgets.at(i);
|
||||||
paintPending << w;
|
paintPending << w;
|
||||||
resetWidget(w);
|
resetWidget(w);
|
||||||
}
|
}
|
||||||
dirtyRenderToTextureWidgets.clear();
|
dirtyRenderToTextureWidgets.clear();
|
||||||
for (int i = 0; i < paintPending.count(); ++i) {
|
for (int i = 0; i < numPaintPending; ++i) {
|
||||||
QWidget *w = paintPending[i];
|
QWidget *w = paintPending[i];
|
||||||
w->d_func()->sendPaintEvent(w->rect());
|
w->d_func()->sendPaintEvent(w->rect());
|
||||||
if (w != tlw) {
|
if (w != tlw) {
|
||||||
|
@ -3350,8 +3350,9 @@ QSet<QTabBar*> QDockAreaLayout::usedTabBars() const
|
|||||||
QSet<QWidget*> QDockAreaLayout::usedSeparatorWidgets() const
|
QSet<QWidget*> QDockAreaLayout::usedSeparatorWidgets() const
|
||||||
{
|
{
|
||||||
QSet<QWidget*> result;
|
QSet<QWidget*> result;
|
||||||
|
const int numSeparators = separatorWidgets.count();
|
||||||
for (int i = 0; i < separatorWidgets.count(); ++i)
|
result.reserve(numSeparators);
|
||||||
|
for (int i = 0; i < numSeparators; ++i)
|
||||||
result << separatorWidgets.at(i);
|
result << separatorWidgets.at(i);
|
||||||
for (int i = 0; i < QInternal::DockCount; ++i) {
|
for (int i = 0; i < QInternal::DockCount; ++i) {
|
||||||
const QDockAreaLayoutInfo &dock = docks[i];
|
const QDockAreaLayoutInfo &dock = docks[i];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user