Skip to content

Avoid per-tail-call owner allocations - #8472

Draft
moreal wants to merge 2 commits into
RustPython:mainfrom
moreal:agent/avoid-per-tail-call-owner-allocations
Draft

Avoid per-tail-call owner allocations#8472
moreal wants to merge 2 commits into
RustPython:mainfrom
moreal:agent/avoid-per-tail-call-owner-allocations

Conversation

@moreal

@moreal moreal commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add a repeatable benchmark for specialized exact-function, exact-bound-method, shallow, and deep tail-call paths
  • replace the pending tail-call owner Vec<PyObjectRef> with a single Option<PyObjectRef>
  • move the exact callable into the owner slot and release temporary bound-method wrappers before entering the callee

Motivation

The specialized exact-call handlers retained callable ownership by allocating a new vector for every tail call. The trampoline only needs one owner object: the exact function itself. Keeping that owner in a single slot removes the per-call vector allocation while preserving ownership across return and unwind paths.

For bound methods, the function owns the executable code and self is already retained in fast locals, so the temporary bound-method wrapper can be released immediately.

Performance

Release-mode benchmark medians from 14 interleaved before/after samples:

Case Before After Change
Exact function 189.210 ns 165.974 ns -12.28%
Exact bound method 177.925 ns 173.704 ns -2.37%
Shallow steady trampoline 320.042 ns 288.682 ns -9.80%
Deep steady trampoline 17,756.400 ns 15,564.300 ns -12.35%
Shallow fresh activation 318.714 ns 289.905 ns -9.04%
Deep fresh activation 17,813.900 ns 15,471.650 ns -13.15%

The inline control changed by +0.99%.

Summary by CodeRabbit

  • Performance

    • Improved tail-call execution and memory ownership handling, including nested calls and exception paths.
    • Reduced unnecessary retained references during bound-method and regular tail calls.
  • Benchmarking

    • Added a RustPython-only benchmark covering arithmetic, function calls, bound methods, recursion, nesting, and trampoline activations.
    • Reports per-iteration timings, medians, and comparisons against an inline baseline.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds a RustPython-only tail-call benchmark and changes VM tail-call lifetime management from a reference vector to one pending callee owner transferred through suspended trampoline frames.

Changes

Tail-call ownership and benchmark coverage

Layer / File(s) Summary
Tail-call benchmark coverage
benches/tailcall_baseline.py
Adds configurable benchmarks for direct, bound-method, nested, recursive, and fresh trampoline calls. It validates specializations, warms paths, samples cases, and reports medians and deltas.
Pending callee owner setup
crates/vm/src/vm/mod.rs, crates/vm/src/vm/thread.rs, crates/vm/src/frame.rs
Replaces the pending tail-call reference vector with one owning callee slot. Regular and bound-method tail calls transfer the required function owner.
Trampoline owner lifecycle
crates/vm/src/vm/mod.rs
Transfers owners into suspended frames and drops or replaces them during nested tail calls, returns, and exception handling.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested reviewers: youknowone, shaharnaveh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: removing per-tail-call owner allocations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@moreal moreal self-assigned this Aug 8, 2026
@moreal moreal added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 8, 2026
@fanninpm

fanninpm commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Does this PR follow our AI Policy? If not, we may close your PR.

@moreal
moreal force-pushed the agent/avoid-per-tail-call-owner-allocations branch from ac3be2e to f197de0 Compare August 8, 2026 23:47
@moreal

moreal commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Does this PR follow our AI Policy? If not, we may close your PR.

@fanninpm I'm sorry to miss leaving Assisted-By: in commits. I adjusted them now. About changes in this PR, I reviewed them manually.

moreal added 2 commits August 9, 2026 08:54
Assisted-by: Codex:gpt-5.6-sol
Assisted-by: Codex:gpt-5.6-sol
@moreal
moreal force-pushed the agent/avoid-per-tail-call-owner-allocations branch from f197de0 to b97e119 Compare August 8, 2026 23:54
@moreal
moreal marked this pull request as ready for review August 9, 2026 04:36
@moreal
moreal marked this pull request as draft August 9, 2026 04:36
@youknowone

youknowone commented Aug 9, 2026

Copy link
Copy Markdown
Member

@fanninpm The AI policy also includes:

These rules apply only to outside contributions to RustPython. Maintainers are exempt from these rules and may use AI tools at their discretion

having Assisted-By: is recommended for everyone though

@coderabbitai coderabbitai Bot left a comment

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.

🧹 Nitpick comments (1)
crates/vm/src/vm/mod.rs (1)

113-117: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use Cell instead of UnsafeCell for pending_tailcall_owner.

Cell<Option<PyObjectRef>> supports .set() and .take() directly, because Option<T> implements Default regardless of whether T does. This gives the same single-threaded interior-mutability guarantee as the current UnsafeCell, without an unsafe raw-pointer dereference in set_pending_tailcall_owner and take_pending_tailcall_owner.

The comment on line 116 justifies the choice by stating the VM is per-thread and the field is accessed only on the owning thread. That reasoning applies equally to Cell, so it does not explain why UnsafeCell is required here.

♻️ Proposed refactor to remove the unsafe raw-pointer access
-    pending_tailcall_owner: core::cell::UnsafeCell<Option<PyObjectRef>>,
+    pending_tailcall_owner: Cell<Option<PyObjectRef>>,
     pub(crate) fn set_pending_tailcall_owner(&self, owner: PyObjectRef) {
-        let slot = unsafe { &mut *self.pending_tailcall_owner.get() };
-        debug_assert!(slot.is_none(), "pending TailCall owner was not consumed");
-        *slot = Some(owner);
+        let previous = self.pending_tailcall_owner.replace(Some(owner));
+        debug_assert!(previous.is_none(), "pending TailCall owner was not consumed");
     }

     fn take_pending_tailcall_owner(&self) -> PyObjectRef {
-        unsafe { &mut *self.pending_tailcall_owner.get() }
-            .take()
-            .expect("TailCall without pending owner")
+        self.pending_tailcall_owner
+            .take()
+            .expect("TailCall without pending owner")
     }

Also applies to: 1409-1424

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/vm/src/vm/mod.rs` around lines 113 - 117, Replace the UnsafeCell-based
pending_tailcall_owner field with Cell<Option<PyObjectRef>> and update its
initialization and accessors, especially set_pending_tailcall_owner and
take_pending_tailcall_owner, to use Cell::set and Cell::take directly. Remove
the unsafe raw-pointer dereferences while preserving the existing ownership and
single-threaded behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/vm/src/vm/mod.rs`:
- Around line 113-117: Replace the UnsafeCell-based pending_tailcall_owner field
with Cell<Option<PyObjectRef>> and update its initialization and accessors,
especially set_pending_tailcall_owner and take_pending_tailcall_owner, to use
Cell::set and Cell::take directly. Remove the unsafe raw-pointer dereferences
while preserving the existing ownership and single-threaded behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: ddf1bc6f-fe93-4dc1-87df-aeecedaa5c21

📥 Commits

Reviewing files that changed from the base of the PR and between 1819677 and b97e119.

📒 Files selected for processing (4)
  • benches/tailcall_baseline.py
  • crates/vm/src/frame.rs
  • crates/vm/src/vm/mod.rs
  • crates/vm/src/vm/thread.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants