Repository navigation
Description
Summary
RustPython's vectorcall copies the PEP 590 calling shape (nargs, kwnames) but not its main property: calling without copying the argument vector. Every generic call moves its arguments into an owned Vec, converts it into FuncArgs (copying keyword names into a map), and then binds positional arguments by popping the front of that Vec. CPython does none of this. This issue compares the two designs and proposes a plan to make the native call path borrow its arguments.
Found while reviewing #9758, where the method descriptor now does args.remove(0) to take the receiver out of band (crates/vm/src/builtins/descriptor.rs, vectorcall_method_descriptor). That shift is not new: before #9758 the same remove(0) ran inside FuncArgs::take_positional. It is a symptom of the owned-Vec design, not of the receiver change.
Comparison
| CPython | RustPython today | |
|---|---|---|
| Vectorcall arguments | PyObject *const *args, borrowed from the caller's stack (vectorcallfunc, Include/object.h) |
Vec<PyObjectRef>, owned (VectorCallFunc, crates/vm/src/types/slot.rs) |
| Skip the first argument | args+1, nargs-1, pointer arithmetic (method_vectorcall_*, Objects/descrobject.c) |
args.remove(0), memmove (vectorcall_method_descriptor) |
Prepend self |
Write into the reserved args[-1] slot when PY_VECTORCALL_ARGUMENTS_OFFSET (Include/abstract.h) is set (method_vectorcall, Objects/classobject.c) |
args.insert(0, self), memmove (vectorcall_bound_method, crates/vm/src/builtins/function.rs; frame.rs call-with-self paths) |
| Native function ABI | METH_FASTCALL|METH_KEYWORDS: meth(self, args, nargs, kwnames), all borrowed (cfunction_vectorcall_FASTCALL_KEYWORDS, Objects/methodobject.c) |
PyNativeFn(vm, receiver, FuncArgs, Callee), where FuncArgs owns a Vec and a keyword map |
| Keyword arguments | kwnames tuple passed through unchanged |
FuncArgs::from_vectorcall_owned builds a map and copies every keyword name (as_wtf8().to_owned()) |
| Positional binding | Index into args (Argument Clinic generated code) |
FuncArgs::take_positional = args.remove(0) per argument, so O(n²) memmove over n arguments |
What already avoids the cost
- The
frame.rsspecializations (CallMethodDescriptor{Noargs,O,Fast,FastWithKeywords}, builtin call specializations) take arguments directly from the value stack, and the receiver without shifting (execute_call_method_descriptor,take_native_method_args). #[from_args(borrowed)]/BorrowedArgsbinds against the caller's storage without per-argument ownership transfer.
So the hot specialized paths are mostly fine. The remaining cost is on every generic call: unspecialized sites, calls through map/sorted(key=)/functools, PyObject_Vectorcall from native code, bound Python methods, and C-API calls.
Plan
Each step should land separately, keep every error message unchanged, and come with before/after benchmarks.
- Borrowed vectorcall arguments. Change
VectorCallFuncto take a borrowed argument slice (&[PyObjectRef]or an equivalent borrowed view),nargsfwith anARGUMENTS_OFFSETbit, andkwnames. Callers that build arguments on the value stack pass a stack slice with noVecallocation. Callers that want to prependselfreserve slot-1and set the offset bit, which removes theinsert(0, ...)invectorcall_bound_methodand the call-with-self paths inframe.rs. Callees that need ownership (for example a Python frame'slocalsplus) clone from the slice, which costs the same refcount increments as today's move plus no allocation. - Native ABI without
FuncArgsownership. Give native functions a borrowed argument view (args,nargs,kwnames), asMETH_FASTCALL|METH_KEYWORDSdoes.FuncArgsstays as the owned form fortp_call-style and*args/**kwargspaths. The method descriptor then passes&args[1..]and the receiver with no shift, and builtin functions pass the slice unchanged. - Index-based binding. Make
FromArgsbinders consume positionals by index (a cursor over the borrowed view) instead oftake_positional'sremove(0), and look keywords up inkwnamesinstead of a prebuilt map. This removes both the per-argument memmove and the keyword-name copies. Generated binders should match Clinic's parsing order and messages. - C-API. Pass the borrowed view straight to
METH_FASTCALLC functions instead of materializing a temporary array.
Validation
- Workspace tests, C-API tests, Clippy,
-m test test_descr test_inspect test_builtin test_call test_capi test_functools. - Micro-benchmarks before and after each step (release build, several interleaved samples, medians):
- unspecialized builtin call with keywords (e.g.
sorted(l, key=f)setup,int(x, base=10)throughmap) - unbound descriptor call through a generic path (
map(list.append, ...)-style) - bound Python method call (
obj.method(x)via a stored bound method) PyObject_Vectorcallfrom a C extension
- unspecialized builtin call with keywords (e.g.
— commented by Claude
Activity