Repository navigation
perf(coerce_input_literal): faster Int! literals, measurable on args bench - #303
Conversation
Unwrap NonNull in place instead of calling coerce_input_literal again, and use direct isinstance checks instead of the is_*_type helpers. Check the null literal once, and keep append/get and the field value node in local names in the list and object loops. Leaf types still go through assert_leaf_type. The execution-arguments benchmark goes from 300.24 ms to 287.30 ms (median of 3 runs). An Int! literal goes from 734 ns to 479 ns. Co-Authored-By: Aiden <aiden@weco.ai>
Cito
left a comment
There was a problem hiding this comment.
Thanks. I measured this on CPython 3.14 and PyPy 3.12. Most of the gain comes from one part of the change, unwrapping the non-null type in place instead of calling the function again. I would like to take that part and keep the rest of the function in the shape of coerceInputLiteral in GraphQL.js, with the is_*_type helpers that the executor uses as well, so that upstream changes stay easy to port:
if is_non_null_type(type_):
if isinstance(value_node, NullValueNode):
return Undefined # Invalid: intentionally return no value.
# Unlike GraphQL.js, we unwrap the type in place instead of recursing,
# because function calls are costly in Python.
type_ = type_.of_type
elif isinstance(value_node, NullValueNode):
return None # Explicitly return the value null.I compared this variant and the PR with the current function on 400,000 random combinations of types, literals and variables, and all three gave the same results. Median times in ms:
| Situation | Current | This PR | Variant |
|---|---|---|---|
| field arguments benchmark, CPython 3.14 | 3.47 | 3.33 | 3.32 |
| field arguments benchmark, PyPy 3.12 | 0.404 | 0.392 | 0.387 |
| 1,000 fields with an input object literal, CPython 3.14 | 10.92 | 9.44 | 9.99 |
| 1,000 fields with an input object literal, PyPy 3.12 | 1.209 | 1.177 | 1.165 |
The other changes (direct isinstance checks, bound methods, the assignment expression) only help input object literals on CPython, by another 5 %, and that is not enough for me to move away from the upstream shape. Could you reduce the PR to the change above?
Restore the type helpers and surrounding code to keep upstream changes easy to port, as requested in review. Fixes graphql-python#302 Co-Authored-By: Aiden <aiden@weco.ai>
Keep the non-null unwrapping change on the current PR base. Fixes graphql-python#302 Co-Authored-By: Aiden <aiden@weco.ai>
|
5006fcb keeps only the non-null unwrapping change and your suggested comment, and merges the PR base. The This skips one function call for non-null literals. The same checks still run on the inner type, so results stay unchanged. On CPython 3.15.2, these are median times from 15 measurements per version, comparing the PR base with this commit:
The field arguments gain is small. The nullable case does not benefit; its 0.2% difference is small enough to be noise.
The separate mixed execution and coercion benchmark, using the original base, also shows a small gain: Median new/old time ratio: 0.967 (95% CI 0.966–0.970). Mean ± SD: 0.968 ± 0.008. P95: 0.982. n = 33 old/new pairs in 3 runs. This is 3.3% faster (95% CI 3.0–3.4%). |
|
Thanks, that is exactly the change I had in mind. tox passes here with it on top of current main, and the gain I measure on CPython 3.14 is in line with your numbers for 3.15. Merged now. |
TL;DR
coerce_input_literalfaster. This function coerces literal arguments such asmultiplier: 2during execution.NonNullin place and uses directisinstancechecks instead of theis_*_typehelpers.main.Refs #302
What & Why
Before, a
NonNulltype made the function call itself with the inner type. That second call checked the variable node and the null literal again. Each type check also went through anis_*_typehelper, which is one more function call aroundisinstance.Now the function checks for a null literal once. Then it replaces
type_withtype_.of_typeand continues in the same call. It checksGraphQLNonNull,GraphQLListandGraphQLInputObjectTypedirectly. The list and object loops keepappendandgetin local names. The object branch reads each field value node once. Leaf types still go throughassert_leaf_type. The branches return the same values in the same order, so every result stays identical.graphql-js uses recursion for
NonNullincoerceInputLiteral. This PR changes that on purpose, because a Python function call costs much more than a JavaScript call.Results
Time per call, median of 3 runs on Python 3.12:
Int!literal (multiplier: 2)[Int!]list of 10 ints{a: 1, b: "x"}$v(code path not changed)execute_syncof the 1000-item query fromtest_execution_arguments.pyWhy this is safe and easy to maintain
The change is small and stays inside one function. It uses the same
isinstancechecks that theis_*_typehelpers use, so behaviour and output do not change. The existing tests intests/utilities/test_coerce_input_value.py,tests/executionand the module doctests cover all branches.Validation
pytest testsgives 3625 passed, 18 skipped, with and without this change.mypyandruff check/ruff format --checkpass on the changed file.tests/benchmarks/test_execution_arguments.pyplus 336 mixed literal cases. It runs in 300.24 ms before and 287.30 ms after (median of 3 runs, each 11 alternating before/after pairs).pytest tests/benchmarks/test_execution_arguments.py --benchmark-onlyI did not add new tests because behaviour does not change.
AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/z3O_QHq3doOpQrFvfclgdtSNB6ioeuCH