Apply vectorcall for more places by youknowone · Pull Request #7336 · RustPython/RustPython
Repository navigation
Apply vectorcall for more places - #7336
Merged
youknowone merged 6 commits into RustPython:main from youknowone:skip-class Mar 4, 2026
Merged
Apply vectorcall for more places #7336 youknowone merged 6 commits into RustPython:main from youknowone:skip-class
Conversation
youknowone
commented
Mar 3, 2026
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
Member
Summary by CodeRabbit
- Refactor
- Call dispatch now prefers a vectorcall fast-path with internal fallbacks across calls, descriptors and types.
- New Features
- Added move-based argument handling to avoid unnecessary cloning during fast calls.
- Performance
- Lazier locals allocation and reduced argument copying for faster, lower-memory invocations.
- Bug Fixes
- Improved validation and clearer type errors for invalid method self-arguments.
coderabbitai
Bot
commented
Mar 3, 2026
·
edited
Loading
Copy link Copy Markdown
Contributor
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds PEP 590 vectorcall handlers and wiring across descriptors, types, frame dispatch, and argument construction; introduces move-based FuncArgs construction and lazy locals allocation in function fast paths. All changes are internal and preserve public APIs. Changes
Sequence DiagramsequenceDiagram
participant Frame as ExecutingFrame
participant Callable as CallableObject
participant Slot as .slots.vectorcall
participant FuncArgs as FuncArgs
participant VM as VirtualMachine
Frame->>Frame: assemble args Vec (include optional self)
Frame->>Callable: execute_call_vectorcall(args, nargs, kwnames, vm)
Callable->>Slot: read .vectorcall
alt slot present
Callable->>Slot: invoke vectorcall_handler(zelf, args, nargs, kwnames, vm)
Slot-->>Callable: PyResult
else slot absent or unsuitable
Callable->>FuncArgs: from_vectorcall_owned(args, nargs, kwnames)
FuncArgs-->>Callable: FuncArgs (moved)
Callable->>Callable: fallback call path using FuncArgs and vm
Callable-->>Callable: PyResult
end
Callable-->>Frame: return PyResult
Loading
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested reviewers
Poem🚥 Pre-merge checks | ✅ 3 ✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches 🧪 Generate unit tests (beta)
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. ❤️ ShareComment |
Bot
reviewed Mar 3, 2026
View reviewed changes
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 (3)crates/vm/src/builtins/type.rs (1)🤖 Prompt for all review comments with AI agentscrates/vm/src/builtins/descriptor.rs (1)
2339-2361: Use owned fallback conversion invectorcall_type.Line 2359 currently clones via borrowed conversion. Since
♻️ Proposed refactorargsis already owned,from_vectorcall_owned(args, ...)keeps fallback on the move-based path.- let func_args = FuncArgs::from_vectorcall(&args, nargs, kwnames); + let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames); PyType::call(zelf, func_args, vm)🤖 Prompt for AI AgentsVerify each finding against the current code and only fix it if needed. In `@crates/vm/src/builtins/type.rs` around lines 2339 - 2361, In vectorcall_type the fallback constructs FuncArgs using the borrowed conversion FuncArgs::from_vectorcall(&args, nargs, kwnames) even though args is owned; change this to use the move-based API by calling FuncArgs::from_vectorcall_owned(args, nargs, kwnames) so the owned Vec<PyObjectRef> is consumed instead of cloned; update the call site in the function vectorcall_type and ensure signatures match the moved value usage when passing func_args into PyType::call.crates/vm/src/frame.rs (1)
429-440: Avoid cloning in descriptor vectorcall helpers.Both helpers take owned
♻️ Proposed refactorVec<PyObjectRef>, butfrom_vectorcall(...)clones values (Line 438 and Line 468). Switching to owned conversion keeps these paths consistent with the PR's vectorcall optimization goal.fn vectorcall_method_descriptor( zelf_obj: &PyObject, args: Vec<PyObjectRef>, nargs: usize, kwnames: Option<&[PyObjectRef]>, vm: &VirtualMachine, ) -> PyResult { let zelf: &Py<PyMethodDescriptor> = zelf_obj.downcast_ref().unwrap(); - let func_args = FuncArgs::from_vectorcall(&args, nargs, kwnames); + let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames); (zelf.method.func)(vm, func_args) } @@ fn vectorcall_wrapper( zelf_obj: &PyObject, args: Vec<PyObjectRef>, nargs: usize, kwnames: Option<&[PyObjectRef]>, vm: &VirtualMachine, ) -> PyResult { let zelf: &Py<PyWrapper> = zelf_obj.downcast_ref().unwrap(); @@ - let obj = args[0].clone(); + let mut iter = args.into_iter(); + let obj = iter.next().unwrap(); @@ - let rest = FuncArgs::from_vectorcall(&args[1..], nargs - 1, kwnames); + let rest = FuncArgs::from_vectorcall_owned(iter.collect(), nargs - 1, kwnames); zelf.wrapped.call(obj, rest, vm) }Also applies to: 442-470
🤖 Prompt for AI AgentsVerify each finding against the current code and only fix it if needed. In `@crates/vm/src/builtins/descriptor.rs` around lines 429 - 440, The vectorcall helpers (e.g., vectorcall_method_descriptor) take an owned Vec<PyObjectRef> and then call FuncArgs::from_vectorcall(&args, nargs, kwnames), which forces cloning; change the helper signatures to accept a slice or ref to the args (e.g., args: &[PyObjectRef] or args: &Vec<PyObjectRef]) and call FuncArgs::from_vectorcall(args, nargs, kwnames) so no clones occur, update all callers to pass a slice, and apply the same fix to the other helper in the 442-470 range to preserve the vectorcall optimization.
5673-5706: Add explicit stack-shape assertions in vectorcall extractors.These helpers are now on more hot paths. Adding targeted debug assertions (and explicit
♻️ Suggested diagnostic hardeningexpect_uncheckedmessages) would make stack-corruption regressions easier to diagnose than raw index/unwrap()panics.fn execute_call_vectorcall(&mut self, nargs: u32, vm: &VirtualMachine) -> FrameResult { let nargs_usize = nargs as usize; let stack_len = self.state.stack.len(); + debug_assert!( + stack_len >= nargs_usize + 2, + "CALL stack underflow: expected callable + self_or_null + args" + ); let callable_idx = stack_len - nargs_usize - 2; let self_or_null_idx = stack_len - nargs_usize - 1; let args_start = stack_len - nargs_usize; @@ for stack_idx in args_start..stack_len { - let val = self.state.stack[stack_idx].take().unwrap().to_pyobj(); + let val = expect_unchecked( + self.state.stack[stack_idx].take(), + "CALL arg slot unexpectedly NULL", + ) + .to_pyobj(); args_vec.push(val); } @@ - let callable_obj = self.state.stack[callable_idx].take().unwrap().to_pyobj(); + let callable_obj = expect_unchecked( + self.state.stack[callable_idx].take(), + "CALL callable slot unexpectedly NULL", + ) + .to_pyobj(); fn execute_call_kw_vectorcall(&mut self, nargs: u32, vm: &VirtualMachine) -> FrameResult { let nargs_usize = nargs as usize; @@ let kw_count = kwarg_names_tuple.len(); + debug_assert!(kw_count <= nargs_usize, "CALL_KW kw_count exceeds nargs"); @@ for stack_idx in args_start..stack_len { - let val = self.state.stack[stack_idx].take().unwrap().to_pyobj(); + let val = expect_unchecked( + self.state.stack[stack_idx].take(), + "CALL_KW arg slot unexpectedly NULL", + ) + .to_pyobj(); args_vec.push(val); } @@ - let callable_obj = self.state.stack[callable_idx].take().unwrap().to_pyobj(); + let callable_obj = expect_unchecked( + self.state.stack[callable_idx].take(), + "CALL_KW callable slot unexpectedly NULL", + ) + .to_pyobj();Also applies to: 5711-5754
🤖 Prompt for AI AgentsVerify each finding against the current code and only fix it if needed. In `@crates/vm/src/frame.rs` around lines 5673 - 5706, In execute_call_vectorcall (and the similar vectorcall extractor at 5711-5754) add explicit stack-shape assertions and replace raw indexing/unwraps with checked messages: assert the stack length and computed indices (callable_idx, self_or_null_idx, args_start) via debug_assert!(self.state.stack.len() >= nargs_usize + 2, "...") and use expect_unchecked or expect with clear messages when taking values (e.g., self.state.stack[self_or_null_idx].take().expect("vectorcall: missing self_or_null at self_or_null_idx"), same for callable and argument takes) so any stack-corruption panics include a descriptive identifier (function name and index) instead of opaque unwrap/index panics.
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/vm/src/builtins/descriptor.rs`:
- Around line 429-440: The vectorcall helpers (e.g.,
vectorcall_method_descriptor) take an owned Vec<PyObjectRef> and then call
FuncArgs::from_vectorcall(&args, nargs, kwnames), which forces cloning; change
the helper signatures to accept a slice or ref to the args (e.g., args:
&[PyObjectRef] or args: &Vec<PyObjectRef]) and call
FuncArgs::from_vectorcall(args, nargs, kwnames) so no clones occur, update all
callers to pass a slice, and apply the same fix to the other helper in the
442-470 range to preserve the vectorcall optimization.
In `@crates/vm/src/builtins/type.rs`:
- Around line 2339-2361: In vectorcall_type the fallback constructs FuncArgs
using the borrowed conversion FuncArgs::from_vectorcall(&args, nargs, kwnames)
even though args is owned; change this to use the move-based API by calling
FuncArgs::from_vectorcall_owned(args, nargs, kwnames) so the owned
Vec<PyObjectRef> is consumed instead of cloned; update the call site in the
function vectorcall_type and ensure signatures match the moved value usage when
passing func_args into PyType::call.
In `@crates/vm/src/frame.rs`:
- Around line 5673-5706: In execute_call_vectorcall (and the similar vectorcall
extractor at 5711-5754) add explicit stack-shape assertions and replace raw
indexing/unwraps with checked messages: assert the stack length and computed
indices (callable_idx, self_or_null_idx, args_start) via
debug_assert!(self.state.stack.len() >= nargs_usize + 2, "...") and use
expect_unchecked or expect with clear messages when taking values (e.g.,
self.state.stack[self_or_null_idx].take().expect("vectorcall: missing
self_or_null at self_or_null_idx"), same for callable and argument takes) so any
stack-corruption panics include a descriptive identifier (function name and
index) instead of opaque unwrap/index panics.
ℹ️ Review info
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between be0c3ca and 5dc8c48 .
📒 Files selected for processing (6)crates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/function.rscrates/vm/src/builtins/type.rscrates/vm/src/frame.rscrates/vm/src/function/argument.rscrates/vm/src/protocol/callable.rs
8388f9e to
4a91e0a
Compare
March 3, 2026 19:38 coderabbitai
Bot
reviewed Mar 3, 2026
View reviewed changes
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 .
Actionable comments posted: 1
🧹 Nitpick comments (1)crates/vm/src/frame.rs (1)🤖 Prompt for all review comments with AI agents
4119-4125: Collapse duplicated branch tails in call specializations.These blocks have identical tail calls with only conditional deopt side effects. You can deopt conditionally and invoke the common path once.
♻️ Proposed refactor@@ - if cached_tag == callable_tag && callable.downcast_ref::<PyType>().is_some() { - return self.execute_call_vectorcall(nargs, vm); - } - - self.deoptimize_call(); - self.execute_call_vectorcall(nargs, vm) + if !(cached_tag == callable_tag && callable.downcast_ref::<PyType>().is_some()) { + self.deoptimize_call(); + } + self.execute_call_vectorcall(nargs, vm) @@ - if cached_tag == callable_tag { - return self.execute_call_vectorcall(nargs, vm); - } - - self.deoptimize_call(); - self.execute_call_vectorcall(nargs, vm) + if cached_tag != callable_tag { + self.deoptimize_call(); + } + self.execute_call_vectorcall(nargs, vm) @@ - if cached_tag == callable_tag { - return self.execute_call_kw_vectorcall(nargs, vm); - } - - self.deoptimize_call_kw(); - self.execute_call_kw_vectorcall(nargs, vm) + if cached_tag != callable_tag { + self.deoptimize_call_kw(); + } + self.execute_call_kw_vectorcall(nargs, vm)As per coding guidelines: "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code".
Also applies to: 4201-4206, 4292-4297
🤖 Prompt for AI AgentsVerify each finding against the current code and only fix it if needed. In `@crates/vm/src/frame.rs` around lines 4119 - 4125, The duplicated tail can be collapsed by testing the specialization condition first (compute callable_tag and check cached_tag == callable_tag and callable.downcast_ref::<PyType>().is_some()), call self.deoptimize_call() only when that condition is false, and then unconditionally call self.execute_call_vectorcall(nargs, vm) once; apply the same pattern to the other similar blocks (around the sites using callable_tag, cached_tag, callable.downcast_ref::<PyType>(), self.deoptimize_call(), and self.execute_call_vectorcall).
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/vm/src/frame.rs`:
- Line 5724: The subtraction of positional args for CALL_KW can underflow in
release builds because the safety check is only a debug_assert; replace the
direct subtraction using nargs_usize - kw_count with
nargs_usize.checked_sub(kw_count).expect("CALL_KW: kw_count exceeds nargs") to
harden the calculation, referencing the kw_count and nargs_usize variables in
the CALL_KW handling code (around the debug_assert!) so it panics with a clear
message instead of underflowing.
---
Nitpick comments:
In `@crates/vm/src/frame.rs`:
- Around line 4119-4125: The duplicated tail can be collapsed by testing the
specialization condition first (compute callable_tag and check cached_tag ==
callable_tag and callable.downcast_ref::<PyType>().is_some()), call
self.deoptimize_call() only when that condition is false, and then
unconditionally call self.execute_call_vectorcall(nargs, vm) once; apply the
same pattern to the other similar blocks (around the sites using callable_tag,
cached_tag, callable.downcast_ref::<PyType>(), self.deoptimize_call(), and
self.execute_call_vectorcall).
ℹ️ Review info
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between 8388f9e and 4a91e0a .
📒 Files selected for processing (6)crates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/function.rscrates/vm/src/builtins/type.rscrates/vm/src/frame.rscrates/vm/src/function/argument.rscrates/vm/src/protocol/callable.rs
- crates/vm/src/builtins/type.rs
- crates/vm/src/protocol/callable.rs
Add PyType vectorcall and use vectorcall in all specialized call fall...
f584c47
...backs
Add vectorcall slot for PyMethodDescriptor and PyWrapper
de9cbc4
Add FuncArgs::from_vectorcall_owned and simplify vectorcall fallback ...
02a6998
...paths Remove has_vectorcall checks from execute_call_vectorcall and execute_call_kw_vectorcall. The invoke_vectorcall fallback now uses from_vectorcall_owned to move args instead of cloning.
Use lazy locals allocation in vectorcall fast path, add debug_assert ...
07f129b
...bounds
Use from_vectorcall_owned in descriptor/type vectorcall, add stack de...
c47d1f2
...bug_asserts
Collapse duplicated branch tails and use checked_sub in CALL_KW
2885f80
- Collapse conditional deopt + unconditional vectorcall pattern in CallBuiltinClass, CallNonPyGeneral, CallKwNonPy - Use checked_sub for nargs_usize - kw_count in execute_call_kw_vectorcall to prevent silent underflow in release buildsyouknowone force-pushed the skip-class branch from
4112577 to
2885f80
Compare
March 4, 2026 07:05 coderabbitai
Bot
reviewed Mar 4, 2026
View reviewed changes
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/frame.rs (1)🤖 Prompt for all review comments with AI agents
3850-3850: Optional: extract shared deopt-and-dispatch helpers for CALL fallbacks.Several handlers now repeat the same
♻️ Proposed refactordeoptize(...)+execute_call_*_vectorcall(...)pattern. A tiny helper pair would reduce repetition and make future edits safer.+ #[inline] + fn deopt_and_call_vectorcall(&mut self, nargs: u32, vm: &VirtualMachine) -> FrameResult { + self.deoptize(Instruction::Call { + argc: Arg::marker(), + }); + self.execute_call_vectorcall(nargs, vm) + } + + #[inline] + fn deopt_and_call_kw_vectorcall(&mut self, nargs: u32, vm: &VirtualMachine) -> FrameResult { + self.deoptize(Instruction::CallKw { + argc: Arg::marker(), + }); + self.execute_call_kw_vectorcall(nargs, vm) + }As per coding guidelines: "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code".
Also applies to: 3887-3887, 3925-3925, 3934-3939, 3999-3999, 4038-4038, 4071-4071, 4080-4085, 4176-4181
🤖 Prompt for AI AgentsVerify each finding against the current code and only fix it if needed. In `@crates/vm/src/frame.rs` at line 3850, Several CALL fallback handlers duplicate the pattern of calling deoptize(...) and then immediately invoking an execute_call_*_vectorcall(...) variant (e.g., the call that currently does self.execute_call_vectorcall(nargs, vm)); extract a small helper (e.g., a private method like deopt_and_dispatch(&mut self, target: CallDispatch, nargs: usize, vm: &mut Vm) or two helpers deoptize_then_dispatch and dispatch_vectorcall) so each branch first computes the differing value (the specific execute_call_*_vectorcall variant to call) and then calls the shared helper to run deoptize(...) and perform the vectorcall; update all similar sites (the instances you listed and the call currently using self.execute_call_vectorcall) to use the helper so the deoptize + dispatch logic is centralized.
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/vm/src/frame.rs`:
- Line 3850: Several CALL fallback handlers duplicate the pattern of calling
deoptize(...) and then immediately invoking an execute_call_*_vectorcall(...)
variant (e.g., the call that currently does self.execute_call_vectorcall(nargs,
vm)); extract a small helper (e.g., a private method like
deopt_and_dispatch(&mut self, target: CallDispatch, nargs: usize, vm: &mut Vm)
or two helpers deoptize_then_dispatch and dispatch_vectorcall) so each branch
first computes the differing value (the specific execute_call_*_vectorcall
variant to call) and then calls the shared helper to run deoptize(...) and
perform the vectorcall; update all similar sites (the instances you listed and
the call currently using self.execute_call_vectorcall) to use the helper so the
deoptize + dispatch logic is centralized.
ℹ️ Review info Run configuration
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
Run ID : 20bd55db-0df7-4bc0-bee1-7a8874c1dd72
Reviewing files that changed from the base of the PR and between 4112577 and 2885f80 .
📒 Files selected for processing (6)crates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/function.rscrates/vm/src/builtins/type.rscrates/vm/src/frame.rscrates/vm/src/function/argument.rscrates/vm/src/protocol/callable.rs
- crates/vm/src/builtins/function.rs
3504993
into
RustPython : main
Mar 4, 2026
22 of 23 checks passed
youknowone
deleted the
skip-class
branch
March 4, 2026 09:09
This was referenced Mar 5, 2026
Harden CALL specialization guards and cache callables
#7360
Merged
Refine specialization caches and extend binary-op coverage
#7386
Merged
coderabbitai
Bot
mentioned this pull request Mar 17, 2026 Add per-type vectorcall for builtin constructors #7407 Merged youknowone added a commit to youknowone/RustPython that referenced this pull request Mar 22, 2026
Apply vectorcall for more places (RustPython#7336)
c03084c
* Add PyType vectorcall and use vectorcall in all specialized call fallbacks * Add vectorcall slot for PyMethodDescriptor and PyWrapper * Add FuncArgs::from_vectorcall_owned and simplify vectorcall fallback paths Remove has_vectorcall checks from execute_call_vectorcall and execute_call_kw_vectorcall. The invoke_vectorcall fallback now uses from_vectorcall_owned to move args instead of cloning.youknowone added a commit that referenced this pull request Sep 16, 2026
Apply vectorcall for more places (#7336)
fe4a5d1
* Add PyType vectorcall and use vectorcall in all specialized call fallbacks * Add vectorcall slot for PyMethodDescriptor and PyWrapper * Add FuncArgs::from_vectorcall_owned and simplify vectorcall fallback paths Remove has_vectorcall checks from execute_call_vectorcall and execute_call_kw_vectorcall. The invoke_vectorcall fallback now uses from_vectorcall_owned to move args instead of cloning.Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment