Optimize Lexer::readName#1813
Merged
spawnia merged 4 commits intowebonyx:masterfrom Dec 15, 2025
kasparsklavins:patch-1
Merged
Conversation
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
spawnia
approved these changes
Dec 12, 2025
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Lexer::readName method by replacing a character-by-character loop with PHP's native strspn() and substr() functions. Since GraphQL names are restricted to ASCII characters [_A-Za-z][_0-9A-Za-z]*, it's safe to use byte-level string operations without UTF-8 character handling. The optimization yields a 30% performance improvement on introspection query tokenization.
Key changes:
- Replaced character-by-character loop in
readName()withstrspn()for counting valid name characters andsubstr()for extracting the name value - Simplified cursor advancement to a single
moveStringCursor()call
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Language/Lexer.php | Optimized readName() method to use strspn() and substr() instead of character loop |
| CHANGELOG.md | Documented the Lexer name tokenization optimization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Collaborator
|
Thank you @kasparsklavins, released with https://github.com/webonyx/graphql-php/releases/tag/v15.28.0. |
1 task
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.
Finding the name's length using
strspn(...), and doing a single string copy withsubstr(...)is faster than looping through one character at a time. As names must be [A-Za-z0-9_]+, it's safe to skip UTF8 checks.Benchmark
Benchmark for introspection query shows promising results on my modest potato running php 8.3 with opcache enabled
bench.php