Title: vectercall per type · Issue #7362 · RustPython/RustPython · GitHub
Open Graph Title: vectercall per type · Issue #7362 · RustPython/RustPython
X Title: vectercall per type · Issue #7362 · RustPython/RustPython
Description: Support per-type-instance vectorcall for builtin types (dict, list, int, ...) Summary Currently, all type-object constructor calls (e.g. dict(x=1), list([1,2,3]), int("42")) go through a single vectorcall_type function, which only has a ...
Open Graph Description: Support per-type-instance vectorcall for builtin types (dict, list, int, ...) Summary Currently, all type-object constructor calls (e.g. dict(x=1), list([1,2,3]), int("42")) go through a single vec...
X Description: Support per-type-instance vectorcall for builtin types (dict, list, int, ...) Summary Currently, all type-object constructor calls (e.g. dict(x=1), list([1,2,3]), int("42")) go through a ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/7362
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"vectercall per type","articleBody":"# Support per-type-instance vectorcall for builtin types (dict, list, int, ...)\n\n## Summary\n\nCurrently, all type-object constructor calls (e.g. `dict(x=1)`, `list([1,2,3])`, `int(\"42\")`) go through a single `vectorcall_type` function, which only has a fast path for `type(x)` and falls back to the generic `PyType::call` slow path for everything else. This means every builtin type constructor pays the cost of `slot_new(args.clone())` + `slot_init(args)` dispatch, including an unnecessary `args.clone()`.\n\nCPython avoids this by giving each `PyTypeObject` its own `tp_vectorcall` function pointer. When you call `dict(...)`, CPython reads `PyDict_Type.tp_vectorcall` (= `dict_vectorcall`) directly, bypassing the generic `type.__call__` → `__new__` + `__init__` chain entirely. Over 15 builtin types have dedicated vectorcall implementations.\n\n## Current Architecture in RustPython\n\n```\ndict(x=1)\n → PyCallable::new(dict_type_obj)\n → obj.class() = type (metatype)\n → type.slots.vectorcall = vectorcall_type\n → vectorcall_type: not type(x), so fallback\n → PyType::call(dict_type, args)\n → slot_new(args.clone()) ← unnecessary clone\n → slot_init(args)\n```\n\nEach `PyType` instance already has its own `slots: PyTypeSlots` with `vectorcall: AtomicCell\u003cOption\u003cVectorCallFunc\u003e\u003e`, but the dispatch path never reads it. `vectorcall_type` receives the type object as its first argument but ignores the type's own `slots.vectorcall`.\n\n## Proposed Solution\n\n### 1. Modify `vectorcall_type` to dispatch per-type vectorcall\n\nIn `crates/vm/src/builtins/type.rs`, add a branch that checks the called type's own `slots.vectorcall`:\n\n```rust\nfn vectorcall_type(...) -\u003e PyResult {\n let zelf: \u0026Py\u003cPyType\u003e = zelf_obj.downcast_ref().unwrap();\n\n if zelf.is(vm.ctx.types.type_type) {\n // type(x) fast path (existing)\n ...\n } else if let Some(type_vc) = zelf.slots.vectorcall.load() {\n // Per-type vectorcall: dict_vectorcall, list_vectorcall, etc.\n return type_vc(zelf_obj, args, nargs, kwnames, vm);\n }\n\n // Fallback to PyType::call\n ...\n}\n```\n\nThe `else if` structure prevents infinite recursion: when `zelf` is `type_type` itself, we never read `zelf.slots.vectorcall` (which would be `vectorcall_type` again).\n\n### 2. Clear vectorcall on `__init__`/`__new__` override\n\nIn `crates/vm/src/types/slot.rs`, when `update_slot` processes `TpInit` or `TpNew` with `ADD=true`, also clear `self.slots.vectorcall.store(None)`.\n\nThis ensures subclasses that override `__init__` or `__new__` don't inherit an incorrect per-type vectorcall. For example, `class MyDict(dict): def __init__(self, ...): ...` must NOT use `dict_vectorcall`, which would skip the Python `__init__` override.\n\n### 3. Implement and register per-type vectorcall functions\n\nEach builtin type gets a dedicated vectorcall function registered in its `init()`:\n\n| Type | File | Pattern |\n|------|------|---------|\n| `dict` | `builtins/dict.rs` | DefaultConstructor + Initializer (skip `slot_new`, pass args only to `slot_init`) |\n| `list` | `builtins/list.rs` | Constructor |\n| `tuple` | `builtins/tuple.rs` | Constructor |\n| `int` | `builtins/int.rs` | Constructor |\n| `float` | `builtins/float.rs` | Constructor |\n| `str` | `builtins/pystr.rs` | Constructor |\n| `bool` | `builtins/bool_.rs` | Constructor |\n| `set` | `builtins/set.rs` | DefaultConstructor + Initializer |\n| `frozenset` | `builtins/set.rs` | Constructor |\n\nThe key optimization for `DefaultConstructor + Initializer` types (like `dict`, `set`) is avoiding the `args.clone()` in `PyType::call` line 2216 — since `Default::default()` needs no args, we construct the object first, then pass args only to `slot_init`.\n\n## Inheritance Behavior\n\n- Vectorcall is already inherited alongside `call` via `copyslot_if_none` in `slot_defs.rs` (lines 574-577)\n- `class MyDict(dict): pass` → inherits `dict_vectorcall` ✓\n- `class MyDict(dict): def __init__(self, ...): ...` → vectorcall cleared to `None`, falls back to `PyType::call` ✓\n- Custom metaclass with `__call__` override → vectorcall cleared on metaclass, per-type vectorcall never reached ✓\n\n## Key Files\n\n- `crates/vm/src/builtins/type.rs` — `vectorcall_type` dispatch modification\n- `crates/vm/src/types/slot.rs` — `update_slot` vectorcall clearing for TpInit/TpNew\n- `crates/vm/src/types/slot_defs.rs` — `copyslot_if_none` inheritance (already correct)\n- `crates/vm/src/protocol/callable.rs` — `PyCallable::new` (no changes needed)\n- Individual builtin type files for vectorcall implementations\n\n## References\n\n- [PEP 590 – Vectorcall: a fast calling protocol for CPython](https://peps.python.org/pep-0590/)\n- CPython `Objects/typeobject.c`: `type_vectorcall`, `inherit_special`\n- CPython `Include/internal/pycore_call.h`: `_PyVectorcall_FunctionInline`, `_PyObject_VectorcallTstate`","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-03-05T19:54:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/7362/RustPython/issues/7362"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6018f7db-761f-1f85-790e-39ee9ba91930 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A4C0:1712DC:253685F:3051481:6A5C9B19 |
| html-safe-nonce | 0622091d6acbd970b3e597df02e0df6fe55a8c8edbb3681b92cfd3f60991d347 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNEMwOjE3MTJEQzoyNTM2ODVGOjMwNTE0ODE6NkE1QzlCMTkiLCJ2aXNpdG9yX2lkIjoiMTc3MTMyNjc5MzM0MzM0MzM4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cd852552f250776f6cca09b0fe8c2517e14490e3e6b0118f4c4fbb840ab8dd97 |
| hovercard-subject-tag | issue:4030270811 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/7362/issue_layout |
| twitter:image | https://opengraph.githubassets.com/798dc7470d1f01d237b9b0baf2d3f62655084e3ad3922372e87a53ce2b91938f/RustPython/RustPython/issues/7362 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/798dc7470d1f01d237b9b0baf2d3f62655084e3ad3922372e87a53ce2b91938f/RustPython/RustPython/issues/7362 |
| og:image:alt | Support per-type-instance vectorcall for builtin types (dict, list, int, ...) Summary Currently, all type-object constructor calls (e.g. dict(x=1), list([1,2,3]), int("42")) go through a single vec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | youknowone |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width