Repository navigation
Conversation
assert_name() checked the name tail with a generator that called is_name_continue() once per character. For ASCII text, str.isidentifier() is true exactly for [_a-zA-Z][_a-zA-Z0-9]*, so valid names now return after isascii() and isidentifier(). Invalid names keep the same checks in the same order, so error classes and messages are unchanged. build_ast_schema on the GitHub SDL: 14.40 ms -> 10.79 ms (25% less time). Co-Authored-By: Aiden <[email protected]>
Merging this PR will improve performance by 47.02%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing |
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
assert_name()faster, which runs for every name during schema construction.build_ast_schemabenchmark on the GitHub SDL, median of best times: 14.40 ms -> 10.79 ms, 25.0% faster.PYTHONPATH=src python -m pytest tests -q: 3625 passed, 18 skipped.Refs #304
What & Why
Before,
assert_name()checked the name tail with a generator.It called
is_name_continue()once for each character, so long names cost many Python calls.Now it first checks
isinstance(name, str) and name.isascii() and name.isidentifier().For ASCII text, a Python identifier is exactly a GraphQL name:
[_a-zA-Z][_a-zA-Z0-9]*.Valid names return at once.
Invalid names keep the same checks in the same order, so each error type and message stays the same.
The tail check now uses
name[1:].replace("_", "")withisascii()andisalnum().If the tail is valid but the name is not, only the first character can be wrong.
The code then raises the "must start with" error.
Results
build_ast_schemaon the GitHub SDL (assume_valid=True)build_client_schemaon the GitHub introspection resultassert_nameon 10,000 valid namesassert_nameon 10,000 invalid namesparseon the large query (does not callassert_name)Each value is the best of 15 runs, then the median of 7 alternating processes per version.
CPython 3.12.13, Linux x86-64, AMD EPYC 9R14, one dedicated CPU core.
Why this is safe and easy to maintain
The change touches one function and adds no new helpers or dependencies.
Valid names give the same return value, and invalid names give the same exception class and message.
tests/type/test_assert_name.py, the schema build tests and the doctests in the module cover these cases.Validation
PYTHONPATH=src python -m pytest tests -q: 3625 passed, 18 skipped.PYTHONPATH=src python -m pytest --doctest-modules src/graphql/type/assert_name.py: 2 passed.pytest tests/benchmarks/test_build_ast_schema.py tests/benchmarks/test_build_client_schema.py --benchmark-only.AI assistance
Candidate changes were generated with Codex (gpt-6.1-sol) and tracked with Weco Observe.
An automated agent selected the final change and ran the tests and benchmarks above.
Weco share link: https://dashboard.weco.ai/share/nBw9xQ9xWsK2u9f9hqyIJaqqBFFOsN7L