Skip to content

Commit 46d7a42

Browse files
authored
Merge pull request #6300 from nyalldawson/opts
Misc optimizations for everything
2 parents 2cbcf74 + 24fb28f commit 46d7a42

35 files changed

+181
-92
lines changed

python/core/geometry/qgsabstractgeometry.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,6 @@ Returns Java-style iterator for traversal of vertices of the geometry
596596
.. versionadded:: 3.0
597597
%End
598598

599-
protected:
600-
601599
virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 /Factory/;
602600
%Docstring
603601
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
@@ -612,6 +610,8 @@ To create it, the geometry is default constructed and then the WKB is changed.
612610
Not available in Python bindings
613611
%End
614612

613+
protected:
614+
615615
virtual bool hasChildGeometries() const;
616616
%Docstring
617617
Returns whether the geometry has any child geometries (false for point / curve, true otherwise)

python/core/geometry/qgscircularstring.sip.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ Sets the circular string's points
127127
virtual double yAt( int index ) const;
128128

129129

130-
protected:
131130
virtual QgsCircularString *createEmptyWithSameType() const /Factory/;
132131

132+
133+
protected:
134+
133135
virtual QgsRectangle calculateBoundingBox() const;
134136

135137

python/core/geometry/qgscompoundcurve.sip.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ Appends first point if not already closed.
160160
virtual double yAt( int index ) const;
161161

162162

163+
virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;
164+
165+
163166
protected:
164167

165168
virtual QgsRectangle calculateBoundingBox() const;
166169

167-
virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;
168-
169170

170171
};
171172

python/core/geometry/qgscurvepolygon.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,12 @@ Returns approximate rotation angle for a vertex. Usually average angle between a
198198

199199
virtual QgsCurvePolygon *toCurveType() const /Factory/;
200200

201-
protected:
201+
202202
virtual QgsCurvePolygon *createEmptyWithSameType() const /Factory/;
203203

204+
205+
protected:
206+
204207
virtual int childCount() const;
205208

206209
virtual QgsAbstractGeometry *childGeometry( int index ) const;

python/core/geometry/qgsgeometrycollection.sip.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ Returns a geometry without curves. Caller takes ownership
176176

177177

178178

179-
protected:
180179
virtual QgsGeometryCollection *createEmptyWithSameType() const /Factory/;
181180

181+
182+
protected:
182183
virtual int childCount() const;
183184

184185
virtual QgsAbstractGeometry *childGeometry( int index ) const;

python/core/geometry/qgslinestring.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,12 @@ of the curve.
290290
virtual bool convertTo( QgsWkbTypes::Type type );
291291

292292

293-
protected:
293+
294294
virtual QgsLineString *createEmptyWithSameType() const /Factory/;
295295

296+
297+
protected:
298+
296299
virtual QgsRectangle calculateBoundingBox() const;
297300

298301

python/core/geometry/qgsmulticurve.sip.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ Returns a copy of the multi curve, where each component curve has had its line d
5353

5454

5555

56-
protected:
57-
5856
virtual QgsMultiCurve *createEmptyWithSameType() const /Factory/;
5957

6058

59+
6160
};
6261

6362

python/core/geometry/qgsmultilinestring.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
4949
:return: the converted geometry. Caller takes ownership*
5050
%End
5151

52-
protected:
52+
5353
virtual QgsMultiLineString *createEmptyWithSameType() const /Factory/;
5454

55+
56+
protected:
57+
5558
virtual bool wktOmitChildType() const;
5659

5760
};

python/core/geometry/qgsmultipoint.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ class QgsMultiPoint: QgsGeometryCollection
5151
virtual double segmentLength( QgsVertexId startVertex ) const;
5252

5353

54-
protected:
54+
5555
virtual QgsMultiPoint *createEmptyWithSameType() const /Factory/;
5656

57+
58+
protected:
59+
5760
virtual bool wktOmitChildType() const;
5861

5962

python/core/geometry/qgsmultipolygon.sip.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
5151
virtual QgsAbstractGeometry *boundary() const /Factory/;
5252

5353

54-
protected:
5554
virtual QgsMultiPolygon *createEmptyWithSameType() const /Factory/;
5655

56+
57+
protected:
58+
5759
virtual bool wktOmitChildType() const;
5860

5961
};

python/core/geometry/qgsmultisurface.sip.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class QgsMultiSurface: QgsGeometryCollection
4545

4646

4747

48-
protected:
4948
virtual QgsMultiSurface *createEmptyWithSameType() const /Factory/;
5049

5150

python/core/geometry/qgspoint.sip.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,11 @@ Angle undefined. Always returns 0.0
419419

420420

421421

422-
protected:
423422
virtual QgsPoint *createEmptyWithSameType() const /Factory/;
424423

424+
425+
protected:
426+
425427
virtual int childCount() const;
426428

427429
virtual QgsPoint childPoint( int index ) const;

python/core/geometry/qgspolygon.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ negative if the point lies outside the polygon.
6262
%End
6363

6464

65-
protected:
66-
6765
virtual QgsPolygon *createEmptyWithSameType() const /Factory/;
6866

6967

68+
protected:
69+
7070

7171
};
7272
/************************************************************************

python/core/geometry/qgsrectangle.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ Examples are storing a layer extent or the current view extent of a map
2626
#include "qgsrectangle.h"
2727
%End
2828
public:
29-
QgsRectangle( double xMin = 0, double yMin = 0, double xMax = 0, double yMax = 0 );
29+
30+
QgsRectangle(); // optimised constructor for null rectangle - no need to call normalize here
31+
32+
explicit QgsRectangle( double xMin, double yMin = 0, double xMax = 0, double yMax = 0 );
3033
%Docstring
3134
Constructor
3235
%End

python/core/geometry/qgstriangle.sip.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,10 @@ Example:
425425
%End
426426

427427

428-
protected:
429-
430428
virtual QgsTriangle *createEmptyWithSameType() const /Factory/;
431429

432430

431+
433432
};
434433
/************************************************************************
435434
* This file has been generated automatically from *

src/core/geometry/qgsabstractgeometry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ class CORE_EXPORT QgsAbstractGeometry
617617
*/
618618
QgsVertexIterator vertices() const;
619619

620-
protected:
621-
622620
/**
623621
* Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
624622
* To create it, the geometry is default constructed and then the WKB is changed.
@@ -628,6 +626,8 @@ class CORE_EXPORT QgsAbstractGeometry
628626
*/
629627
virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 SIP_FACTORY;
630628

629+
protected:
630+
631631
/**
632632
* Returns whether the geometry has any child geometries (false for point / curve, true otherwise)
633633
* \note used for vertex_iterator implementation

src/core/geometry/qgscircularstring.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
113113
}
114114
#endif
115115

116-
protected:
117116
QgsCircularString *createEmptyWithSameType() const override SIP_FACTORY;
117+
118+
protected:
119+
118120
QgsRectangle calculateBoundingBox() const override;
119121

120122
private:

src/core/geometry/qgscompoundcurve.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
142142
}
143143
#endif
144144

145+
QgsCompoundCurve *createEmptyWithSameType() const override SIP_FACTORY;
146+
145147
protected:
146148

147149
QgsRectangle calculateBoundingBox() const override;
148-
QgsCompoundCurve *createEmptyWithSameType() const override SIP_FACTORY;
149150

150151
private:
151152
QVector< QgsCurve * > mCurves;

src/core/geometry/qgscurvepolygon.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
175175
return nullptr;
176176
}
177177
#endif
178-
protected:
178+
179179
QgsCurvePolygon *createEmptyWithSameType() const override SIP_FACTORY;
180+
181+
protected:
182+
180183
int childCount() const override;
181184
QgsAbstractGeometry *childGeometry( int index ) const override;
182185

src/core/geometry/qgsgeometry.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,20 @@ QgsGeometry QgsGeometry::fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly
223223

224224
QgsGeometry QgsGeometry::fromRect( const QgsRectangle &rect )
225225
{
226-
QgsPolylineXY ring;
227-
ring.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) );
228-
ring.append( QgsPointXY( rect.xMaximum(), rect.yMinimum() ) );
229-
ring.append( QgsPointXY( rect.xMaximum(), rect.yMaximum() ) );
230-
ring.append( QgsPointXY( rect.xMinimum(), rect.yMaximum() ) );
231-
ring.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) );
232-
233-
QgsPolygonXY polygon;
234-
polygon.append( ring );
235-
236-
return fromPolygonXY( polygon );
226+
std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString >(
227+
QVector< double >() << rect.xMinimum()
228+
<< rect.xMaximum()
229+
<< rect.xMaximum()
230+
<< rect.xMinimum()
231+
<< rect.xMinimum(),
232+
QVector< double >() << rect.yMinimum()
233+
<< rect.yMinimum()
234+
<< rect.yMaximum()
235+
<< rect.yMaximum()
236+
<< rect.yMinimum() );
237+
std::unique_ptr< QgsPolygon > polygon = qgis::make_unique< QgsPolygon >();
238+
polygon->setExteriorRing( ext.release() );
239+
return QgsGeometry( std::move( polygon ) );
237240
}
238241

239242
QgsGeometry QgsGeometry::collectGeometry( const QVector< QgsGeometry > &geometries )
@@ -1258,8 +1261,16 @@ bool QgsGeometry::convertToMultiType()
12581261
return false;
12591262
}
12601263

1261-
multiGeom->addGeometry( d->geometry->clone() );
1262-
reset( std::move( geom ) );
1264+
//try to avoid cloning existing geometry whenever we can
1265+
1266+
//want to see a magic trick?... gather round kiddies...
1267+
detach(); // maybe a clone, hopefully not if we're the only ref to the private data
1268+
// now we cheat a bit and steal the private geometry and add it direct to the multigeom
1269+
// we can do this because we're the only ref to this geometry, guaranteed by the detach call above
1270+
multiGeom->addGeometry( d->geometry.release() );
1271+
// and replace it with the multi geometry.
1272+
// TADA! a clone free conversion in some cases
1273+
d->geometry = std::move( geom );
12631274
return true;
12641275
}
12651276

src/core/geometry/qgsgeometrycollection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
159159
}
160160
#endif
161161

162-
protected:
163162
QgsGeometryCollection *createEmptyWithSameType() const override SIP_FACTORY;
163+
164+
protected:
164165
int childCount() const override;
165166
QgsAbstractGeometry *childGeometry( int index ) const override;
166167

src/core/geometry/qgslinestring.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,11 @@ class CORE_EXPORT QgsLineString: public QgsCurve
253253
return nullptr;
254254
}
255255
#endif
256-
protected:
256+
257257
QgsLineString *createEmptyWithSameType() const override SIP_FACTORY;
258+
259+
protected:
260+
258261
QgsRectangle calculateBoundingBox() const override;
259262

260263
private:

src/core/geometry/qgsmulticurve.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection
7171
}
7272
#endif
7373

74-
protected:
75-
7674
QgsMultiCurve *createEmptyWithSameType() const override SIP_FACTORY;
7775

76+
7877
};
7978

8079
// clazy:excludeall=qstring-allocations

src/core/geometry/qgsmultilinestring.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve
6262
return nullptr;
6363
}
6464
#endif
65-
protected:
65+
6666
QgsMultiLineString *createEmptyWithSameType() const override SIP_FACTORY;
67+
68+
protected:
69+
6770
bool wktOmitChildType() const override;
6871
};
6972

src/core/geometry/qgsmultipoint.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection
6262
return nullptr;
6363
}
6464
#endif
65-
protected:
65+
6666
QgsMultiPoint *createEmptyWithSameType() const override SIP_FACTORY;
67+
68+
protected:
69+
6770
bool wktOmitChildType() const override;
6871

6972
};

src/core/geometry/qgsmultipolygon.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class CORE_EXPORT QgsMultiPolygon: public QgsMultiSurface
6363
}
6464
#endif
6565

66-
protected:
6766
QgsMultiPolygon *createEmptyWithSameType() const override SIP_FACTORY;
67+
68+
protected:
69+
6870
bool wktOmitChildType() const override;
6971
};
7072

src/core/geometry/qgsmultisurface.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class CORE_EXPORT QgsMultiSurface: public QgsGeometryCollection
6565
}
6666
#endif
6767

68-
protected:
6968
QgsMultiSurface *createEmptyWithSameType() const override SIP_FACTORY;
7069

7170
};

0 commit comments

Comments
 (0)