Conversation
86db41f to
217b80c
Compare
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 for the declared fields. 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 Native CPU profile (~4.2 kHz sampling, ~6k samples, Python 3.14.4): | Hotspot | Before | After | |---|---|---| | `_PyObject_GenericGetAttrWithDict` own | 5.47% | 4.28% | | `_PyObject_GenericGetAttrWithDict` total | 19.74% | 13.20% | | `PyMember_GetOne` (slot access) | 1.77% | eliminated | 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%.
In Cython cdef class, a property without a deleter raises NotImplementedError instead of Python's AttributeError. Add an explicit deleter to BaseInstr.arg that raises AttributeError to keep consistent behaviour across pure Python and Cython builds.
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.
For better performance