Skip to content

Resolve synchronous fields without futures in async execution - #1419

Merged
LegNeato merged 1 commit into
graphql-rust:masterfrom
LegNeato:sync-fast-path
Nov 9, 2026
Merged

LegNeato merged 1 commit into
graphql-rust:masterfrom
LegNeato:sync-fast-path

Conversation

@LegNeato

@LegNeato LegNeato commented Nov 1, 2026

Copy link
Copy Markdown
Member

This PR builds on #1418. Only its last commit is new. It is the last of the five stacked PRs that speed up execution.

  • The macros mark the fields without an async resolver with the new Field::synchronous() method.
  • RootNode finds the types that resolve without awaiting when it builds the schema. An interface field is synchronous if it is synchronous in all implementers. Recursive types form cycles, so the search removes candidates until no candidate changes.
  • The asynchronous executor resolves a synchronous field of a synchronous type in place with GraphQLValue::resolve_field(), without a future. Executor::resolve_async() calls GraphQLValue::resolve() for values of synchronous types.
  • The Book explains which fields resolve without futures.

With synchronous resolvers, asynchronous execution now makes as many allocations as synchronous execution, and it runs as fast. On the benchmarks of #1413 except introspection, it takes 13 to 67% less time than with #1418. With async fn resolvers, scalars and enums resolve without futures, and execution takes up to 8% less time.

The table compares master with this PR and the four PRs before it. It shows the median time per request on an Apple M1 Pro. Synchronous execution takes 17 to 69% less time than on master, and asynchronous execution takes 20 to 87% less time. With synchronous resolvers and with async fn resolvers, juniper is now faster than async-graphql and apollo-compiler on every operation.

Operation juniper_sync juniper juniper_async_fn async_graphql apollo_compiler
minimal 1.99 µs → 1.64 µs 2.42 µs → 1.66 µs 2.39 µs → 1.91 µs 4.99 µs 3.41 µs
flat_list 1.4 ms → 437 µs 3.15 ms → 436 µs 3.11 ms → 999 µs 1.87 ms 1.67 ms
nested_lists 1.16 ms → 374 µs 2.83 ms → 374 µs 2.81 ms → 1.06 ms 1.58 ms 1.61 ms
star_wars 72.7 µs → 31.6 µs 157 µs → 31.1 µs 157 µs → 64.7 µs 121 µs 112 µs
fragments 51.5 µs → 27.5 µs 94.6 µs → 27.7 µs 94.4 µs → 40.3 µs 73.7 µs 67.4 µs
introspection 453 µs → 187 µs 458 µs → 188 µs 452 µs → 185 µs 634 µs —
introspection_without_one_of 451 µs → 186 µs 455 µs → 188 µs 447 µs → 182 µs 613 µs 559 µs

Asynchronous execution created a future for every field, even if the
resolver of the field and all resolvers below it were synchronous. Each
such field paid for a boxed resolver future.

The macros mark the fields without an `async` resolver with the new
`Field::synchronous()` method. `RootNode` finds the types that resolve
without awaiting when it builds the schema. Recursive types form
cycles. The search therefore starts with all types as candidates and
removes the candidates with an asynchronous dependency until no
candidate changes. An interface field is synchronous if it is
synchronous in all implementers.

`Executor::resolve_async()` calls `GraphQLValue::resolve()` for values
of synchronous types. The asynchronous executor resolves a synchronous
field of a synchronous type in place with
`GraphQLValue::resolve_field()`. The Book explains which fields resolve
without futures.

Asynchronous execution with synchronous resolvers now makes as many
allocations as synchronous execution, and it runs as fast. A flat list
of 1000 objects makes 4060 allocations instead of 19,067. On the
benchmark operations except introspection, asynchronous execution with
synchronous resolvers takes 13 to 67% less time. With `async fn`
resolvers, scalars and enums resolve without futures, and execution
takes up to 8% less time.

New tests cover the detection of synchronous objects, interfaces,
unions, cycles, and introspection types. Executor tests check that
marked fields and synchronous types use the synchronous resolvers, and
that other fields use the asynchronous resolvers.
@LegNeato
LegNeato merged commit cd0f15d into graphql-rust:master Nov 9, 2026
184 checks passed
@LegNeato
LegNeato deleted the sync-fast-path branch November 9, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant