Skip to content

avoid incrementing reference count of function args where possible#5941

Merged
davidhewitt merged 6 commits into
PyO3:mainfrom
davidhewitt:borrowed-func-args
Apr 3, 2026
Merged

avoid incrementing reference count of function args where possible#5941
davidhewitt merged 6 commits into
PyO3:mainfrom
davidhewitt:borrowed-func-args

Conversation

@davidhewitt
Copy link
Copy Markdown
Member

This is a micro-optimization to avoid temporary reference count cycles in .call family of functions when passed a Rust tuple containing references such as &Py, &Bound, and Borrowed where the reference is known to be owned by code somewhere higher up the stack.

Benchmarks will show an oversize effect on call_method functions because I changed these to re-use the method as a Python str rather than passing a Rust &str (which adds a conversion cost).

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 3, 2026

Merging this PR will improve performance by ×2.4

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 5 improved benchmarks
✅ 100 untouched benchmarks
⏩ 1 skipped benchmark1

Performance Changes

Benchmark BASE HEAD Efficiency
call_1 270 µs 198.6 µs +35.96%
call_method_1 709.4 µs 300.3 µs ×2.4
call_method_one_arg 612.7 µs 271.5 µs ×2.3
call_method 1,268.2 µs 791.7 µs +60.19%
call 607.2 µs 534.8 µs +13.53%

Comparing davidhewitt:borrowed-func-args (44c62ab) with main (46d9ca3)

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.

Comment thread src/types/tuple.rs Outdated
Comment thread src/types/tuple.rs
@davidhewitt davidhewitt enabled auto-merge April 3, 2026 12:19
@davidhewitt davidhewitt added this pull request to the merge queue Apr 3, 2026
Merged via the queue into PyO3:main with commit 3df3e4e Apr 3, 2026
43 of 45 checks passed
@davidhewitt davidhewitt deleted the borrowed-func-args branch April 3, 2026 16:17
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