Faster JSON Mapping with Just-In-Time codegen
Project description
Snaplet
Faster JSON Mapping, the only dependency is orjson.
Features
- Just-In-Time (JIT) Codegen:
Snaplet generates specialized property accessors at runtime using
exec(). By eliminatinggetattrcalls and internal dictionary lookups during access, it achieves near-native attribute access speeds. - Zero-Copy Lazy Instantiation: Nested models and lists are only instantiated when accessed. This minimizes the performance impact when processing large JSON payloads where only specific fields are required.
- High-Throughput Bulk Loading:
Bypasses the standard
__init__constructor and leverages__slots__with__new__to mass-produce instances. This enables high-speed loading by reducing Python-level overhead during object creation. - Explicit Validation Control: Snaplet does not perform eager validation on load. This design allows developers to implement custom validation logic only where necessary, avoiding global performance bottlenecks.
- Transparent Alias Mapping:
Supports
AnnotatedandField(alias=...)to map complex JSON keys to Pythonic names. JIT compilation ensures that using aliases does not incur additional runtime performance costs. - Minimal Dependency:
The only requirement is
orjson. It operates as a pure Python library utilizing JIT logic, ensuring high portability across different environments without the need for complex build chains.
Installation
uv add snaplet
Usage
from typing import Annotated
from snaplet import SnapletBase
from snaplet import Field
class User(SnapletBase):
user_id: int
internal_id: Annotated[int, Field(alias="ID")]
tags: Annotated[list[str], Field(alias="Tags-List-V1")]
data = {"userId": 1, "ID": 999, "Tags-List-V1": ["python", "snaplet"]}
user = User(data)
print(user.internal_id)
print(user.user_id)
You can disable JIT with jit=False but, this is not recommended as it may significantly degrade performance.
class User(SnapletBase, jit=False):
user_id: int
internal_id: Annotated[int, Field(alias="ID")]
tags: Annotated[list[str], Field(alias="Tags-List-V1")]
Performance
Snaplet is designed for high-performance applications that handle large JSON datasets with minimal overhead.
Benchmark (1,000 items)
Measured on Python 3.12.
| Operation | Time | Notes |
|---|---|---|
| Bulk Load | ~108 μs | Using orjson + __new__ bypass |
| Attribute Access | ~115 ns | Powered by JIT Property Access |
| Lazy Export (dict) | ~165 ns | Zero-cost for unaccessed fields |
Why so fast?
- JIT Property Access: Specialized property accessors are compiled at runtime using
exec(), eliminating branch overhead. - Lazy Instantiation: Nested objects are only instantiated when accessed.
- Minimal Overhead: Bypassing
__init__during bulk loading to achieve near-native speeds.
LICENSE
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snaplet-0.1.1.tar.gz.
File metadata
- Download URL: snaplet-0.1.1.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2915d51e466a2eb32f3760aeafc35f67f7f90070cacb8697f766cbaa2cfb7f44
|
|
| MD5 |
140942dbcbeda532578fedc6ee6cb4a1
|
|
| BLAKE2b-256 |
2a6372594dd85b895056f7763ff43b3c13c65d81931f4622a468f654c9b2a8db
|
Provenance
The following attestation bundles were made for snaplet-0.1.1.tar.gz:
Publisher:
publish.yml on AmaseCocoa/snaplet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaplet-0.1.1.tar.gz -
Subject digest:
2915d51e466a2eb32f3760aeafc35f67f7f90070cacb8697f766cbaa2cfb7f44 - Sigstore transparency entry: 1005170544
- Sigstore integration time:
-
Permalink:
AmaseCocoa/snaplet@d2e63016e204e808175537e01ca3546f5714a042 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/AmaseCocoa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d2e63016e204e808175537e01ca3546f5714a042 -
Trigger Event:
release
-
Statement type:
File details
Details for the file snaplet-0.1.1-py3-none-any.whl.
File metadata
- Download URL: snaplet-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a41906bb0907fdc40557150de3d31dc737604c118a154cc69aebbb751769d6cb
|
|
| MD5 |
ac2721401498feba56c78ea18c7972cf
|
|
| BLAKE2b-256 |
566028c3836ea2161b1b828c7df55c9da1d71b69bbbc9ccebddb72977e6ed48c
|
Provenance
The following attestation bundles were made for snaplet-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on AmaseCocoa/snaplet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaplet-0.1.1-py3-none-any.whl -
Subject digest:
a41906bb0907fdc40557150de3d31dc737604c118a154cc69aebbb751769d6cb - Sigstore transparency entry: 1005170548
- Sigstore integration time:
-
Permalink:
AmaseCocoa/snaplet@d2e63016e204e808175537e01ca3546f5714a042 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/AmaseCocoa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d2e63016e204e808175537e01ca3546f5714a042 -
Trigger Event:
release
-
Statement type: