From 8540f70f27ed59a1006d4c3c255be7532160e317 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 11 May 2020 03:25:41 -0700 Subject: [PATCH 1/2] Release the GIL while performing long processing. --- pyclipper/pyclipper.pyx | 117 +++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 44 deletions(-) diff --git a/pyclipper/pyclipper.pyx b/pyclipper/pyclipper.pyx index 7beea17..693fd5d 100644 --- a/pyclipper/pyclipper.pyx +++ b/pyclipper/pyclipper.pyx @@ -170,8 +170,8 @@ cdef extern from "clipper.hpp" namespace "ClipperLib": Clipper() #~Clipper() void Clear() - bool Execute(ClipType clipType, Paths & solution, PolyFillType subjFillType, PolyFillType clipFillType) - bool Execute(ClipType clipType, PolyTree & solution, PolyFillType subjFillType, PolyFillType clipFillType) + bool Execute(ClipType clipType, Paths & solution, PolyFillType subjFillType, PolyFillType clipFillType) nogil + bool Execute(ClipType clipType, PolyTree & solution, PolyFillType subjFillType, PolyFillType clipFillType) nogil bool ReverseSolution() void ReverseSolution(bool value) bool StrictlySimple() @@ -180,7 +180,7 @@ cdef extern from "clipper.hpp" namespace "ClipperLib": void PreserveCollinear(bool value) bool AddPath(Path & path, PolyType polyType, bool closed) bool AddPaths(Paths & paths, PolyType polyType, bool closed) - IntRect GetBounds() + IntRect GetBounds() nogil cdef cppclass ClipperOffset: ClipperOffset(double miterLimit, double roundPrecision) @@ -189,35 +189,35 @@ cdef extern from "clipper.hpp" namespace "ClipperLib": #~ClipperOffset() void AddPath(Path & path, JoinType joinType, EndType endType) void AddPaths(Paths & paths, JoinType joinType, EndType endType) - void Execute(Paths & solution, double delta) - void Execute(PolyTree & solution, double delta) + void Execute(Paths & solution, double delta) nogil + void Execute(PolyTree & solution, double delta) nogil void Clear() double MiterLimit double ArcTolerance # prefixes are added to original functions to prevent naming collisions - bool c_Orientation "Orientation"(const Path & poly) - double c_Area "Area"(const Path & poly) - int c_PointInPolygon "PointInPolygon"(const IntPoint & pt, const Path & path) + bool c_Orientation "Orientation"(const Path & poly) nogil + double c_Area "Area"(const Path & poly) nogil + int c_PointInPolygon "PointInPolygon"(const IntPoint & pt, const Path & path) nogil # In the following 4 functions default values for fillType and distance were removed # because it caused a bug in C++ code generated by Cython. # See Cython bug report: http://trac.cython.org/ticket/816 - void c_SimplifyPolygon "SimplifyPolygon"(const Path & in_poly, Paths & out_polys, PolyFillType fillType) - void c_SimplifyPolygons "SimplifyPolygons"(const Paths & in_polys, Paths & out_polys, PolyFillType fillType) - void c_CleanPolygon "CleanPolygon"(const Path& in_poly, Path& out_poly, double distance) - void c_CleanPolygons "CleanPolygons"(Paths& polys, double distance) + void c_SimplifyPolygon "SimplifyPolygon"(const Path & in_poly, Paths & out_polys, PolyFillType fillType) nogil + void c_SimplifyPolygons "SimplifyPolygons"(const Paths & in_polys, Paths & out_polys, PolyFillType fillType) nogil + void c_CleanPolygon "CleanPolygon"(const Path& in_poly, Path& out_poly, double distance) nogil + void c_CleanPolygons "CleanPolygons"(Paths& polys, double distance) nogil - void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed) - void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed) - void c_MinkowskiDiff "MinkowskiDiff"(const Path& poly1, const Path& poly2, Paths& solution) + void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed) nogil + void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed) nogil + void c_MinkowskiDiff "MinkowskiDiff"(const Path& poly1, const Path& poly2, Paths& solution) nogil void c_PolyTreeToPaths "PolyTreeToPaths"(const PolyTree& polytree, Paths& paths) void c_ClosedPathsFromPolyTree "ClosedPathsFromPolyTree"(const PolyTree& polytree, Paths& paths) void c_OpenPathsFromPolyTree "OpenPathsFromPolyTree"(PolyTree& polytree, Paths& paths) - void c_ReversePath "ReversePath"(Path& p) - void c_ReversePaths "ReversePaths"(Paths& p) + void c_ReversePath "ReversePath"(Path& p) nogil + void c_ReversePaths "ReversePaths"(Paths& p) nogil #============================= Enum mapping ================ @@ -276,7 +276,11 @@ def Orientation(poly): True -- counter-clockwise orientation False -- clockwise orientation """ - return c_Orientation(_to_clipper_path(poly)) + cdef Path c_path = _to_clipper_path(poly) + cdef bint result + with nogil: + result = c_Orientation(c_path) + return result def Area(poly): @@ -291,7 +295,11 @@ def Area(poly): Negative number if orientation is False """ - return c_Area(_to_clipper_path(poly)) + cdef Path c_path = _to_clipper_path(poly) + cdef double result + with nogil: + result = c_Area(c_path) + return result def PointInPolygon(point, poly): @@ -308,8 +316,12 @@ def PointInPolygon(point, poly): 1 -- point is in polygon """ - return c_PointInPolygon(_to_clipper_point(point), - _to_clipper_path(poly)) + cdef IntPoint c_point = _to_clipper_point(point) + cdef Path c_path = _to_clipper_path(poly) + cdef int result + with nogil: + result = c_PointInPolygon(c_point, c_path) + return result def SimplifyPolygon(poly, PolyFillType fill_type=pftEvenOdd): @@ -324,7 +336,9 @@ def SimplifyPolygon(poly, PolyFillType fill_type=pftEvenOdd): list of simplified polygons (containing one or more polygons) """ cdef Paths out_polys - c_SimplifyPolygon(_to_clipper_path(poly), out_polys, fill_type) + cdef Path c_path = _to_clipper_path(poly) + with nogil: + c_SimplifyPolygon(c_path, out_polys, fill_type) return _from_clipper_paths(out_polys) @@ -340,7 +354,9 @@ def SimplifyPolygons(polys, PolyFillType fill_type=pftEvenOdd): list of simplified polygons """ cdef Paths out_polys - c_SimplifyPolygons(_to_clipper_paths(polys), out_polys, fill_type) + cdef Paths c_polys = _to_clipper_paths(polys) + with nogil: + c_SimplifyPolygons(c_polys, out_polys, fill_type) return _from_clipper_paths(out_polys) @@ -356,7 +372,9 @@ def CleanPolygon(poly, double distance=1.415): cleaned polygon """ cdef Path out_poly - c_CleanPolygon(_to_clipper_path(poly), out_poly, distance) + cdef Path c_path = _to_clipper_path(poly) + with nogil: + c_CleanPolygon(c_path, out_poly, distance) return _from_clipper_path(out_poly) @@ -372,7 +390,8 @@ def CleanPolygons(polys, double distance=1.415): list of cleaned polygons """ cdef Paths out_polys = _to_clipper_paths(polys) - c_CleanPolygons(out_polys, distance) + with nogil: + c_CleanPolygons(out_polys, distance) return _from_clipper_paths(out_polys) @@ -389,11 +408,10 @@ def MinkowskiSum(pattern, path, bint path_is_closed): list of polygons (containing one or more polygons) """ cdef Paths solution - c_MinkowskiSum(_to_clipper_path(pattern), - _to_clipper_path(path), - solution, - path_is_closed - ) + cdef Path c_pat = _to_clipper_path(pattern) + cdef Path c_path = _to_clipper_path(path) + with nogil: + c_MinkowskiSum(c_pat, c_path, solution, path_is_closed) return _from_clipper_paths(solution) @@ -410,12 +428,10 @@ def MinkowskiSum2(pattern, paths, bint path_is_closed): list of polygons """ cdef Paths solution - c_MinkowskiSum( - _to_clipper_path(pattern), - _to_clipper_paths(paths), - solution, - path_is_closed - ) + cdef Path c_pat = _to_clipper_path(pattern) + cdef Paths c_paths = _to_clipper_paths(paths) + with nogil: + c_MinkowskiSum(c_pat, c_paths, solution, path_is_closed) return _from_clipper_paths(solution) @@ -431,7 +447,10 @@ def MinkowskiDiff(poly1, poly2): list of polygons """ cdef Paths solution - c_MinkowskiDiff(_to_clipper_path(poly1), _to_clipper_path(poly2), solution) + cdef Path c_path1 = _to_clipper_path(poly1) + cdef Path c_path2 = _to_clipper_path(poly2) + with nogil: + c_MinkowskiDiff(c_path1, c_path2, solution) return _from_clipper_paths(solution) @@ -495,7 +514,8 @@ def ReversePath(path): reversed path """ cdef Path c_path = _to_clipper_path(path) - c_ReversePath(c_path) + with nogil: + c_ReversePath(c_path) return _from_clipper_path(c_path) @@ -513,7 +533,8 @@ def ReversePaths(paths): list if reversed paths """ cdef Paths c_paths = _to_clipper_paths(paths) - c_ReversePaths(c_paths) + with nogil: + c_ReversePaths(c_paths) return _from_clipper_paths(c_paths) @@ -651,7 +672,9 @@ cdef class Pyclipper: """ _check_scaling_factor() - cdef IntRect rr = self.thisptr.GetBounds() + cdef IntRect rr + with nogil: + rr = self.thisptr.GetBounds() return PyIntRect(left=rr.left, top=rr.top, right=rr.right, bottom=rr.bottom) @@ -673,7 +696,9 @@ cdef class Pyclipper: """ cdef Paths solution - cdef object success = self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type) + cdef bint success + with nogil: + success = self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type) if not success: raise ClipperException('Execution of clipper did not succeed!') return _from_clipper_paths(solution) @@ -695,7 +720,9 @@ cdef class Pyclipper: ClipperException -- operation did not succeed """ cdef PolyTree solution - cdef object success = self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type) + cdef bint success + with nogil: + success = self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type) if not success: raise ClipperException('Execution of clipper did not succeed!') return _from_poly_tree(solution) @@ -787,7 +814,8 @@ cdef class PyclipperOffset: list of offset paths """ cdef Paths c_solution - self.thisptr.Execute(c_solution, delta) + with nogil: + self.thisptr.Execute(c_solution, delta) return _from_clipper_paths(c_solution) def Execute2(self, double delta): @@ -802,7 +830,8 @@ cdef class PyclipperOffset: PyPolyNode """ cdef PolyTree solution - self.thisptr.Execute(solution, delta) + with nogil: + self.thisptr.Execute(solution, delta) return _from_poly_tree(solution) def Clear(self): From b089ab9610cb3e4e0b5fedf6165f2211226d6b22 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Tue, 30 Jun 2020 23:41:51 -0700 Subject: [PATCH 2/2] Updated multibuild submodule to devel --- multibuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multibuild b/multibuild index 88a0b6f..0c2bd30 160000 --- a/multibuild +++ b/multibuild @@ -1 +1 @@ -Subproject commit 88a0b6f0eb770cf9f95792d66410e7696ce3d384 +Subproject commit 0c2bd30a67989c3a2386ffbbd4e77e9dc74458b4