-
-
Notifications
You must be signed in to change notification settings - Fork 571
[Performance] 6% performance improvement by lessening is_array and instanceof checks. #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+40
−29
Conversation
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
instanceof is a slow-ish operation, since it's a string compare, but using ->kind directly we can do a direct pointer check, which is way faster.
By doing a null check before calling is_array we prevent tons of method calls, since its very common for calling visitors to be null.
Since it common for the result to be null, by doing that check before we do the more expensive instanceof checks we can prevent allot of if checks
b343de6 to
021b507
Compare
spawnia
approved these changes
Jan 29, 2026
Collaborator
|
Thanks, released with https://github.com/webonyx/graphql-php/releases/tag/v15.30.1. Please consider sponsoring. |
Collaborator
|
Thanks Sven 💪 |
Contributor
|
Yeah, man, awesome. Keep 'em coming! |
Contributor
|
Respect, thanks! |
Contributor
|
Yeey thank you @SavageTiger :) |
wmfgerrit
pushed a commit
to wikimedia/mediawiki-vendor
that referenced
this pull request
Feb 2, 2026
https://github.com/webonyx/graphql-php/releases/tag/v15.30.1 > Improve performance 6% by lessening is_array and instanceof checks webonyx/graphql-php#1845 Change-Id: Ica32b9e771ad4abaaa0b5c8522e5e055364df3fd
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.
Description
While investigating the performance of our API that runs on top of Webonyx, I made some cachegrind profiles and found that we perform an insane amount of is_array checks for each query.
The source of this is the method extractVisitFn. While investigating this, I found that if I do a null check on $kindVisitor and $specificVisitor before checking if they are arrays, it cuts down our callgraph by about 50%. It might seem a bit counterintuitive since is_array also performs the null check implicitly; however, the overhead cost of calling the method on such a hot path is pretty high.
After this finding, I did some more digging into the visitor and found that we also do a bunch of instanceof checks on the result while it is null in many cases. By doing an early null check, all those if and else-if conditions can be skipped.
Lastly, I noticed the instanceof checks in leave() in TypeInfo. instanceof is more expensive computationally than you might think since, under the hood, it’s a string comparison for which memory gets allocated. In this case, we can use ->kind due to the strict typehint.
I tested all these changes by comparing a previous benchmark run to the latest benchmark run and have seen a clear performance improvement with each consecutive change. Here is a table of the overall win:
The baseline is the current version