Skip to content

Make vectorcall and the native call ABI borrow their arguments #9850

Open

Description

@youknowone

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.rs specializations (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)] / BorrowedArgs binds 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.

  1. Borrowed vectorcall arguments. Change VectorCallFunc to take a borrowed argument slice (&[PyObjectRef] or an equivalent borrowed view), nargsf with an ARGUMENTS_OFFSET bit, and kwnames. Callers that build arguments on the value stack pass a stack slice with no Vec allocation. Callers that want to prepend self reserve slot -1 and set the offset bit, which removes the insert(0, ...) in vectorcall_bound_method and the call-with-self paths in frame.rs. Callees that need ownership (for example a Python frame's localsplus) clone from the slice, which costs the same refcount increments as today's move plus no allocation.
  2. Native ABI without FuncArgs ownership. Give native functions a borrowed argument view (args, nargs, kwnames), as METH_FASTCALL|METH_KEYWORDS does. FuncArgs stays as the owned form for tp_call-style and *args/**kwargs paths. The method descriptor then passes &args[1..] and the receiver with no shift, and builtin functions pass the slice unchanged.
  3. Index-based binding. Make FromArgs binders consume positionals by index (a cursor over the borrowed view) instead of take_positional's remove(0), and look keywords up in kwnames instead 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.
  4. C-API. Pass the borrowed view straight to METH_FASTCALL C 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) through map)
    • unbound descriptor call through a generic path (map(list.append, ...)-style)
    • bound Python method call (obj.method(x) via a stored bound method)
    • PyObject_Vectorcall from a C extension

— commented by Claude

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions