Skip to content

Commit db12f00

Browse files
committed
Avoid cloning in QgsGeometry::convertToMultiType where possible
Refs #17809
1 parent c7e257e commit db12f00

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/core/geometry/qgsgeometry.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,16 @@ bool QgsGeometry::convertToMultiType()
12581258
return false;
12591259
}
12601260

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

0 commit comments

Comments
 (0)