Skip to main content

Rust acceleration core for marshmallow serialization, installed as a separate, opt-in package.

Project description

marshmallow_core

CI

A Rust acceleration core for marshmallow, shipped as a separate, opt-in package. Install it next to stock marshmallow and activate it explicitly — it replaces marshmallow's per-object _serialize / _deserialize loops with a PyO3 extension while producing identical results.

pip install marshmallow marshmallow_core
import marshmallow as ma
import marshmallow_core

marshmallow_core.install()      # patch marshmallow.Schema in this process

class Person(ma.Schema):
    name = ma.fields.String()
    age = ma.fields.Integer()

Person().load({"name": "ann", "age": "30"})   # accelerated
Person().dump({"name": "ann", "age": 30})      # accelerated

marshmallow_core.uninstall()    # restore the stock pure-Python methods

How it works

  • install() monkey-patches Schema._serialize and Schema._do_load. Each bound schema is compiled once (cached on the instance) into a recursive payload describing every field as either native (run entirely in Rust) or a callback (defers to the Python Field method). Anything not modelled natively stays a callback, so output is behaviour-identical.
  • Both cores handle the happy path and raise an internal AccelFallback on any error/edge case, so marshmallow re-runs the unchanged pure-Python path and every error message and value matches exactly. (Dump has no side effects — it builds a fresh output — so it can discard a partial result and re-run safely, just like load.)
  • dumps is fused: it writes JSON bytes directly in Rust, skipping the intermediate Python dict and the json.dumps pass, byte-for-byte identical to json.dumps(schema.dump(obj)). It activates for hook-free schemas using the stdlib json render module with no extra json kwargs, and falls back to dump + json.dumps for anything it can't reproduce exactly. (loads is already accelerated through the patched load path; a Rust JSON parser was prototyped but did not beat CPython's C json.loads, so it was not shipped.)
  • Acceleration is strictly a speedup. Set MARSHMALLOW_NO_ACCEL=1 (or hit a protocol-version mismatch between the Python and Rust halves) and the core becomes a no-op even after install().

Scope / limitations

install() accelerates dump for all compilable schemas, and load for most schemas — including those with pre_load / post_load / validates / validates_schema hooks: the core runs the per-field deserialize step while those hooks run in Python around it (mirroring marshmallow's own _do_load split). Recognized field validators (Range / Length / OneOf / Equal / NoneOf / ContainsOnly) run natively; any other validator, or field-level pre_load / post_load, keeps that field on the callback path. unknown=INCLUDE, collection/dotted partial, and dotted attribute writes are all accelerated. Natively modelled fields include the scalars plus Decimal, Dict (incl. typed keys/values), Tuple, Pluck, Constant, TimeDelta, Boolean, Integer(strict=True), and NaiveDateTime / AwareDateTime. The dump core has an AccelFallback (it discards a partial result and re-runs pure Python on any shape it can't reproduce), so it accelerates the composite fields too. Custom dict_class / get_attribute, self-referential schemas, custom strptime temporal formats, and callable defaults always fall back to pure Python.

Where the speedup is limited

Some shapes are inherently bounded — the work the core can't move into Rust dominates the call. These are correct, just not where the gains are:

  • Hook-bearing loads are the weakest case (~2x, vs ~7x without hooks). When a schema has pre_load / post_load / validates / validates_schema, the core runs the per-field deserialize but marshmallow's Python hook-dispatch (_invoke_load_processors, _invoke_field_validators) runs around it. On a small schema the core step is ~8% of the load; the remaining ~90% is that Python machinery, which wraps user callbacks and cannot be moved into Rust.
  • Small / flat schemas are capped by fixed per-call overhead (~20–30%). The dump / load entry prologue (argument normalization, the per-instance serializer-cache lookup, the partial/unknown checks) is constant per call, so it dominates exactly when the payload is tiny. It amortizes to near-zero as the payload grows — speedup on a list of records is flat regardless of length.
  • loads gains less than load. JSON parsing still goes through CPython's C json.loads; a fused Rust parser was prototyped but did not beat it, so only the subsequent per-field step is accelerated.

For collections of records (the common hot path) the fixed overhead vanishes and the speedup is steady (~7–8x). Run performance/analyze_paths.py to see whether a given schema even reaches the core, and performance/benchmark.py to measure it.

Development

Requires cargo (rustup) and maturin.

# build + install the extension into the current venv
uvx maturin develop --release

# run the tests (needs marshmallow + pytest installed)
pytest

# force the pure-Python path
MARSHMALLOW_NO_ACCEL=1 pytest

tests/test_equivalence.py asserts that dump/load produce identical output and errors with the core active vs. forced onto pure Python, across scalars, nested/list/enum/temporal/UUID fields, partial=True, and error inputs.

Benchmarking

The performance/ directory (not shipped in wheels) measures the core against stock marshmallow through the public install() / uninstall() API. Run it from the repo root with the compiled extension importable (uvx maturin develop --release first, or point PYTHONPATH at the repo while the wheel is installed):

# stock-vs-core table for dump / load / dumps / loads on four schema shapes
python -m performance.benchmark                       # all cases
python -m performance.benchmark --number 20000 --only flat,list

# coverage probe: per-field native vs callback for each schema shape
python -m performance.analyze_paths

benchmark.py reports per-call microseconds for stock and core plus the speedup ratio, across flat-scalar, nested, list-heavy, and validator-heavy schemas. analyze_paths.py inspects the compiled payload and shows which fields run native in Rust vs. fall back to a Python callback — it tells you exactly where a real schema still defers to pure Python.

Releasing

CI (.github/workflows/ci.yml) builds the wheel and runs the suite against stock marshmallow on Python 3.10–3.13, both with the core active and with MARSHMALLOW_NO_ACCEL=1. Publishing (.github/workflows/release.yml) builds abi3 wheels + sdist for Linux/macOS/Windows on a v* tag and uploads them to PyPI via trusted publishing. Before the first release, configure the PyPI trusted publisher for this repo and create a pypi GitHub Environment, then push a tag (e.g. git tag v0.1.0 && git push --tags).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow_core-0.1.8.tar.gz (83.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

marshmallow_core-0.1.8-cp310-abi3-win_amd64.whl (252.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (384.6 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (386.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

marshmallow_core-0.1.8-cp310-abi3-macosx_11_0_arm64.whl (352.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file marshmallow_core-0.1.8.tar.gz.

File metadata

  • Download URL: marshmallow_core-0.1.8.tar.gz
  • Upload date:
  • Size: 83.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for marshmallow_core-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c17d247580f53efbd2384ef9b83fe99d9764304958b5b90786717b6d2cca0438
MD5 8b496d2d602b5dc3fcf61357dc2aca9b
BLAKE2b-256 c79d02ef81e4d4d770897bb5334fd978d98c45507fd956f9d6bf95f7ef0688bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_core-0.1.8.tar.gz:

Publisher: release.yml on gunlinux/marshmallow_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_core-0.1.8-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for marshmallow_core-0.1.8-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a7c6c4cd85290106838667a87840bf77386fc12c67ff8f5ee81abbabaa405d58
MD5 87d484c647e98355b48b7e5da3f2d78c
BLAKE2b-256 8536014ab777a2196a0115274af9a9b336773400e0406f50dab16b9411b4a2dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_core-0.1.8-cp310-abi3-win_amd64.whl:

Publisher: release.yml on gunlinux/marshmallow_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6255e3a0bd93ffa0c2c5b2f46f585bcc47043d391432fc99fdcfca3b2e28b915
MD5 360d9a71e2baad528126cc228a54b522
BLAKE2b-256 a181fd2ec812c25b688f3c84b93b5787889994a9e61bbf3e8994f30c5cb9b0de

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on gunlinux/marshmallow_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2e96572b73d2dec6ace7cea107904d302b566f5aa0dfee10b653897836733d5a
MD5 e8e5474e2a2b78f9c59ca762146d766a
BLAKE2b-256 1794a184e0cc5323907dd5251e3efe773e71563b155e461a85b504c07fe0fd74

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_core-0.1.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on gunlinux/marshmallow_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_core-0.1.8-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for marshmallow_core-0.1.8-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 24628640542f8d7bc92f9488293f56de42700b1710e2e0f7727c316bb57536f9
MD5 0d7b45d43208da0f61f0e4b1bfccdd29
BLAKE2b-256 0b64a609c32dfecb68ba02a6f422646ac71524e7e2a52d4e3750d31b0a543acd

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_core-0.1.8-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on gunlinux/marshmallow_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page