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 — PERF: avoid an unnecessary copy in np.take with out= by Ijtihed · Pull Request #31940 · numpy/numpy

Conversation

@Ijtihed

@Ijtihed Ijtihed commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

PR summary

Fixes #28636.

np.take(a, indices, out=out) in the default mode="raise" was slower than the same call without out. PyArray_TakeFrom forced a defensive ENSURECOPY of out and wrote through a temporary that was copied back on success and only so out stayed unchanged if an out-of-bounds index raised

this removes that copy so in raise mode the result is written directly into out just like mode="wrap"/"clip" already do. The overlap-with-self copy (arrays_overlap) was kept so aliasing stays correct.

there IS a behavior change on an out-of-bounds index since out may now be partially written before IndexError is raised (previously it was left unchanged). take still raises. This proooobably warrants a release note and I'll write it after review

Before/after out=

  • 1-D, 85 -> 9350 elems: 7.66us -> 4.90us (1.56x)
  • 1-D, 1000 -> 100000 elems: 99.5us -> 47.3us (2.11x)
  • axis 0, (200, 64) -> (4000, 64): 214us -> 64.6us (3.31x)

The no-out path is unchanged and the speedup grows with output size (since the removed copy is O(n))

AI Disclosure

Used AI to double check my code + find any edge cases I might've missed :)

Sorry, something went wrong.

Uh oh!

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

All reactions
@Ijtihed

Ijtihed commented Jul 9, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@mattip since you mentioned this I thought you might be interested :)

All reactions

Sorry, something went wrong.

Uh oh!

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

ikrommyd
ikrommyd reviewed Jul 10, 2026
View reviewed changes

@ikrommyd ikrommyd left a comment •
edited
Loading

Uh oh!

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

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.

If the consensus is to just let out be modified, that is like basic guarantee in terms of c++ exception safety. I'd be tempted to add an additional message to the raised error in that case. Like if the error is raised and out= is set, you also say in the error message that out may be modified. cc @seberg @mattip what do you think? It can be done using by getting the exception that would be raised which is IndexError: index 4 is out of bounds and adding a note to it using the add_note method they have (numpy is already 3.12+). Something like

if (npy_fasttake(
        dest, src, indices_data, n, m, max_item, nelem, chunk,
        clipmode, itemsize, needs_refcounting, src_descr, dst_descr,
        axis) < 0) {
    if (out != NULL && obj == out) {
        PyObject *exc = PyErr_GetRaisedException();
        if (exc != NULL) {
            PyObject *res = PyObject_CallMethod(exc, "add_note", "s",
                "the output array `out` may have been partially modified");
            if (res == NULL) {
                PyErr_Clear();
            }
            Py_XDECREF(res);
            PyErr_SetRaisedException(exc);
        }
    }
    goto fail;
}

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/_core/tests/test_multiarray.py

@ikrommyd ikrommyd Jul 10, 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 might be a little bit of an excessive testing for this kind of tiny change IMO.

Sorry, something went wrong.

Uh oh!

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

seberg and Ijtihed reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
Comment thread numpy/_core/tests/test_multiarray.py Resolved

Uh oh!

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

@seberg

seberg commented Jul 10, 2026

Copy link
Copy Markdown
Member

I like the add-note idea, although it applies to many operations (i.e. if we do that, I think it should probably be a central helper and maybe added to some other places).

FWIW, it seems OK to me, although I don't care too much either way (mode=... is an existing path to get the no-copy in practice).
But it does need a release note IMO as it relaxes an intentional guarantee.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Ijtihed
PERF: avoid an unnecessary copy in np.take with out=
d27656a
@Ijtihed
Ijtihed force-pushed the perf/take-out-no-copy branch from e87e7ba to d27656a Compare July 10, 2026 07:50
@Ijtihed

Ijtihed commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

thanks both! I added a compatibility release note documenting that out can now be partially modified if an out of bounds index raises (the relaxed guarantee) then dropped the refcount-asserting test and trimmed the rest down to the essentials so there's no getrefcount asserts left. leak coverage can just be left to the sanitized CI

@ikrommyd @seberg I left add_note out for now since i agree it makes more sense as a central helper across the ops that can partially write out :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@Ijtihed

Ijtihed commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

I like the add-note idea, although it applies to many operations (i.e. if we do that, I think it should probably be a central helper and maybe added to some other places).

I'd also be happy to open a pr for this too if you think it makes sense

funtimefranky1530-coder, ngoldbaum, and ikrommyd reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

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

@charris charris added the 03 - Maintenance label Jul 10, 2026
This was referenced Jul 11, 2026
ENH: note on the error when out may be partially written #31965
Closed
ignore #31973
Closed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ikrommyd ikrommyd ikrommyd left review comments

Uh oh!

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

Assignees

No one assigned

Labels

03 - Maintenance

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

take slower when out is provided

Uh oh!

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

4 participants

@Ijtihed @seberg @ikrommyd @charris
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 01402775 — 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.