Repository navigation
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 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Candidate changes came from Codex (gpt-6.1-sol), and Weco Observe tracked them. Claude Code picked this candidate, checked it with the test suite, mypy and ruff, and measured it. A person did not review the change before submission.
Weco share link: https://dashboard.weco.ai/share/z3O_QHq3doOpQrFvfclgdtSNB6ioeuCH