@@ -84,37 +84,6 @@ searchsorted_descr_carrier(PyArray_Descr *descr)
8484}
8585
8686
87- /*
88- * The carrier is built once per call in `searchsorted_get_loop` rather than
89- * once per chunk in the loop, and travels to the loop as its auxiliary data.
90- */
91- typedef struct {
92- NpyAuxData base;
93- PyArrayObject *carrier;
94- } searchsorted_auxdata;
95-
96-
97- static void
98- searchsorted_auxdata_free (NpyAuxData *data)
99- {
100- Py_XDECREF (((searchsorted_auxdata *)data)->carrier );
101- PyMem_Free (data);
102- }
103-
104-
105- static NpyAuxData *
106- searchsorted_auxdata_clone (NpyAuxData *data)
107- {
108- searchsorted_auxdata *copy = PyMem_New (searchsorted_auxdata, 1 );
109- if (copy == NULL ) {
110- return NULL ;
111- }
112- *copy = *(searchsorted_auxdata *)data;
113- Py_XINCREF (copy->carrier );
114- return (NpyAuxData *)copy;
115- }
116-
117-
11887/*
11988 * `generic` selects the legacy comparison function, used by the dtypes
12089 * without a dedicated kernel. It needs a descriptor carrier because the
@@ -125,7 +94,7 @@ template <bool generic, NPY_SEARCHSIDE side>
12594static int
12695searchsorted_loop (PyArrayMethod_Context *context, char *const data[],
12796 npy_intp const dimensions[], npy_intp const strides[],
128- NpyAuxData *auxdata)
97+ NpyAuxData *NPY_UNUSED ( auxdata) )
12998{
13099 PyArray_Descr *descr = context->descriptors [0 ];
131100 PyArray_BinSearchFunc *bs = get_binsearch_func (descr, side);
@@ -137,8 +106,16 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
137106 PyArrayObject *carrier = NULL ;
138107 int needs_api = 0 ;
139108 if constexpr (generic) {
140- carrier = ((searchsorted_auxdata *)auxdata)->carrier ;
109+ /* Bytes and unicode search with the GIL released, so take it just
110+ * to build the carrier. */
141111 needs_api = PyDataType_FLAGCHK (descr, NPY_NEEDS_PYAPI );
112+ NPY_ALLOW_C_API_DEF ;
113+ NPY_ALLOW_C_API ;
114+ carrier = searchsorted_descr_carrier (descr);
115+ NPY_DISABLE_C_API ;
116+ if (carrier == NULL ) {
117+ return -1 ;
118+ }
142119 }
143120
144121 npy_intp n_outer = dimensions[0 ];
@@ -166,6 +143,12 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
166143 }
167144 }
168145 searchsorted_restore_floatstatus ((char *)&n_outer, saved);
146+ if constexpr (generic) {
147+ NPY_ALLOW_C_API_DEF ;
148+ NPY_ALLOW_C_API ;
149+ Py_DECREF (carrier);
150+ NPY_DISABLE_C_API ;
151+ }
169152 return ret;
170153}
171154
@@ -174,7 +157,7 @@ template <bool generic, NPY_SEARCHSIDE side>
174157static int
175158searchsorted_sorter_loop (PyArrayMethod_Context *context, char *const data[],
176159 npy_intp const dimensions[], npy_intp const strides[],
177- NpyAuxData *auxdata)
160+ NpyAuxData *NPY_UNUSED ( auxdata) )
178161{
179162 PyArray_Descr *descr = context->descriptors [0 ];
180163 PyArray_ArgBinSearchFunc *bs = get_argbinsearch_func (descr, side);
@@ -186,8 +169,16 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
186169 PyArrayObject *carrier = NULL ;
187170 int needs_api = 0 ;
188171 if constexpr (generic) {
189- carrier = ((searchsorted_auxdata *)auxdata)->carrier ;
172+ /* Bytes and unicode search with the GIL released, so take it just
173+ * to build the carrier. */
190174 needs_api = PyDataType_FLAGCHK (descr, NPY_NEEDS_PYAPI );
175+ NPY_ALLOW_C_API_DEF ;
176+ NPY_ALLOW_C_API ;
177+ carrier = searchsorted_descr_carrier (descr);
178+ NPY_DISABLE_C_API ;
179+ if (carrier == NULL ) {
180+ return -1 ;
181+ }
191182 }
192183
193184 npy_intp n_outer = dimensions[0 ];
@@ -224,6 +215,12 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
224215 }
225216 }
226217 searchsorted_restore_floatstatus ((char *)&n_outer, saved);
218+ if constexpr (generic) {
219+ NPY_ALLOW_C_API_DEF ;
220+ NPY_ALLOW_C_API ;
221+ Py_DECREF (carrier);
222+ NPY_DISABLE_C_API ;
223+ }
227224 return ret;
228225}
229226
@@ -263,46 +260,13 @@ searchsorted_resolve_descriptors(
263260}
264261
265262
266- /*
267- * Build the descriptor carrier the legacy comparison needs, and release the
268- * GIL for the dtypes whose comparison cannot reach Python. This is the test
269- * PyArray_SearchSorted makes through NPY_BEGIN_THREADS_DESCR. VOID_compare
270- * calls into Python even with no object fields, so the dtype flag rather
271- * than a reference check decides it, and it depends on the descriptor rather
272- * than the DType, which is why it happens here.
273- */
274- static int
275- searchsorted_get_loop (PyArrayMethod_Context *context, int aligned,
276- int move_references, const npy_intp *strides,
277- PyArrayMethod_StridedLoop **out_loop,
278- NpyAuxData **out_transferdata,
279- NPY_ARRAYMETHOD_FLAGS *flags)
263+ template <bool generic, NPY_SEARCHSIDE side>
264+ static void *
265+ searchsorted_loop_for (int with_sorter)
280266{
281- if (npy_default_get_strided_loop (context, aligned, move_references,
282- strides, out_loop, out_transferdata,
283- flags) < 0 ) {
284- return -1 ;
285- }
286- searchsorted_auxdata *auxdata = PyMem_New (searchsorted_auxdata, 1 );
287- if (auxdata == NULL ) {
288- PyErr_NoMemory ();
289- return -1 ;
290- }
291- memset (auxdata, 0 , sizeof (*auxdata));
292- auxdata->base .free = &searchsorted_auxdata_free;
293- auxdata->base .clone = &searchsorted_auxdata_clone;
294-
295- PyArray_Descr *descr = context->descriptors [0 ];
296- auxdata->carrier = searchsorted_descr_carrier (descr);
297- if (auxdata->carrier == NULL ) {
298- PyMem_Free (auxdata);
299- return -1 ;
300- }
301- *out_transferdata = (NpyAuxData *)auxdata;
302- if (!PyDataType_FLAGCHK (descr, NPY_NEEDS_PYAPI )) {
303- *flags = (NPY_ARRAYMETHOD_FLAGS )(*flags & ~NPY_METH_REQUIRES_PYAPI );
304- }
305- return 0 ;
267+ return (void *)(with_sorter
268+ ? searchsorted_sorter_loop<generic, side>
269+ : searchsorted_loop<generic, side>);
306270}
307271
308272
@@ -323,43 +287,22 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
323287 PyArray_DTypeMeta *dtypes[4 ] = {dt, dt, intp_dt, intp_dt};
324288
325289 void *loop;
326- if (side == NPY_SEARCHLEFT ) {
327- if (generic) {
328- loop = (void *)(with_sorter
329- ? searchsorted_sorter_loop<true , NPY_SEARCHLEFT >
330- : searchsorted_loop<true , NPY_SEARCHLEFT >);
331- }
332- else {
333- loop = (void *)(with_sorter
334- ? searchsorted_sorter_loop<false , NPY_SEARCHLEFT >
335- : searchsorted_loop<false , NPY_SEARCHLEFT >);
336- }
290+ if (generic) {
291+ loop = side == NPY_SEARCHLEFT
292+ ? searchsorted_loop_for<true , NPY_SEARCHLEFT >(with_sorter)
293+ : searchsorted_loop_for<true , NPY_SEARCHRIGHT >(with_sorter);
337294 }
338295 else {
339- if (generic) {
340- loop = (void *)(with_sorter
341- ? searchsorted_sorter_loop<true , NPY_SEARCHRIGHT >
342- : searchsorted_loop<true , NPY_SEARCHRIGHT >);
343- }
344- else {
345- loop = (void *)(with_sorter
346- ? searchsorted_sorter_loop<false , NPY_SEARCHRIGHT >
347- : searchsorted_loop<false , NPY_SEARCHRIGHT >);
348- }
296+ loop = side == NPY_SEARCHLEFT
297+ ? searchsorted_loop_for<false , NPY_SEARCHLEFT >(with_sorter)
298+ : searchsorted_loop_for<false , NPY_SEARCHRIGHT >(with_sorter);
349299 }
350300 PyType_Slot slots[] = {
351301 {NPY_METH_strided_loop, loop},
352302 {NPY_METH_resolve_descriptors,
353303 (void *)&searchsorted_resolve_descriptors},
354- {0 , NULL },
355304 {0 , NULL }
356305 };
357- if (generic) {
358- /* Only the legacy comparison needs a carrier and, for some dtypes,
359- * the GIL. */
360- slots[2 ].slot = NPY_METH_get_loop;
361- slots[2 ].pfunc = (void *)&searchsorted_get_loop;
362- }
363306
364307 PyArrayMethod_Spec spec = {};
365308 spec.name = with_sorter ? " searchsorted_sorter_loop" : " searchsorted_loop" ;
@@ -369,9 +312,14 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
369312 spec.casting = NPY_SAFE_CASTING ;
370313 /* Discarded by the gufunc machinery, see searchsorted_restore_floatstatus */
371314 spec.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS ;
372- if (generic) {
373- /* Cleared again in searchsorted_get_loop for the dtypes whose
374- * comparison cannot call into Python. */
315+ /*
316+ * Only the legacy comparisons that reach Python need the GIL: object
317+ * always does, and VOID_compare does even without object fields. Bytes
318+ * and unicode compare their raw buffers, so they search with the GIL
319+ * released as PyArray_SearchSorted does.
320+ */
321+ if (generic && dt->type_num != NPY_STRING
322+ && dt->type_num != NPY_UNICODE ) {
375323 spec.flags = (NPY_ARRAYMETHOD_FLAGS )(spec.flags
376324 | NPY_METH_REQUIRES_PYAPI );
377325 }
0 commit comments