Skip to content

Commit bdc39ff

Browse files
committed
Fix clazy reserve-candidates suggestions
Avoids repeated memory allocations
1 parent 904b004 commit bdc39ff

21 files changed

+32
-3
lines changed

src/app/qgisapp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,8 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
38603860

38613861
QgsSublayersDialog::LayerDefinitionList layers;
38623862
QStringList names;
3863+
names.reserve( sublayers.size() );
3864+
layers.reserve( sublayers.size() );
38633865
for ( int i = 0; i < sublayers.size(); i++ )
38643866
{
38653867
// simplify raster sublayer name - should add a function in gdal provider for this?

src/app/qgsvectorlayerproperties.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()
10521052

10531053
QList<QgsMapLayer*> joinedLayers;
10541054
const QList< QgsVectorJoinInfo >& joins = mLayer->vectorJoins();
1055+
joinedLayers.reserve( joins.size() );
10551056
for ( int i = 0; i < joins.size(); ++i )
10561057
{
10571058
joinedLayers.append( QgsMapLayerRegistry::instance()->mapLayer( joins[i].joinLayerId ) );

src/core/pal/costcalculator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition* >
104104
// IMPORTANT - only want to sort first nblp positions. The rest have not had the cost
105105
// calculated so will have nonsense values
106106
QList< LabelPosition* > toSort;
107+
toSort.reserve( nblp );
107108
for ( int i = 0; i < nblp; ++i )
108109
{
109110
toSort << lPos.at( i );

src/core/qgscolorramp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ QgsColorRamp* QgsPresetSchemeColorRamp::create( const QgsStringMap& properties )
784784
QList<QColor> QgsPresetSchemeColorRamp::colors() const
785785
{
786786
QList< QColor > l;
787+
l.reserve( mColors.count() );
787788
for ( int i = 0; i < mColors.count(); ++i )
788789
{
789790
l << mColors.at( i ).first;

src/core/qgsexpression.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,7 @@ static QVariant fcnOrderParts( const QVariantList& values, const QgsExpressionCo
25892589
QgsExpressionSorter sorter( orderBy );
25902590

25912591
QList<QgsFeature> partFeatures;
2592+
partFeatures.reserve( collection->partCount() );
25922593
for ( int i = 0; i < collection->partCount(); ++i )
25932594
{
25942595
f.setGeometry( QgsGeometry( collection->geometryN( i )->clone() ) );

src/core/qgslayerdefinition.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ void QgsLayerDefinition::DependencySorter::init( const QDomDocument& doc )
220220
QStringList layerIds;
221221

222222
QDomNodeList nl = doc.elementsByTagName( "maplayer" );
223+
layerIds.reserve( nl.count() );
224+
QVector<QString> deps; //avoid expensive allocation for list for every iteration
223225
for ( int i = 0; i < nl.count(); i++ )
224226
{
225-
QVector<QString> deps;
227+
deps.resize( 0 ); // preserve capacity - don't use clear
226228
QDomNode node = nl.item( i );
227-
QDomElement element = node.toElement();
228229

229230
QString id = node.namedItem( "id" ).toElement().text();
230231
layerIds << id;

src/core/raster/qgsrasterfilewriter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const Qgs
185185
QList<bool> destHasNoDataValueList;
186186
QList<double> destNoDataValueList;
187187
QList<Qgis::DataType> destDataTypeList;
188+
destDataTypeList.reserve( nBands );
189+
destHasNoDataValueList.reserve( nBands );
190+
destNoDataValueList.reserve( nBands );
191+
188192
for ( int bandNo = 1; bandNo <= nBands; bandNo++ )
189193
{
190194
QgsRasterNuller *nuller = pipe->nuller();

src/core/symbology-ng/qgsmarkersymbollayer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,7 @@ bool QgsSimpleMarkerSymbolLayer::writeDxf( QgsDxfExport& e, double mmMapUnitScal
13481348
polygon = t.map( polygon );
13491349

13501350
QgsPointSequence p;
1351+
p.reserve( polygon.size() );
13511352
for ( int i = 0; i < polygon.size(); i++ )
13521353
p << QgsPointV2( polygon[i] );
13531354
p << p[0];

src/gui/attributetable/qgsattributetablefiltermodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ void QgsAttributeTableFilterModel::setFilteredFeatures( const QgsFeatureIds& ids
271271
QgsFeatureIds QgsAttributeTableFilterModel::filteredFeatures()
272272
{
273273
QgsFeatureIds ids;
274+
ids.reserve( rowCount() );
274275
for ( int i = 0; i < rowCount(); ++i )
275276
{
276277
QModelIndex row = index( i, 0 );

src/gui/attributetable/qgsorganizetablecolumnsdialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ QgsOrganizeTableColumnsDialog::~QgsOrganizeTableColumnsDialog()
109109
QgsAttributeTableConfig QgsOrganizeTableColumnsDialog::config() const
110110
{
111111
QVector<QgsAttributeTableConfig::ColumnConfig> columns;
112+
columns.reserve( mFieldsList->count() );
112113

113114
for ( int i = 0 ; i < mFieldsList->count() ; i++ )
114115
{

src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ void QgsValueRelationSearchWidgetWrapper::initWidget( QWidget* editor )
272272
else if ( mLineEdit )
273273
{
274274
QStringList values;
275+
values.reserve( mCache.size() );
275276
Q_FOREACH ( const ValueRelationItem& i, mCache )
276277
{
277278
values << i.second;

src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget* editor )
141141
else if ( mLineEdit )
142142
{
143143
QStringList values;
144+
values.reserve( mCache.size() );
144145
Q_FOREACH ( const ValueRelationItem& i, mCache )
145146
{
146147
values << i.second;

src/gui/qgsdatumtransformdialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ QList< int > QgsDatumTransformDialog::selectedDatumTransform()
151151
QTreeWidgetItem * item = mDatumTransformTreeWidget->currentItem();
152152
if ( item )
153153
{
154+
list.reserve( 2 );
154155
for ( int i = 0; i < 2; ++i )
155156
{
156157
int transformNr = item->data( i, Qt::UserRole ).toInt();

src/gui/raster/qgsrastertransparencywidget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ void QgsRasterTransparencyWidget::apply()
406406
{
407407
QgsRasterTransparency::TransparentThreeValuePixel myTransparentPixel;
408408
QList<QgsRasterTransparency::TransparentThreeValuePixel> myTransparentThreeValuePixelList;
409+
myTransparentThreeValuePixelList.reserve( tableTransparency->rowCount() );
409410
for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
410411
{
411412
myTransparentPixel.red = transparencyCellValue( myListRunner, 0 );
@@ -420,6 +421,7 @@ void QgsRasterTransparencyWidget::apply()
420421
{
421422
QgsRasterTransparency::TransparentSingleValuePixel myTransparentPixel;
422423
QList<QgsRasterTransparency::TransparentSingleValuePixel> myTransparentSingleValuePixelList;
424+
myTransparentSingleValuePixelList.reserve( tableTransparency->rowCount() );
423425
for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
424426
{
425427
myTransparentPixel.min = transparencyCellValue( myListRunner, 0 );

src/gui/symbology-ng/qgssvgselectorwidget.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ QgsSvgSelectorGroupsModel::QgsSvgSelectorGroupsModel( QObject* parent )
306306
QStringList svgPaths = QgsApplication::svgPaths();
307307
QStandardItem *parentItem = invisibleRootItem();
308308
QStringList parentPaths;
309+
parentPaths.reserve( svgPaths.size() );
309310

310311
for ( int i = 0; i < svgPaths.size(); i++ )
311312
{
@@ -538,7 +539,7 @@ void QgsSvgSelectorWidget::populateList()
538539
//-- QgsSvgSelectorDialog
539540

540541
QgsSvgSelectorDialog::QgsSvgSelectorDialog( QWidget *parent, Qt::WindowFlags fl,
541-
const QDialogButtonBox::StandardButtons& buttons,
542+
QDialogButtonBox::StandardButtons buttons,
542543
Qt::Orientation orientation )
543544
: QDialog( parent, fl )
544545
{

src/providers/arcgisrest/qgsafsprovider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ bool QgsAfsProvider::getFeature( const QgsFeatureId &id, QgsFeature &f, bool fet
172172
// When fetching from server, fetch all attributes and geometry by default so that we can cache them
173173
QStringList fetchAttribNames;
174174
QList<int> fetchAttribIdx;
175+
fetchAttribIdx.reserve( mFields.size() );
175176
for ( int idx = 0, n = mFields.size(); idx < n; ++idx )
176177
{
177178
fetchAttribNames.append( mFields.at( idx ).name() );
@@ -183,6 +184,7 @@ bool QgsAfsProvider::getFeature( const QgsFeatureId &id, QgsFeature &f, bool fet
183184
int startId = ( id / 100 ) * 100;
184185
int stopId = qMin( startId + 100, mObjectIds.length() );
185186
QList<quint32> objectIds;
187+
objectIds.reserve( stopId );
186188
for ( int i = startId; i < stopId; ++i )
187189
{
188190
objectIds.append( mObjectIds[i] );

src/providers/db2/qgsdb2tablemodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProper
242242

243243
QList<QStandardItem *> row;
244244

245+
row.reserve( dbtmColumns );
245246
for ( int j = 0; j < dbtmColumns; j++ )
246247
{
247248
row << itemFromIndex( currentChildIndex.sibling( i, j ) );

src/providers/mssql/qgsmssqltablemodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ void QgsMssqlTableModel::setGeometryTypesForTable( QgsMssqlLayerProperty layerPr
241241
}
242242

243243
QList<QStandardItem *> row;
244+
row.reserve( dbtmColumns );
244245

245246
for ( int j = 0; j < dbtmColumns; j++ )
246247
{

src/providers/wcs/qgswcsprovider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ QgsWcsProvider::QgsWcsProvider( const QString& uri )
272272
// Get types
273273
// TODO: we are using the same data types like GDAL (not wider like GDAL provider)
274274
// with expectation to replace 'no data' values by NaN
275+
mSrcGdalDataType.reserve( mBandCount );
276+
mGdalDataType.reserve( mBandCount );
275277
for ( int i = 1; i <= mBandCount; i++ )
276278
{
277279
GDALRasterBandH gdalBand = GDALGetRasterBand( mCachedGdalDataset, i );

src/server/qgsserverprojectparser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QStr
4747
QDomNodeList layerNodeList = mXMLDoc->elementsByTagName( "maplayer" );
4848
QDomElement currentElement;
4949
int nNodes = layerNodeList.size();
50+
mProjectLayerElements.reserve( nNodes );
5051
for ( int i = 0; i < nNodes; ++i )
5152
{
5253
currentElement = layerNodeList.at( i ).toElement();
@@ -1470,6 +1471,7 @@ QList< QPair< QString, QgsLayerCoordinateTransform > > QgsServerProjectParser::l
14701471
}
14711472

14721473
QDomNodeList layerTransformNodeList = coordTransformInfoElem.elementsByTagName( "layer_coordinate_transform" );
1474+
layerTransformList.reserve( layerTransformNodeList.size() );
14731475
for ( int i = 0; i < layerTransformNodeList.size(); ++i )
14741476
{
14751477
QPair< QString, QgsLayerCoordinateTransform > layerEntry;

src/server/qgswmsprojectparser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ void QgsWmsProjectParser::addDrawingOrder( QDomElement& parentElem, QDomDocument
996996
if ( !layerList.isEmpty() )
997997
{
998998
QStringList reversedList;
999+
reversedList.reserve( layerList.size() );
9991000
for ( int i = layerList.size() - 1; i >= 0; --i )
10001001
reversedList << layerList[ i ];
10011002

0 commit comments

Comments
 (0)