Skip to main content

Performance helpers for strawberry-django: skip resolver overhead on sync Django.

Project description

fastberry

Performance helpers for strawberry-django GraphQL schemas running under synchronous Django.

strawberry-django wraps every field resolver in django_resolver, which calls in_async_context() on each resolution. Under sync Django that call raises and catches a RuntimeError internally (~0.15 ms per field). On large result sets this dominates response time — pure CPU overhead with zero extra database queries.

fastberry lets you opt specific "hot" types out of that machinery.

Install

pip install fastberry

Requires Python 3.10+, Django 4.2+, and strawberry-graphql.

Usage

Mark hot types with @fast_path (outermost, after @strawberry_django.type) and register FastPathExtension on the schema:

import strawberry
import strawberry_django
from fastberry import fast_path, FastPathExtension

from myapp.models import Stock


@fast_path
@strawberry_django.type(Stock, disable_optimization=True)
class StockType:
    id: int
    title: str


@strawberry.type
class Query:
    stocks: list[StockType] = strawberry_django.field()


schema = strawberry.Schema(
    query=Query,
    extensions=[FastPathExtension],
)

Only types decorated with @fast_path take the fast path; everything else resolves normally. To turn the optimization off, remove the extension (global) or the decorator (per type).

How it works

  • Plain fields resolve via a direct getattr(root, attr). Related managers (*-to-many) are materialized with .all().
  • Custom resolvers are called directly, bypassing the django_resolver wrapper.

The field→resolver map is built once, at class-definition time, and keyed by the GraphQL type name (info.parent_type.name) — not the Python class — because at resolve time root is the Django model instance, not the Strawberry type.

Benchmarks

Indicative numbers from a real schema (~3000 objects, ~27 fields each):

Scenario Without fast-path With fast-path Speedup
stocks_by_house 4.4 s 1.8 s ~2.4×

Your mileage will vary with field count and result-set size.

Caveats

  • Built for sync Django execution. Async schemas don't hit the overhead this targets.
  • @fast_path does its own field resolution, so it skips any custom logic that django_resolver would otherwise apply. Use it on read-heavy, plain types; keep complex types on the default path.

Links

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

fastberry-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

fastberry-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file fastberry-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for fastberry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 577ecad08b82fc022775f0449cacbcb7e43a3dc7cd30a9044e2ff1f59103cc6d
MD5 dc591fc1d92a8312bef7c05448022deb
BLAKE2b-256 5ac6320ac1e64ccb9ecb99d71b10dbd683d55e764a5a134ff2bc88871bda48c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastberry-0.1.0.tar.gz:

Publisher: release.yml on davidsarosi92/fastberry

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

File details

Details for the file fastberry-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fastberry-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastberry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 177a7cf184ae9c43558244f4fd5a2ac37125dda454266fa6fa2fe6b8c9c35a6e
MD5 ee66bc56d24de4885013f67e06dbbd95
BLAKE2b-256 991b3438ea2a880c7b4c2bf8d19bd0b8ab51bbbf041e5a3a819a07a29708b2d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastberry-0.1.0-py3-none-any.whl:

Publisher: release.yml on davidsarosi92/fastberry

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