Skip to content

Add PyTuple_FromArray#5990

Merged
bschoenmaeckers merged 2 commits intoPyO3:mainfrom
bschoenmaeckers:tuple_from_array
Apr 23, 2026
Merged

Add PyTuple_FromArray#5990
bschoenmaeckers merged 2 commits intoPyO3:mainfrom
bschoenmaeckers:tuple_from_array

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Member

Avoids creating tuples using mutation api's.

This will add a additional Py_INCREF + Py_DECREF on the tuple items, so this may be slower.

@bschoenmaeckers bschoenmaeckers marked this pull request as draft April 20, 2026 09:12
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 20, 2026

Merging this PR will not alter performance

✅ 105 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing bschoenmaeckers:tuple_from_array (970d57d) with main (c24a951)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@davidhewitt
Copy link
Copy Markdown
Member

It might be possible to reclaim some peformance loss here by making an array of borrowed rather than owned values at the callsites of array_into_tuple, so only owned references actually get the incref/decref cycle.

Alternatively, if this is for RustPython, could gate this path on #[cfg(RustPython)]?

@bschoenmaeckers
Copy link
Copy Markdown
Member Author

bschoenmaeckers commented Apr 20, 2026

It might be possible to reclaim some peformance loss here by making an array of borrowed rather than owned values at the callsites of array_into_tuple, so only owned references actually get the incref/decref cycle.

Thanks! Using Borrowed gives some performance back indeed. But it still has some small performance hit (for owned IntoPyObject results).

Alternatively, if this is for RustPython, could gate this path on #[cfg(RustPython)]?

If the performance hit is too large, I will consider adding support for RustPython cfg.

Comment thread pyo3-ffi/src/compat/py_3_15.rs Outdated
@davidhewitt
Copy link
Copy Markdown
Member

I asked about the question of switching entirely for 3.15 at python/cpython#127058 (comment)

@bschoenmaeckers bschoenmaeckers marked this pull request as ready for review April 23, 2026 13:56
@bschoenmaeckers
Copy link
Copy Markdown
Member Author

I've removed the compat function and made it only available on python 3.15 & RustPython. Because of the slowdown I only actually use it for RustPython.

@bschoenmaeckers bschoenmaeckers added this pull request to the merge queue Apr 23, 2026
@bschoenmaeckers
Copy link
Copy Markdown
Member Author

Thanks!

Merged via the queue into PyO3:main with commit 4712a0a Apr 23, 2026
44 of 45 checks passed
@bschoenmaeckers bschoenmaeckers deleted the tuple_from_array branch April 23, 2026 16:15
davidhewitt pushed a commit to davidhewitt/pyo3 that referenced this pull request May 1, 2026
* Add `PyTuple_FromArray`

* Changelog
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