feat: support jitclass for native codon jit class#809
Closed
LeeLee26 wants to merge 2 commits into
Closed
Conversation
Author
|
closed new RFC: RFC: Native-backed Python Classes for Codon JIT via PR Stack: |
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.
Proposal: Native-backed Python Classes for Codon JIT via
codon.jitclassBackground
Codon already provides
codon.convertas a way to expose Python classes to Codon JIT-compiled functions. This mechanism is useful for simple interoperability scenarios: a Python object can be converted into a Codon-compatible representation, passed into compiled code, and used from JIT functions.However,
codon.converthas several inherent limitations when the goal is to model Python classes as native Codon objects:Object identity and lifetime are not naturally preserved
codon.convertprimarily treats Python objects as values that can be converted into Codon-side data structures. This works well for immutable or value-like objects, but it is not ideal for class instances that are expected to behave like long-lived mutable objects.Mutation semantics are limited
For Python classes with fields, users often expect operations such as:
to operate on the same underlying object. With conversion-based interop, field updates may not naturally map to persistent native state unless the object is explicitly reconstructed or round-tripped.
Methods are not represented as native class methods
codon.convertallows converted objects to be consumed by JIT functions, but it does not make the Python class itself a native Codon class with compiled methods, constructors, and field accessors. This limits the ability to express object-oriented workloads directly in the JIT layer.Performance opportunities are left on the table
A conversion-based approach introduces boundaries between Python objects and Codon values. For class-heavy workloads, repeatedly converting objects or routing behavior through Python-level wrappers can reduce the performance benefits expected from JIT compilation.
The user model is less direct
Users who write normal Python classes usually expect a decorator-based workflow where the class itself becomes compiled, rather than manually converting instances or writing separate JIT functions around them.
Because of these limitations, we propose adding first-class support for JIT-compiled classes through
codon.jitclass.Proposal
We propose adding
codon.jitclass, a decorator that compiles a Python class into a native Codon class and exposes it through a Python proxy object.Example:
The class definition is compiled by Codon JIT. Python receives a lightweight object whose data and methods are backed by a native Codon instance.
Key Capabilities
codon.jitclasssupports:close()and context managers.Internally, each instance is stored in the JIT runtime using a handle. Method calls and field access are dispatched to compiled Codon wrappers.
The implementation also keeps native objects safe by rooting them in Codon’s GC while they are reachable from Python.
Benefits
This feature extends Codon JIT from function-level acceleration to class-level acceleration.
Compared with
codon.convert,codon.jitclassprovides:Summary
codon.convertremains useful for converting Python values into Codon-compatible objects.codon.jitclassaddresses a different use case: compiling Python classes themselves into native Codon classes, while preserving a Python-friendly interface.This makes Codon JIT more expressive and better suited for stateful, object-oriented code.