GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid per-tail-call owner allocations by moreal · Pull Request #8472 · RustPython/RustPython

Conversation

@moreal

@moreal moreal commented Aug 8, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • This PR follows our AI policy

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@coderabbitai

coderabbitai Bot commented Aug 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

  • RustPython/RustPython#8354: Overlaps with interpreter-frame execution and VM thread-frame plumbing.
  • RustPython/RustPython#8431: Introduces the trampoline path refined by this PR.
  • RustPython/RustPython#8433: Provides the pending ownership and trampoline mechanisms refined here.

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
  • X
  • Mastodon
  • Reddit
  • LinkedIn

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

moreal added 2 commits August 9, 2026 08:54
@moreal
Add repeatable tail-call trampoline benchmarks
068a2ce
Assisted-by: Codex:gpt-5.6-sol
@moreal
Avoid per-tail-call owner allocations
b97e119
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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

coderabbitai[bot]
coderabbitai Bot reviewed Aug 9, 2026
View reviewed changes

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@youknowone
youknowone requested a review from ShaharNaveh August 22, 2026 07:25
ShaharNaveh
ShaharNaveh approved these changes Aug 22, 2026
View reviewed changes
@moreal

moreal commented Aug 22, 2026

Copy link
Copy Markdown
Contributor Author

This pull request is replaced by fd27902#diff-fe30ecf7c6b462e46e0874a55a05ea0edba83cefad5ac7a86e7137fa8f86ba74L863

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@moreal moreal closed this Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@coderabbitai coderabbitai[bot] coderabbitai[bot] left review comments
@ShaharNaveh ShaharNaveh ShaharNaveh approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@moreal moreal

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@moreal @fanninpm @youknowone @ShaharNaveh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.