Skip to content

PERF: fast-path no-op roll cases#31894

Draft
KRRT7 wants to merge 2 commits into
numpy:mainfrom
KRRT7:roll-zero-shift-fastpath
Draft

PERF: fast-path no-op roll cases#31894
KRRT7 wants to merge 2 commits into
numpy:mainfrom
KRRT7:roll-zero-shift-fastpath

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

PR summary

Fast-paths two no-op np.roll cases:

  • return the input directly for axis=()
  • avoid the generic slice-product roll loop when all normalized shifts are zero, while preserving copy semantics

Addresses gh-12389.

Benchmarks from spin bench --compare main HEAD -t bench_manipulate.DimsManipulations:

  • time_roll_empty_axes: 59-64% faster
  • time_roll_zero_shift: 16-18% faster

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.

@KRRT7 KRRT7 marked this pull request as draft July 7, 2026 16:52
Comment thread numpy/_core/numeric.py
for ax, offset in shifts.items():
offset %= a.shape[ax] or 1 # If `a` is empty, nothing matters.
if offset:
roll_needed = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This adds a (tiny) bit of overhead for all cases. How important is the case where no roll is needed? Or where axis is empty?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would show up as a slower benchmark if it was noticeable no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

either way, I have more PRs that build on top of this and recover whatever (imo, negligible) losses there might be.

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.

4 participants