Skip to content

Use datastack path for __init__ dispatch in CallAllocAndEnterInit - #8448

Open
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:init-datastack-path
Open

Use datastack path for __init__ dispatch in CallAllocAndEnterInit#8448
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:init-datastack-path

Conversation

@youknowone

@youknowone youknowone commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

specialization_run_init (used by CallAllocAndEnterInit) called prepare_exact_args_frame which heap-allocates a FrameObject with 4-5 atomic refcount bumps per call. This switches to invoke_prepared_exact_args which uses InterpreterFrame::new_on_datastack + run_frame_fast with zero refcount bumps on code/globals/builtins/func_obj.

Part of #40.

Safety

CallAllocAndEnterInit already guards against:

  • Tracing (specialization_eval_frame_active)
  • Recursion limits (specialization_call_recursion_guard)
  • Datastack space (specialization_has_datastack_space_for_func)

These are the same guards used by CallPyExactArgs which already uses the datastack path.

Changes

  • frame.rs: specialization_run_init — replace prepare_exact_args_frame + run_frame + release_datastack_frame with invoke_prepared_exact_args
  • function.rs: invoke_prepared_exact_args visibility fnpub(crate) fn

Summary by CodeRabbit

  • Refactor
    • Streamlined object initialization by using the standard prepared-call flow.
    • Preserved existing initialization result validation and error handling.

specialization_run_init used prepare_exact_args_frame which heap-allocates
a FrameObject with 4-5 atomic refcount bumps per call. Switch to
invoke_prepared_exact_args which uses InterpreterFrame::new_on_datastack
with zero refcount bumps.

CallAllocAndEnterInit already guards against tracing (eval_frame_active)
and recursion limits before reaching this path.

Assisted-by: Claude
Copilot AI review requested due to automatic review settings August 3, 2026 14:13
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: 485d7f99-9612-41b9-b196-fccac2c5603d

📥 Commits

Reviewing files that changed from the base of the PR and between a635d0b and b8a1d72.

📒 Files selected for processing (2)
  • crates/vm/src/builtins/function.rs
  • crates/vm/src/frame.rs

📝 Walkthrough

Walkthrough

The change makes invoke_prepared_exact_args crate-visible and updates specialization_run_init to use it for __init__ execution. Result validation and error propagation remain unchanged.

Changes

Initializer invocation

Layer / File(s) Summary
Shared prepared invocation
crates/vm/src/builtins/function.rs, crates/vm/src/frame.rs
invoke_prepared_exact_args is visible within the crate. specialization_run_init uses it instead of manually preparing, running, and releasing a frame.

Estimated code review effort: 2 (Simple) | ~5 minutes

Possibly related PRs

Suggested reviewers: copilot, 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 describes the main change: using the datastack path for init dispatch in CallAllocAndEnterInit.
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR optimizes CallAllocAndEnterInit’s __init__ dispatch by switching from a heap-allocated FrameObject path to a datastack-based invocation path, reducing refcount churn and allocation overhead.

Changes:

  • Replace prepare_exact_args_frame + run_frame + release_datastack_frame with invoke_prepared_exact_args in specialization_run_init.
  • Widen visibility of invoke_prepared_exact_args to pub(crate) to enable reuse from frame.rs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/vm/src/frame.rs Uses the datastack fast-path invocation for __init__ instead of allocating a frame object.
crates/vm/src/builtins/function.rs Exposes invoke_prepared_exact_args within the crate to support the new call site.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/vm/src/frame.rs
let init_result = vm.run_frame(init_frame.clone());
release_datastack_frame(&init_frame, vm);
let init_result = init_result?;
let init_result = init_func.invoke_prepared_exact_args(taken, vm)?;
Comment on lines +794 to 797
pub(crate) fn invoke_prepared_exact_args(
&self,
args: impl ExactSizeIterator<Item = PyObjectRef>,
vm: &VirtualMachine,
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