Repository navigation
perf(python): pass the module to pyfunctions to keep CPython 3.14 call specialization - #58
Merged
Merged
Conversation
📊 Benchmark Comparison Results
➖ 81 benchmarks within noise (click to expand)
Legend: ✅ Faster (>5%) · |
mrecachinas
force-pushed
the
perf/pyo3-pass-module
branch
3 times, most recently
from
September 23, 2026 02:08
f8056ee to
73c0b0b
Compare
…l specialization PyO3 tags every `#[pyfunction]` without `pass_module` as METH_STATIC (ml_flags 0xa2). CPython 3.14 still specializes calls to them as CALL_BUILTIN_FAST_WITH_KEYWORDS (the specializer masks METH_STATIC off), but the specialized op's guard compares the full flags against METH_FASTCALL | METH_KEYWORDS, so every call deopts to the generic PyObject_Vectorcall path. Profiles showed `PyObject_Vectorcall` and `cfunction_vectorcall_FASTCALL_KEYWORDS` frames only for PyO3 functions. Taking the (unused) module drops METH_STATIC (flags 0x82). The only Python-visible difference is `__self__` becoming the extension module and `__text_signature__` gaining `$module`, as with CPython's own builtins such as `len`; `inspect.signature`, keyword arguments, return values, and error messages are unchanged (checked 168 call probes across 8 functions). Interleaved A/B in one process (Apple M4 Max, CPython 3.14.7, min of 3 x 7 runs; call floor `operator.is_` = 11.8 ns): | call | before | after | | |------------------------------------|---------|---------|-------| | hamming_distance_string, 16 chars | 31.9 ns | 28.9 ns | 1.11x | | hamming_distance_string, 64 chars | 34.0 ns | 31.3 ns | 1.09x | | hamming_distance_bytes, 16 B | 29.1 ns | 26.4 ns | 1.11x | | hamming_distance_bytes, 64 B | 33.3 ns | 29.7 ns | 1.12x | | hamming_distance_bytes, bytearray | 49.7 ns | 47.0 ns | 1.06x | | check_hexstrings_within_dist, 16 | 34.9 ns | 31.2 ns | 1.12x | | check_bytes_within_dist, 16 B | 31.1 ns | 28.1 ns | 1.11x | | set_algo | 36.8 ns | 31.5 ns | 1.17x | Adds a regression test that every exported function is bound to the extension module; it fails without this change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c7edd725-5714-4044-acf4-c9afc6ec8cf8
mrecachinas
force-pushed
the
perf/pyo3-pass-module
branch
from
September 23, 2026 11:59
73c0b0b to
6472976
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Finding: PyO3 0.29 gives every
#[pyfunction]withoutpass_modulethe flagMETH_STATIC(ml_flags = 0xa2). CPython 3.14'sCALL_BUILTIN_FAST_WITH_KEYWORDSspecialization rejects that flag, so every call to a module function deoptimizes to the genericPyObject_Vectorcallpath.Fix: all 17 module functions now take
pass_module(ml_flags = 0x82), which keeps the specialized call path. A regression test checks that each function's__self__is the extension module; the test fails without this change.Interleaved A/B in one process (Apple M4 Max, CPython 3.14, ns/call):
hamming_distance_string16 charshamming_distance_bytes16 Bset_algoThe saving is about 3 ns per call on every function.