[MRG + 1] FIX gil acquisition in dist_metric
@@ -1093,11 +1093,19 @@ cdef class PyFuncDistance(DistanceMetric):
self.func = func
self.kwargs = kwargs
+ # in cython < 0.26, GIL was required to be acquired during definition of
+ # the function and inside the body of the function. This behaviour is not
+ # allowed in cython >= 0.26 since it is a redundant GIL acquisition. The
+ # only way to be back compatible is to inherit `dist` from the base class
+ # without GIL and called an inline `_dist` which acquire GIL.
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2,
+ ITYPE_t size) nogil except -1:
+ return self._dist(x1, x2, size)
+
+ cdef inline DTYPE_t _dist(self, DTYPE_t* x1, DTYPE_t* x2,
ITYPE_t size) except -1 with gil:
cdef np.ndarray x1arr
cdef np.ndarray x2arr
- with gil:
x1arr = _buffer_to_ndarray(x1, size)
x2arr = _buffer_to_ndarray(x2, size)
d = self.func(x1arr, x2arr, **self.kwargs)
@@ -1110,6 +1118,5 @@ cdef class PyFuncDistance(DistanceMetric):
"vectors and return a float.")
-
cdef inline double fmax(double a, double b) nogil:
return max(a, b)
