Skip to content

Commit 92d368c

Browse files
committed
refactor zero-filling to use a traversal loop
1 parent ba1aa5e commit 92d368c

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

stringdtype/stringdtype/src/dtype.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,34 @@ stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
317317
}
318318

319319
static int
320-
stringdtype_fill_zero_value(PyArrayObject *arr)
320+
stringdtype_fill_zero_loop(void *NPY_UNUSED(traverse_context),
321+
PyArray_Descr *NPY_UNUSED(descr), char *data,
322+
npy_intp size, npy_intp stride,
323+
NpyAuxData *NPY_UNUSED(auxdata))
321324
{
322-
char *buf = PyArray_DATA(arr);
323-
npy_intp sz = PyArray_SIZE(arr);
324-
int elsize = PyArray_DESCR(arr)->elsize;
325-
326-
for (npy_intp i = 0; i < sz; i++) {
327-
if (ssnewlen("", 0, (ss *)(buf + i * elsize)) < 0) {
325+
while (size--) {
326+
if (ssnewlen("", 0, (ss *)(data)) < 0) {
328327
return -1;
329328
}
329+
data += stride;
330330
}
331331
return 0;
332332
}
333333

334+
static int
335+
stringdtype_get_fill_zero_loop(void *NPY_UNUSED(traverse_context),
336+
PyArray_Descr *NPY_UNUSED(descr),
337+
int NPY_UNUSED(aligned),
338+
npy_intp NPY_UNUSED(fixed_stride),
339+
traverse_loop_function **out_loop,
340+
NpyAuxData **NPY_UNUSED(out_auxdata),
341+
NPY_ARRAYMETHOD_FLAGS *flags)
342+
{
343+
*flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
344+
*out_loop = &stringdtype_fill_zero_loop;
345+
return 0;
346+
}
347+
334348
static PyType_Slot StringDType_Slots[] = {
335349
{NPY_DT_common_instance, &common_instance},
336350
{NPY_DT_common_dtype, &common_dtype},
@@ -344,7 +358,7 @@ static PyType_Slot StringDType_Slots[] = {
344358
{NPY_DT_PyArray_ArrFuncs_argmax, &argmax},
345359
{NPY_DT_PyArray_ArrFuncs_argmin, &argmin},
346360
{NPY_DT_get_clear_loop, &stringdtype_get_clear_loop},
347-
{NPY_DT_fill_zero_value, &stringdtype_fill_zero_value},
361+
{NPY_DT_get_fill_zero_loop, &stringdtype_get_fill_zero_loop},
348362
{0, NULL}};
349363

350364
static PyObject *

0 commit comments

Comments
 (0)