Added patch
This commit is contained in:
		@@ -0,0 +1,107 @@
 | 
			
		||||
From eb85684feb0505694e66365ba9f4d10a409f8f0b Mon Sep 17 00:00:00 2001
 | 
			
		||||
From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?=
 | 
			
		||||
 <34657725+jeremiedbb@users.noreply.github.com>
 | 
			
		||||
Date: Wed, 11 Oct 2023 11:43:47 +0200
 | 
			
		||||
Subject: [PATCH] MAINT Follow cython performance hints (#27521)
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 .../_middle_term_computer.pyx.tp                   |  2 +-
 | 
			
		||||
 .../preprocessing/_csr_polynomial_expansion.pyx    |  2 +-
 | 
			
		||||
 sklearn/tree/_splitter.pyx                         |  2 +-
 | 
			
		||||
 sklearn/tree/_utils.pxd                            |  2 +-
 | 
			
		||||
 sklearn/tree/_utils.pyx                            | 14 ++++++--------
 | 
			
		||||
 5 files changed, 10 insertions(+), 12 deletions(-)
 | 
			
		||||
 | 
			
		||||
diff --git a/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.tp b/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.tp
 | 
			
		||||
index f2d89ed65909c..bfe465394c4d2 100644
 | 
			
		||||
--- a/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.tp
 | 
			
		||||
+++ b/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.tp
 | 
			
		||||
@@ -82,7 +82,7 @@ cdef void _middle_term_sparse_dense_{{name_suffix}}(
 | 
			
		||||
     intp_t Y_end,
 | 
			
		||||
     bint c_ordered_middle_term,
 | 
			
		||||
     float64_t * dist_middle_terms,
 | 
			
		||||
-) nogil:
 | 
			
		||||
+) noexcept nogil:
 | 
			
		||||
     # This routine assumes that dist_middle_terms is a pointer to the first element
 | 
			
		||||
     # of a buffer filled with zeros of length at least equal to n_X × n_Y, conceptually
 | 
			
		||||
     # representing a 2-d C-ordered of F-ordered array.
 | 
			
		||||
diff --git a/sklearn/preprocessing/_csr_polynomial_expansion.pyx b/sklearn/preprocessing/_csr_polynomial_expansion.pyx
 | 
			
		||||
index 90f81c0399a6e..017af83f035b2 100644
 | 
			
		||||
--- a/sklearn/preprocessing/_csr_polynomial_expansion.pyx
 | 
			
		||||
+++ b/sklearn/preprocessing/_csr_polynomial_expansion.pyx
 | 
			
		||||
@@ -166,7 +166,7 @@ cpdef void _csr_polynomial_expansion(
 | 
			
		||||
     INDEX_B_t[:] result_indptr,     # OUT
 | 
			
		||||
     FLAG_t interaction_only,
 | 
			
		||||
     FLAG_t degree
 | 
			
		||||
-) nogil:
 | 
			
		||||
+):
 | 
			
		||||
     """
 | 
			
		||||
     Perform a second or third degree polynomial or interaction expansion on a
 | 
			
		||||
     compressed sparse row (CSR) matrix. The method used only takes products of
 | 
			
		||||
diff --git a/sklearn/tree/_splitter.pyx b/sklearn/tree/_splitter.pyx
 | 
			
		||||
index a9d3a169ec84a..81cd61ed22631 100644
 | 
			
		||||
--- a/sklearn/tree/_splitter.pyx
 | 
			
		||||
+++ b/sklearn/tree/_splitter.pyx
 | 
			
		||||
@@ -263,7 +263,7 @@ cdef inline void shift_missing_values_to_left_if_required(
 | 
			
		||||
     SplitRecord* best,
 | 
			
		||||
     intp_t[::1] samples,
 | 
			
		||||
     intp_t end,
 | 
			
		||||
-) nogil:
 | 
			
		||||
+) noexcept nogil:
 | 
			
		||||
     cdef intp_t i, p, current_end
 | 
			
		||||
     # The partitioner partitions the data such that the missing values are in
 | 
			
		||||
     # samples[-n_missing:] for the criterion to consume. If the missing values
 | 
			
		||||
diff --git a/sklearn/tree/_utils.pxd b/sklearn/tree/_utils.pxd
 | 
			
		||||
index 4167230bfbf4d..b59d18879ca94 100644
 | 
			
		||||
--- a/sklearn/tree/_utils.pxd
 | 
			
		||||
+++ b/sklearn/tree/_utils.pxd
 | 
			
		||||
@@ -39,7 +39,7 @@ ctypedef fused realloc_ptr:
 | 
			
		||||
     (Cell*)
 | 
			
		||||
     (Node**)
 | 
			
		||||
 
 | 
			
		||||
-cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil
 | 
			
		||||
+cdef int safe_realloc(realloc_ptr* p, size_t nelems) except -1 nogil
 | 
			
		||||
 
 | 
			
		||||
 
 | 
			
		||||
 cdef cnp.ndarray sizet_ptr_to_ndarray(intp_t* data, intp_t size)
 | 
			
		||||
diff --git a/sklearn/tree/_utils.pyx b/sklearn/tree/_utils.pyx
 | 
			
		||||
index 3c0c312b25fbe..b6115d4f21d18 100644
 | 
			
		||||
--- a/sklearn/tree/_utils.pyx
 | 
			
		||||
+++ b/sklearn/tree/_utils.pyx
 | 
			
		||||
@@ -22,22 +22,20 @@ from ..utils._random cimport our_rand_r
 | 
			
		||||
 # Helper functions
 | 
			
		||||
 # =============================================================================
 | 
			
		||||
 
 | 
			
		||||
-cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil:
 | 
			
		||||
+cdef int safe_realloc(realloc_ptr* p, size_t nelems) except -1 nogil:
 | 
			
		||||
     # sizeof(realloc_ptr[0]) would be more like idiomatic C, but causes Cython
 | 
			
		||||
     # 0.20.1 to crash.
 | 
			
		||||
     cdef size_t nbytes = nelems * sizeof(p[0][0])
 | 
			
		||||
     if nbytes / sizeof(p[0][0]) != nelems:
 | 
			
		||||
         # Overflow in the multiplication
 | 
			
		||||
-        with gil:
 | 
			
		||||
-            raise MemoryError("could not allocate (%d * %d) bytes"
 | 
			
		||||
-                              % (nelems, sizeof(p[0][0])))
 | 
			
		||||
+        raise MemoryError(f"could not allocate ({nelems} * {sizeof(p[0][0])}) bytes")
 | 
			
		||||
+
 | 
			
		||||
     cdef realloc_ptr tmp = <realloc_ptr>realloc(p[0], nbytes)
 | 
			
		||||
     if tmp == NULL:
 | 
			
		||||
-        with gil:
 | 
			
		||||
-            raise MemoryError("could not allocate %d bytes" % nbytes)
 | 
			
		||||
+        raise MemoryError(f"could not allocate {nbytes} bytes")
 | 
			
		||||
 
 | 
			
		||||
     p[0] = tmp
 | 
			
		||||
-    return tmp  # for convenience
 | 
			
		||||
+    return 0
 | 
			
		||||
 
 | 
			
		||||
 
 | 
			
		||||
 def _realloc_test():
 | 
			
		||||
@@ -111,7 +109,7 @@ cdef class WeightedPQueue:
 | 
			
		||||
         or 0 otherwise.
 | 
			
		||||
         """
 | 
			
		||||
         self.array_ptr = 0
 | 
			
		||||
-        # Since safe_realloc can raise MemoryError, use `except *`
 | 
			
		||||
+        # Since safe_realloc can raise MemoryError, use `except -1`
 | 
			
		||||
         safe_realloc(&self.array_, self.capacity)
 | 
			
		||||
         return 0
 | 
			
		||||
 
 | 
			
		||||
@@ -38,6 +38,10 @@ BDEPEND="
 | 
			
		||||
	dev-python/cython[${PYTHON_USEDEP}]
 | 
			
		||||
"
 | 
			
		||||
 | 
			
		||||
PATCHES=(
 | 
			
		||||
	"${FILESDIR}/eb85684feb0505694e66365ba9f4d10a409f8f0b.patch"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
distutils_enable_tests pytest
 | 
			
		||||
# For some reason this wants to use urllib to fetch things from the internet
 | 
			
		||||
# distutils_enable_sphinx doc \
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user