Skip to content

About

Optional Rust (PyO3) accelerator for graphql-core execution

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

graphql-core-speedups

An optional Rust (PyO3) accelerator for graphql-core execution. Plug it in with one argument; results are graphql-core's own.

Status: 0.3.1 is the current release. Tested with graphql-core 3.3.1 and 3.3.2. 0.1.0 was synchronous only and native per whole object; since 0.3.0 the library handles mixed objects, native async execution, framework markers and is_type_of, and calls custom resolvers from the native walk (see Changelog). Measured results: 0.3.0 and v0.1. Design: RFC 0001.

Documentation: https://eamigo86.github.io/graphql-core-speedups/

What to expect

How much faster depends on how much of the response uses graphql-core's default resolver. Each custom resolver hands control back to Python.

Measured on 0.1.0.

Workload Faster than plain graphql-core
Large response from graphql-core#189: 5,000 persons, every field on the default resolver, non-null types, dataclass objects 13.4–14.5×
Small query on the same schema (one person, 17 fields) 4.3–4.4×
The large query with a custom resolver on Person.name and Pet.name about 1.5×
Operations that cannot use the native path (introspection: every field has a resolver) no faster, at most 1.5 % slower

0.3.1. Measured with and without FastExecutor in every framework (0.3.1 results); Linux x86-64, three GitHub-hosted runs of one batch (graphql-core-speedups 0.3.1, django-graphex at 24f9081, the code released as 4.6.1, installed from GitHub), CPython 3.12.16, graphql-core 3.3.2. Each cell is the range over the three runs; a range marked * includes a reading with an arm whose IQR is above 10 %. The machines, the commands and the native flag are in per framework:

Framework Entry point Large Small Mixed
graphql-core execute_sync / execute (async) 27.30–34.21× / 16.15–18.77× 3.96–4.18× / 2.69–3.15× 11.84–12.77× / 9.53–9.94×
Ariadne graphql_sync / graphql (async) 27.35–30.24× / 15.86–16.88× 1.06× / 1.05–1.06× 9.60–10.51× / 7.95–8.86×
Litestar sync + sync_to_thread / async 20.47–23.03× / 13.80–14.58× 1.02× / 1.03× not measured
FastAPI def + graphql_sync / async def 11.80–16.86×* / 9.71–11.67× 1.02–1.07× / 0.96–1.08×* not measured
Django in memory sync view / async view 13.28–14.82×* / 10.29–11.08× 1.05× / 1.03–1.04× not measured
Django + ORM (control) sync view / async view 2.36–2.49× / 2.27–2.54× 1.03× / 1.03× not measured
Strawberry (a) as shipped execute_sync / execute (async) 0.98–1.00× / 0.98–1.00× 0.99–1.01× / 0.99× 1.00–1.02× / 0.96–0.99×
Strawberry (b) markers applied by the harness execute_sync / execute (async) 14.49–16.86× / 14.66–15.69×* 1.04–1.05× / 1.04–1.05× 5.28–5.57× / 5.29–5.72×
django-graphex GraphQLView 1.92–2.35×* 1.05× 1.99–2.33×*

Django + ORM and django-graphex load the root from in-memory SQLite on every request; every other row reads a Python object in memory.

django-graphex page size. The harness raises django-graphex's DEFAULT_PAGE_SIZE from 100 to 5,000 so the list returns all 5,000 persons, the same load as the other frameworks. A deployment with the default returns 100 rows per page; at the default, an earlier batch (results) read 1.564–1.660× on large.

Small queries in web frameworks gain little because parsing and validation dominate. Strawberry gains nothing as shipped (0.96–1.02× across large, small and mixed, within noise). Django gains 10.29–14.82× on large with in-memory data but the Django + ORM control only 2.27–2.54×: loading 5,000 persons through the ORM costs 161.7–202.6 ms without FastExecutor and 155.7–200.2 ms with it, which FastExecutor does not touch. django-graphex gains 1.92–2.35× on large, bounded by the same ORM load: with FastExecutor, about two thirds of its request is the ORM (63.7–66.2 %) (profile). The Apple M3 Max table, and the django-graphex 4.4.0, PR #271 and PR #273 rows, are on the results page. The reason for each row is in Why each case gains, or not.

  • Ranges, one machine. The released ranges span the v0.1 runs: three of the large and small cases, two of the others, each interleaved with plain graphql-core in the same process. All of them ran on one Apple M3 Max with CPython 3.12.11. On Linux x86-64 (three GitHub-hosted runners, CPython 3.12.16) the same cases gave 14.7–18.0× (large), 4.3–4.6× (small) and about 1.5× (mixed); see the results page. The 0.3.1 framework table above is the Linux one, over three GitHub-hosted runs, with django-graphex at 24f9081 (the code released as 4.6.1) at 1.92–2.35× large (Linux, per framework).
  • Not 15×. The spike prototype once measured 15.67× on the large response. The same prototype code measures 13.7–13.8× in a later session, so that figure was session variance.
  • Mixed schemas. In 0.1.x, custom resolvers on just two fields (Person.name, Pet.name) brought the large case from about 14× down to 1.5×. In 0.3.0 the same case is 4.35× in bench.compare (its fast IQR was 58.5 %, so treat it as noisy) and 11.75–11.97× in the framework harness for execute_sync (Linux, 0.3.1).

Usage

from graphql import execute_sync, graphql_sync
from graphql_core_speedups import FastExecutor

result = execute_sync(schema, document, root_value, executor_class=FastExecutor)
result = graphql_sync(schema, source, root_value, executor_class=FastExecutor)

FastExecutor is a graphql-core IncrementalExecutor. If the compiled extension cannot be imported, it warns once and behaves exactly like IncrementalExecutor.

What runs natively

0.3.0. Custom resolvers are called from the native walk: a field with a callable resolver, with or without arguments, is called by the walk, and its result is completed natively. Only fields of abstract (interface or union) type are still handed to graphql-core's execute_field. The rest of this section describes 0.2.0, which 0.3.0 builds on.

0.2.0. An object is completed in Rust when at least one selected field is natively readable: it takes no arguments and uses graphql-core's default resolver (resolve is None or default_field_resolver), or its resolver carries a marker saying it is a plain attribute read. Every other field of the object (custom resolver, arguments, abstract type) is handed to graphql-core's own execute_field. An object whose selected fields are all delegated stays in graphql-core.

  • is_type_of is called natively, once per object.
  • Async execution is native: the executor accepts graphql-core's own is_awaitable (execute(), ASGI apps, async def routes), the execute_sync sentinel and hooks marked ("is_awaitable", "builtins_not_awaitable"), and completes awaitables like graphql-core.
  • Marked resolvers: ("attr", name, "dict_or_getattr_none") and ("attr", name, "getattr_strict"), read natively. See Markers.
  • Leaves are any scalar or enum. String, Int, Float, Boolean, ID and plain enums have native fast paths; other values go through graphql-core's own complete_leaf_value.

0.1.x. An object was native only when all its fields used the default resolver with no arguments and it had no is_type_of, and only synchronous execution was native.

In both versions the native path is off for the whole execution with middleware, a custom default field resolver, an abort signal, an unsupported is_awaitable hook, or a document with directives on fields or fragments (operation-level directives such as @experimental_disableErrorPropagation are fine). Everything else runs in graphql-core unchanged. When an operation cannot use the native path at all, the executor switches to graphql-core's own object and list completion. That keeps the cost within the 5 % gate: 0.6–1.5 % slower on introspection across the v0.1 runs.

Errors are completed natively too: on every error path the native walk hands the value it already read to graphql-core's own methods, so messages, paths, locations, ordering and null propagation are graphql-core's, and no attribute is read twice.

Correctness

  • Hypothesis differential test (tests/test_differential.py): this is the main evidence for the native path. It draws schemas, data and queries, and requires identical data, errors, key order, and logs of attribute reads and resolver calls.
  • graphql-core's own tests/execution suite runs with FastExecutor as the default executor (sh tests/upstream/run.sh) for every tested release: 3.3.2 passes 738/738 and 3.3.1 688/688, no differences. Most of those tests use custom resolvers: 0.1.x reached the native path 26 times in 3.3.2, 0.2.0 124 times (3.3.2) and 123 times (3.3.1). The suite mainly shows that everything else behaves exactly as graphql-core.
  • Known difference: exception __context__ chaining. Native error boundaries are not inside an except block. Error objects, messages, paths and locations are identical.

Subclasses and frameworks

A framework that needs its own executor class can be combined with FastExecutor:

class Executor(FastExecutor, FrameworkExecutor): ...

When an operation cannot use the native path, such a subclass also switches to graphql-core's own object and list completion, and keeps its other overrides. A subclass that overrides complete_object_value or complete_list_value itself keeps FastExecutor's hook, because its override must see every call. It pays for that on operations that cannot use the native path: 1.26× on introspection for the hook alone, before the cost of its own override.

  • Ariadne leaves resolve=None on plain fields, so its schemas qualify: native with graphql_sync in 0.1.0, and with the async graphql() and the ASGI app in 0.2.0.
  • Strawberry does not reach the native path (its is_awaitable hook and basic-field resolvers are unmarked), in 0.1.0, 0.2.0 or 0.3.0: 0.96–1.02× in the 0.3.1 Linux measurement. Combined as above, it is no slower than Strawberry alone (0.1.0 gate: 1.01×, 1,000 people with 3 friends each).
  • django-graphex 4.4.0 did not reach the native path: it had no markers (0.995× large on the M3 Max). Its 4.5.0 release ships the markers (PR #271) and the middleware scoping (PR #273), and 4.6.0 marks forward relations (PR #278); it needs graphql-core-speedups 0.3.0 or later (0.3.1 is faster for alternating object types). At 24f9081 (the code released as 4.6.1, installed from GitHub), with middleware=[] and its page size raised to 5,000, it gains 1.92–2.35× large on Linux; the ORM load and its own layer dominate (about two thirds of its request with FastExecutor is the ORM). The PR figures below were measured on the PR branches, not on a released package (results page). With the markers and middleware=[]: 1.667× on a model schema at the default page size, 1.11× on its playground (large). With the playground's 3 middleware entries there is no native path: any middleware disables it. With the middleware scoping (PR #273) the playground's real settings open the native path: FastExecutor adds 1.103× large, on top of 1.431× that the scoping alone gives, which is django-graphex's gain.
  • graphene does not support graphql-core 3.3.

Details: Integrations, 0.3.0 results and docs/v0.1/results.md.

Development

uv sync                                    # builds the extension (release profile)
# For benchmarks on 3.12+, build what pip installs there (the cp312-abi3 wheel):
# MATURIN_PEP517_ARGS="--no-default-features --features abi3-py312" uv sync --reinstall-package graphql-core-speedups
uv run pytest -q
HYPOTHESIS_PROFILE=ci uv run pytest -q tests/test_differential.py
sh tests/upstream/run.sh
uv run python -m bench.compare             # interleaved plain-vs-fast timings and verdicts

License

MIT.

About

Optional Rust (PyO3) accelerator for graphql-core execution

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages