Skip to main content

RustModelSerializer: DRF ModelSerializer read accelerated by Rust (PyO3)

Project description

drf-rust

37× Faster Serialization for Django REST Framework

Drop-in Rust acceleration for Django REST Framework's ModelSerializer serialization process.

Just replace:

ModelSerializer  RustModelSerializer

No other changes required.

Works with:

  • serializers
  • views
  • pagination
  • filtering
  • URL configs

Everything continues to work exactly the same.


Benchmark

Tested on a production PostgreSQL database with a 3-level nested serializer.

Rows DRF drf-rust Speedup
100 0.042s 0.019s 2.2x
400 0.888s 0.035s 25.4x
800 1.775s 0.056s 31.7x
1,200 2.541s 0.079s 32.2x
2,400 5.090s 0.144s 35.3x
4,800 10.356s 0.278s 37.3x
9,600 20.442s 0.558s 36.6x
19,200 39.753s 1.208s 32.9x
38,400 79.135s 4.145s 19.1x

Key takeaway

  • Up to 37x faster

Example

Before

from rest_framework import serializers

class ArticleSerializer(serializers.ModelSerializer):
    author_name = serializers.CharField(source="author.name")

    class Meta:
        model = Article
        fields = ["id", "title", "author_name"]

After

from drf_rust.serializers import RustModelSerializer

class ArticleSerializer(RustModelSerializer):
    author_name = serializers.CharField(source="author.name")

    class Meta:
        model = Article
        fields = ["id", "title", "author_name"]

That's it.


Why DRF Serialization Is Slow

DRF serialization suffers from several performance issues:

1. Python per-row processing

DRF serializes each object individually.

for obj in queryset:
    serializer.to_representation(obj)

For large querysets this becomes extremely expensive.


2. Nested serializer N+1 queries

Nested serializers often trigger extra database queries.

Example:

Order
 └── user
      └── organization
           └── name

DRF may execute many queries instead of one JOIN.


3. Python attribute resolution overhead

Every field requires:

  • attribute lookup
  • field conversion
  • serializer processing

This overhead accumulates quickly.


How drf-rust Works

drf-rust moves serialization into compiled Rust code.

Pipeline:

Django View
     │
     ▼
RustModelSerializer
     │
     ▼
Field classification
     │
     ▼
SQL generation
     │
     ▼
Rust execution engine
     │
     ▼
Database
     │
     ▼
Python list[dict]

Key improvements:

  • SQL JOIN flattening
  • zero N+1 queries
  • compiled row processing
  • GIL released during I/O

Features

Accelerated fields

  • CharField
  • IntegerField
  • DecimalField
  • BooleanField
  • DateTimeField
  • ...

Supported relationships

  • ForeignKey
  • OneToOne
  • ManyToMany
  • deep join chains

Example:

source="author.department.company.name"

Nested serializers

Nested serializers are automatically optimized into SQL JOINs.


Python-only fields

Some fields are handled by DRF normally:

  • SerializerMethodField
  • source='*'
  • callable sources

These are filled after Rust returns results.


Automatic Fallback

If Rust fails for any reason:

drf-rust → fallback to native DRF

Your API will still work.


Installation

Install from source

git clone https://github.com/john-tanner-dev/drf-rust
cd drf-rust

pip install maturin
maturin develop --release

Requirements

  • Python ≥ 3.8
  • Django ≥ 3.0
  • djangorestframework ≥ 3.12
  • Rust toolchain
  • maturin ≥ 1.0

Supported Databases

Database Status
PostgreSQL
MySQL
SQLite

Roadmap

Planned improvements:

  • ManyToMany optimization
  • async database execution
  • query planner
  • serializer caching
  • Running speed increased to 50X of DRF

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

drf_rust-0.1.1.tar.gz (76.1 kB view details)

Uploaded Source

Built Distributions

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

drf_rust-0.1.1-cp38-abi3-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.8+Windows x86-64

drf_rust-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view details)

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

drf_rust-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file drf_rust-0.1.1.tar.gz.

File metadata

  • Download URL: drf_rust-0.1.1.tar.gz
  • Upload date:
  • Size: 76.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for drf_rust-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c356f0919e0b21f144706c76ac1df0795bab06dc98408c2aa403af67223b6b38
MD5 6ae460fef4f94a2e6030b00fc4250ab6
BLAKE2b-256 4405102d4b00d0edc09e64fbb2870c8dd77b403470084752283af7217a084617

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_rust-0.1.1.tar.gz:

Publisher: release.yml on john-tanner-dev/drf-rust

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

File details

Details for the file drf_rust-0.1.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: drf_rust-0.1.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for drf_rust-0.1.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a3e71feb7ad76eb5600e2f13dc68fb74b667eb7ff4eef472b61d0b03c62e3925
MD5 7b533256b0b099d761199de9ef153c95
BLAKE2b-256 1c1ffd3ad75296f9ee69dc6a7c5f5ddb5558ae70ca4eaa0c85153439055df349

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_rust-0.1.1-cp38-abi3-win_amd64.whl:

Publisher: release.yml on john-tanner-dev/drf-rust

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

File details

Details for the file drf_rust-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for drf_rust-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a40c6ed5918758d9f4219dce681294555ee05ea9e4b3d1cb04d27e64d1b5e64
MD5 9e5de9dd156fc27690dc673fcdaef1e0
BLAKE2b-256 10b48c52dfb66261a2412fdac8fa4a5c9a935956925709ae5b2adf8ad07e5330

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_rust-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on john-tanner-dev/drf-rust

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

File details

Details for the file drf_rust-0.1.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for drf_rust-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e4d2094c7b42890e58bb9ebfffecd2bf42ddafd3d04dee81f2697043702dd015
MD5 2766a2190e23c29836e7fe042c16175d
BLAKE2b-256 46f8d2c6a293c5eb35d1d7165e7062c9c6d24ddfd502b70cf2ae57ace02dbf2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_rust-0.1.1-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on john-tanner-dev/drf-rust

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