Skip to content

PERF: fast-path large order diff#31897

Draft
KRRT7 wants to merge 2 commits into
numpy:mainfrom
KRRT7:diff-large-n-fastpath
Draft

PERF: fast-path large order diff#31897
KRRT7 wants to merge 2 commits into
numpy:mainfrom
KRRT7:diff-large-n-fastpath

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

PR summary

Add a fast path for np.diff when n is at least the effective axis length. In that case the result is empty, so numeric and non-object dtypes can avoid repeated differencing of already-empty arrays. Object arrays keep the existing path so Python-level subtraction behavior is preserved.

ASV quick comparison against main:

bench_function_base.Diff.time_large_n((10,), 10):       21.0 us -> 16.5 us
bench_function_base.Diff.time_large_n((10, 10), 10):    41.3 us -> 13.9 us
bench_function_base.Diff.time_large_n((10,), 1000):      346 us -> 12.4 us
bench_function_base.Diff.time_large_n((10, 10), 1000):   481 us -> 15.0 us

First time committer introduction

I am a NumPy user/contributor looking at small, focused performance improvements in common array operations. I will handle review discussion and follow-up changes directly.

AI Disclosure

LLM / Harness: hybrid.

AI assistance was used to inspect the codebase, run local commands and benchmarks, draft PR text, and help prepare candidate patches. I reviewed the generated suggestions, selected the final changes, and am responsible for the code, submission, and follow-up discussion. Some PR text and candidate code edits were drafted with AI assistance; the final patch and text were reviewed and edited by me.

@mattip

mattip commented Jul 7, 2026

Copy link
Copy Markdown
Member

This is the third such PR. If we accept all of them, what will be the overall impact on time to run the benchmarking suite? I am a bit hesitant to add so many new benchmarks. Also, like the other one, you need to benchmark the non-empty paths.

@KRRT7

KRRT7 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Also, like the other one, you need to benchmark the non-empty paths.

internally I run the end to end benchmarks, however I'll start posting all of them.

what will be the overall impact on time to run the benchmarking suite?

overall shouldn't be much, but I can drop the benchmarks, but then in the future I'll be asked to provide benchmarks....

@KRRT7 KRRT7 marked this pull request as draft July 7, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants