Skip to content

Cythonise all sources#1

Open
P403n1x87 wants to merge 18 commits into
mainfrom
cythonize
Open

Cythonise all sources#1
P403n1x87 wants to merge 18 commits into
mainfrom
cythonize

Conversation

@P403n1x87
Copy link
Copy Markdown
Owner

For better performance

P403n1x87 and others added 5 commits May 8, 2026 09:59
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%.
P403n1x87 and others added 8 commits May 11, 2026 17:24
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants