Skip to content

Commit

Permalink
Tag some inexpensive, frequently called methods with the HoldGIL anno…
Browse files Browse the repository at this point in the history
…tation

This prevents the Python GIL from being released before calling the method
(which is the default behaviour). For very cheap to call c++ methods the
cost of releasing the GIL can outweigh the cost of the c++ call, which means
it's more efficient to retain the hold on the GIL.

Ideally we'd do this everywhere, and switch to an explicit ReleaseGIL
annotation on functions which are slow or risky (raise exceptions, or
do something which can cause a GIL deadlock). But those are very tricky
to identify, so instead just explicitly hold the gil on cheap methods
which are likely to be called many times and could have an impact on
script performance.
  • Loading branch information
nyalldawson committed Sep 17, 2020
1 parent cc70b9f commit d4a2ddd
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 148 deletions.
36 changes: 18 additions & 18 deletions python/core/auto_generated/geometry/qgsrectangle.sip.in
Expand Up @@ -29,22 +29,22 @@ Examples are storing a layer extent or the current view extent of a map

QgsRectangle(); // optimised constructor for null rectangle - no need to call normalize here

explicit QgsRectangle( double xMin, double yMin = 0, double xMax = 0, double yMax = 0 );
explicit QgsRectangle( double xMin, double yMin = 0, double xMax = 0, double yMax = 0 ) /HoldGIL/;
%Docstring
Constructor
%End

QgsRectangle( const QgsPointXY &p1, const QgsPointXY &p2 );
QgsRectangle( const QgsPointXY &p1, const QgsPointXY &p2 ) /HoldGIL/;
%Docstring
Construct a rectangle from two points. The rectangle is normalized after construction.
%End

QgsRectangle( const QRectF &qRectF );
QgsRectangle( const QRectF &qRectF ) /HoldGIL/;
%Docstring
Construct a rectangle from a QRectF. The rectangle is normalized after construction.
%End

QgsRectangle( const QgsRectangle &other );
QgsRectangle( const QgsRectangle &other ) /HoldGIL/;
%Docstring
Copy constructor
%End
Expand Down Expand Up @@ -79,48 +79,48 @@ Sets the rectangle from four points. The rectangle is
normalised after construction.
%End

void setXMinimum( double x );
void setXMinimum( double x ) /HoldGIL/;
%Docstring
Set the minimum x value.
%End

void setXMaximum( double x );
void setXMaximum( double x ) /HoldGIL/;
%Docstring
Set the maximum x value.
%End

void setYMinimum( double y );
void setYMinimum( double y ) /HoldGIL/;
%Docstring
Set the minimum y value.
%End

void setYMaximum( double y );
void setYMaximum( double y ) /HoldGIL/;
%Docstring
Set the maximum y value.
%End

void setMinimal();
void setMinimal() /HoldGIL/;
%Docstring
Set a rectangle so that min corner is at max
and max corner is at min. It is NOT normalized.
%End

double xMaximum() const;
double xMaximum() const /HoldGIL/;
%Docstring
Returns the x maximum value (right side of rectangle).
%End

double xMinimum() const;
double xMinimum() const /HoldGIL/;
%Docstring
Returns the x minimum value (left side of rectangle).
%End

double yMaximum() const;
double yMaximum() const /HoldGIL/;
%Docstring
Returns the y maximum value (top side of rectangle).
%End

double yMinimum() const;
double yMinimum() const /HoldGIL/;
%Docstring
Returns the y minimum value (bottom side of rectangle).
%End
Expand All @@ -130,7 +130,7 @@ Returns the y minimum value (bottom side of rectangle).
Normalize the rectangle so it has non-negative width/height.
%End

double width() const;
double width() const /HoldGIL/;
%Docstring
Returns the width of the rectangle.

Expand All @@ -139,7 +139,7 @@ Returns the width of the rectangle.
.. seealso:: :py:func:`area`
%End

double height() const;
double height() const /HoldGIL/;
%Docstring
Returns the height of the rectangle.

Expand All @@ -148,7 +148,7 @@ Returns the height of the rectangle.
.. seealso:: :py:func:`area`
%End

double area() const;
double area() const /HoldGIL/;
%Docstring
Returns the area of the rectangle.

Expand All @@ -161,7 +161,7 @@ Returns the area of the rectangle.
.. versionadded:: 3.0
%End

double perimeter() const;
double perimeter() const /HoldGIL/;
%Docstring
Returns the perimeter of the rectangle.

Expand All @@ -170,7 +170,7 @@ Returns the perimeter of the rectangle.
.. versionadded:: 3.0
%End

QgsPointXY center() const;
QgsPointXY center() const /HoldGIL/;
%Docstring
Returns the center point of the rectangle.
%End
Expand Down
44 changes: 22 additions & 22 deletions python/core/auto_generated/qgspointxy.sip.in
Expand Up @@ -33,20 +33,20 @@ supports Z and M values.
public:
QgsPointXY();

QgsPointXY( const QgsPointXY &p );
QgsPointXY( const QgsPointXY &p ) /HoldGIL/;
%Docstring
Create a point from another point
%End

QgsPointXY( double x, double y );
QgsPointXY( double x, double y ) /HoldGIL/;
%Docstring
Create a point from x,y coordinates

:param x: x coordinate
:param y: y coordinate
%End

QgsPointXY( QPointF point );
QgsPointXY( QPointF point ) /HoldGIL/;
%Docstring
Create a point from a QPointF

Expand All @@ -55,7 +55,7 @@ Create a point from a QPointF
.. versionadded:: 2.7
%End

QgsPointXY( QPoint point );
QgsPointXY( QPoint point ) /HoldGIL/;
%Docstring
Create a point from a QPoint

Expand All @@ -64,7 +64,7 @@ Create a point from a QPoint
.. versionadded:: 2.7
%End

QgsPointXY( const QgsPoint &point );
QgsPointXY( const QgsPoint &point ) /HoldGIL/;
%Docstring
Create a new point.
Z and M values will be dropped.
Expand All @@ -74,33 +74,33 @@ Z and M values will be dropped.

~QgsPointXY();

void setX( double x );
void setX( double x ) /HoldGIL/;
%Docstring
Sets the x value of the point

:param x: x coordinate
%End

void setY( double y );
void setY( double y ) /HoldGIL/;
%Docstring
Sets the y value of the point

:param y: y coordinate
%End

void set( double x, double y );
void set( double x, double y ) /HoldGIL/;
%Docstring
Sets the x and y value of the point
%End

double x() const;
double x() const /HoldGIL/;
%Docstring
Gets the x value of the point

:return: x coordinate
%End

double y() const;
double y() const /HoldGIL/;
%Docstring
Gets the y value of the point

Expand Down Expand Up @@ -128,21 +128,21 @@ Returns the well known text representation for the point (e.g. "POINT(x y)").
The wkt is created without an SRID.
%End

double sqrDist( double x, double y ) const;
double sqrDist( double x, double y ) const /HoldGIL/;
%Docstring
Returns the squared distance between this point a specified x, y coordinate.

.. seealso:: :py:func:`distance`
%End

double sqrDist( const QgsPointXY &other ) const;
double sqrDist( const QgsPointXY &other ) const /HoldGIL/;
%Docstring
Returns the squared distance between this point another point.

.. seealso:: :py:func:`distance`
%End

double distance( double x, double y ) const;
double distance( double x, double y ) const /HoldGIL/;
%Docstring
Returns the distance between this point and a specified x, y coordinate.

Expand All @@ -154,7 +154,7 @@ Returns the distance between this point and a specified x, y coordinate.
.. versionadded:: 2.16
%End

double distance( const QgsPointXY &other ) const;
double distance( const QgsPointXY &other ) const /HoldGIL/;
%Docstring
Returns the distance between this point and another point.

Expand All @@ -165,17 +165,17 @@ Returns the distance between this point and another point.
.. versionadded:: 2.16
%End

double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint /Out/, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint /Out/, double epsilon = DEFAULT_SEGMENT_EPSILON ) const /HoldGIL/;
%Docstring
Returns the minimum distance between this point and a segment
%End

double azimuth( const QgsPointXY &other ) const;
double azimuth( const QgsPointXY &other ) const /HoldGIL/;
%Docstring
Calculates azimuth between this point and other one (clockwise in degree, starting from north)
%End

QgsPointXY project( double distance, double bearing ) const;
QgsPointXY project( double distance, double bearing ) const /HoldGIL/;
%Docstring
Returns a new point which corresponds to this point projected by a specified distance
in a specified bearing.
Expand All @@ -186,7 +186,7 @@ in a specified bearing.
.. versionadded:: 2.16
%End

bool isEmpty() const;
bool isEmpty() const /HoldGIL/;
%Docstring
Returns ``True`` if the geometry is empty.
Unlike :py:class:`QgsPoint`, this class is also used to retrieve graphical coordinates like QPointF.
Expand All @@ -196,7 +196,7 @@ A QgsPointXY is considered empty, when the coordinates have not been explicitly
.. versionadded:: 3.10
%End

bool compare( const QgsPointXY &other, double epsilon = 4 * DBL_EPSILON ) const;
bool compare( const QgsPointXY &other, double epsilon = 4 * DBL_EPSILON ) const /HoldGIL/;
%Docstring
Compares this point with another point with a fuzzy tolerance

Expand All @@ -208,11 +208,11 @@ Compares this point with another point with a fuzzy tolerance
.. versionadded:: 2.9
%End

bool operator==( const QgsPointXY &other );
bool operator==( const QgsPointXY &other ) /HoldGIL/;

bool operator!=( const QgsPointXY &other ) const;
bool operator!=( const QgsPointXY &other ) const /HoldGIL/;

void multiply( double scalar );
void multiply( double scalar ) /HoldGIL/;
%Docstring
Multiply x and y by the given value
%End
Expand Down

0 comments on commit d4a2ddd

Please sign in to comment.