Skip to content

add slice-ptr accessors to PyBuffer - #6382

Merged
davidhewitt merged 3 commits into
PyO3:mainfrom
Person-93:buffer-slice-pointers
Sep 12, 2026
Merged

davidhewitt merged 3 commits into
PyO3:mainfrom
Person-93:buffer-slice-pointers

Conversation

@Person-93

@Person-93 Person-93 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Add accessors to PyBuffer that get the data as a pointer to a slice. See #6267

@codspeed

codspeed Bot commented Sep 2, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 13.23%

⚡ 1 improved benchmark
✅ 140 untouched benchmarks
⏩ 6 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
test_empty_class_init 32.4 µs 28.7 µs +13.23%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Person-93:buffer-slice-pointers (96b31c5) with main (da9da49)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@davidhewitt davidhewitt left a comment

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.

Thanks

Comment thread src/buffer.rs Outdated
Comment on lines +331 to +332
/// Returns null if the buffer is not C-style contiguous
pub fn as_slice_ptr(&self) -> *mut [T] {

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 Option<NonNull<[T]>> be better? (And similar on the other methods.)

@davidhewitt davidhewitt left a comment

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.

Thanks, this looks fine to me now.

I think it might be worth adding some documentation explaining to users that they are responsible for avoiding data races while reading / writing the buffer. We can do that as part of #6381

@davidhewitt
davidhewitt added this pull request to the merge queue Sep 12, 2026
Merged via the queue into PyO3:main with commit 5f3ea66 Sep 12, 2026
50 of 53 checks passed
@Person-93
Person-93 deleted the buffer-slice-pointers branch September 12, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants