GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: ArrayMethod sorting for built-in dtypes by MaanasArora · Pull Request #31278 · numpy/numpy

ENH: ArrayMethod sorting for built-in dtypes #31278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MaanasArora wants to merge 5 commits into numpy:main from MaanasArora:builtin-sort-array-methods

Uh oh!

There was an error while loading. Please reload this page.

Closed

ENH: ArrayMethod sorting for built-in dtypes #31278

Changes from 2 commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
f4bbfc2
ENH: ArrayMethod sorting for built-in dtypes
MaanasArora Apr 20, 2026
bec707a
REF: Remove unused template variable
MaanasArora Apr 20, 2026
64e7d0a
REF: Simplify output descriptor in sort_resolve_descriptors
MaanasArora Apr 20, 2026
45b558f
BUG: Move builtin sort registrations after array method types are ready
MaanasArora Apr 21, 2026
66c183e
REF: Remove set_sorts function back to set_typeinfo, move ready array…
MaanasArora Apr 21, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

171 changes: 171 additions & 0 deletions 171 numpy/_core/src/multiarray/arraytypes.c.src
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4509,6 +4509,111 @@ static int
}
/**end repeat**/

/*
*****************************************************************************
** SORTING ARRAYMETHODS **
*****************************************************************************
*/

static NPY_CASTING
sort_resolve_descriptors(
PyArrayMethodObject *method,
PyArray_DTypeMeta *const *dtypes,
PyArray_Descr *const *input_descrs,
PyArray_Descr **output_descrs,
npy_intp *view_offset)
{
output_descrs[0] = NPY_DT_CALL_ensure_canonical(input_descrs[0]);
if (NPY_UNLIKELY(output_descrs[0] == NULL)) {
return -1;
}
output_descrs[1] = NPY_DT_CALL_ensure_canonical(input_descrs[1]);
if (NPY_UNLIKELY(output_descrs[1] == NULL)) {
Py_XDECREF(output_descrs[0]);
return -1;
}
Comment thread
seberg marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


return method->casting;
}

static NPY_CASTING
argsort_resolve_descriptors(
PyArrayMethodObject *method,
PyArray_DTypeMeta *const *dtypes,
PyArray_Descr *const *input_descrs,
PyArray_Descr **output_descrs,
npy_intp *view_offset)
{
output_descrs[0] = NPY_DT_CALL_ensure_canonical(input_descrs[0]);
if (NPY_UNLIKELY(output_descrs[0] == NULL)) {
return -1;
}
output_descrs[1] = PyArray_DescrFromType(NPY_INTP);
if (NPY_UNLIKELY(output_descrs[1] == NULL)) {
Py_XDECREF(output_descrs[0]);
return -1;
}

return method->casting;
}
Comment thread

@seberg seberg Apr 20, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I guess we these them for timedelta64/datetime64 unfortunately, because it is parametric? (Just too bad, since this is all very simple in principle...)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MaanasArora MaanasArora Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate indeed, yes, it's because they're parametric. Given how often we're running into this, probably good to fix the issue somehow...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

/**begin repeat
*
* #suff = bool,
* byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* half, float, double, longdouble,
* cfloat, cdouble, clongdouble,
* datetime, timedelta#
* #rsort = 1*5, 0*15#
*/

static int
@suff@_sort_loop(PyArrayMethod_Context *context,
char *const data[], npy_intp const dimensions[],
npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata))
{
PyArrayMethod_SortParameters *params = (PyArrayMethod_SortParameters *)context->parameters;
switch (params->flags) {
case NPY_SORT_DEFAULT:
return quicksort_@suff@(data[0], dimensions[0], NULL);
case _NPY_SORT_HEAPSORT:
case NPY_SORT_STABLE:
#if @rsort@
return radixsort_@suff@(data[0], dimensions[0], NULL);
#else
return timsort_@suff@(data[0], dimensions[0], NULL);
#endif
default:
PyErr_Format(PyExc_RuntimeError,
Comment thread

@mhvk mhvk Apr 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it was possible to hit this RuntimeError -- but ideally from python. But that does seem tricky; having checked with print statements really is good enough!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Apr 21, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure we can reach it easily. Although, once we follow up with reverse sorting, I think we'll have our proof at least?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MaanasArora MaanasArora Apr 21, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can reach it by setting descending=True right? That should hit this?

Edit: sorry, I didn't recall we don't have it in Python yet for some reason! Yeah, that could be easier to test... no kwarg seems to hit this path right now in Python.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
"unknown sort kind %d for @suff@", (int)params->flags);
return -1;
}
}
static int
@suff@_argsort_loop(PyArrayMethod_Context *context,
char *const data[], npy_intp const dimensions[],
npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata))
{
PyArrayMethod_SortParameters *params = (PyArrayMethod_SortParameters *)context->parameters;
switch (params->flags) {
case NPY_SORT_DEFAULT:
return aquicksort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL);
case _NPY_SORT_HEAPSORT:
case NPY_SORT_STABLE:
#if @rsort@
return aradixsort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL);
Comment thread
seberg marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

#else
return atimsort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL);
#endif
default:
PyErr_Format(PyExc_RuntimeError,
"unknown sort kind %d for @suff@", (int)params->flags);
return -1;
}
}

/**end repeat**/

/*
*****************************************************************************
Expand Down Expand Up @@ -4600,6 +4705,72 @@ set_typeinfo(PyObject *dict)

initialize_legacy_dtypemeta_aliases(_builtin_descrs);

/*
* Add sorting array methods for the new types.
*/

/**begin repeat
* #name = bool,
* byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* half, float, double, longdouble,
* cfloat, cdouble, clongdouble,
* datetime, timedelta#
* #NAME = BOOL,
* BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG,
* HALF, FLOAT, DOUBLE, LONGDOUBLE,
* CFLOAT, CDOUBLE, CLONGDOUBLE,
* DATETIME, TIMEDELTA#
*/
dtypemeta = NPY_DTYPE(_builtin_descrs[NPY_@NAME@]);

PyArray_DTypeMeta *sort_dtypes_@name@[2] = {dtypemeta, dtypemeta};
PyType_Slot sort_slots_@name@[3] = {
{NPY_METH_resolve_descriptors, sort_resolve_descriptors},
{NPY_METH_strided_loop, @name@_sort_loop},
{0, NULL}
};
PyArrayMethod_Spec sort_spec_@name@ = {
.name = "@name@_sort",
.nin = 1,
.nout = 1,
.dtypes = sort_dtypes_@name@,
.slots = sort_slots_@name@,
.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS,
};
PyBoundArrayMethodObject *sort_method_@name@ = PyArrayMethod_FromSpec_int(
&sort_spec_@name@, 1);
if (sort_method_@name@ == NULL) {
return -1;
}
NPY_DT_SLOTS(dtypemeta)->sort_meth = sort_method_@name@->method;
Py_INCREF(sort_method_@name@->method);
Comment thread

@MaanasArora MaanasArora Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, decrefing sort_method_@name@ causes a segfault, but this is leaking otherwise...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Apr 20, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather cryptic, I agree. The problem is that:

    if (PyType_Ready(&PyArrayMethod_Type) < 0) {
        return -1;
    }
    if (PyType_Ready(&PyBoundArrayMethod_Type) < 0) {
        return -1;
    }

needs to be called first. Remind me: object and void already use this due to being the default fallback path, I guess?
(But I agree that they are different enough, either way.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MaanasArora MaanasArora Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think so actually! object and void have a comparison function that is set here:

    (PyArray_CompareFunc*)@from@_compare,

So, they are using the old path. That said it shouldn't at all be hard to use npy_default_sort_loop to take in a new comparison function for these.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Apr 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if those use @from@_compare, can we then not do the same in #30415 (we discussed there somewhat at #30415 (review))

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Apr 21, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we could add that slot, but in the end, I suppose either way we'll be using a hard-coded path for those. (We'll probably just want to change the signature of the internal sort function to re-align, but it is hard-coded either way.)
One could add a "uses compare slot" auto-fill, but I am not sure it is worth it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MaanasArora MaanasArora Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, probably will hard-code those, so unclear whether filling the new slot with legacy sort is worth the tradeoff.

So for the DECREF... I guess this entire registration needed to be moved, so I added a function and called that after that block instead of within set_typeinfo, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@seberg seberg Apr 21, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should work yeah. If it is nicer, you can also just move the finalization of ArrayMethod_Type and BoundArrayMethod_Type to happen first, they don't have any dependencies and can happen much earlier.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MaanasArora MaanasArora Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, thanks! That does look nicer (less verbose), pushed!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

PyArray_DTypeMeta *argsort_dtypes_@name@[2] = {dtypemeta, &PyArray_IntpDType};
PyType_Slot argsort_slots_@name@[3] = {
{NPY_METH_resolve_descriptors, argsort_resolve_descriptors},
{NPY_METH_strided_loop, @name@_argsort_loop},
{0, NULL}
};
PyArrayMethod_Spec argsort_spec_@name@ = {
.name = "@name@_argsort",
.nin = 1,
.nout = 1,
.dtypes = argsort_dtypes_@name@,
.slots = argsort_slots_@name@,
.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS,
};
PyBoundArrayMethodObject *argsort_method_@name@ = PyArrayMethod_FromSpec_int(
&argsort_spec_@name@, 1);
if (argsort_method_@name@ == NULL) {
return -1;
}
NPY_DT_SLOTS(dtypemeta)->argsort_meth = argsort_method_@name@->method;
Py_INCREF(argsort_method_@name@->method);

/**end repeat**/

/*
* Add cast functions for the new types
*/
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.