You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
declare cdef extension types for hot classes via .pxd files
Add Cython .pxd declaration files and @cython.cclass decorators so that
BaseInstr, Instr, ConcreteInstr and InstrLocation are compiled as C
extension types (cdef class) instead of regular Python classes.
Without typed declarations, every attribute access on these objects went
through _PyObject_GenericGetAttrWithDict (Python's generic slot/dict
lookup), which dominated the native CPU profile at ~19% combined.
With cdef public attributes declared in .pxd files, Cython generates
direct C struct field access, eliminating the dict lookup chain entirely.
Changes:
- src/bytecode/instr.pxd: declares InstrLocation, BaseInstr, Instr as
cdef classes with public C-level attributes
- src/bytecode/concrete.pxd: declares ConcreteInstr(BaseInstr) with
_extended_args and _size as cdef public fields
- @cython.cclass added to InstrLocation, BaseInstr, Instr, ConcreteInstr
in their .py files (no-op when Cython not installed)
- BaseInstr drops Generic[A] base (incompatible with cdef class); adds
__class_getitem__ so BaseInstr[int] syntax still works for annotations
- object.__new__ replaced with cls.__new__ throughout fast-path
constructors (copy, _from_trusted, _from_opcode, _from_tuple)
- InstrLocation.__init__ and _from_tuple branch on cython.compiled to
use direct assignment in compiled mode vs object.__setattr__ in pure
Python (where @DataClass(frozen=True) is still in effect)
- .pxd files are included in Cython wheel builds only (package_data)
- setup.py: cython added as unconditional setup_requires so import cython
is always available; annotation_typing left False to avoid treating
function parameter annotations as C types
- pyproject.toml: mypy ignores errors in the affected modules since
dropping Generic[A] from BaseInstr cascades type errors on this branch
Throughput analysis:
| Build | r/s range | median |
|---|---|---|
| Pure Python 3.14 | 125–130 | ~129 |
| Cythonized 3.14 (before) | 130–134 | ~133 |
| Cythonized 3.14 (after) | 153–163 | ~161 |
The Cython speedup over pure Python went from ~3% to ~25%.
0 commit comments