-
-
Notifications
You must be signed in to change notification settings - Fork 603
Add custom is_awaitable to test perf #4035
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
Draft
patrick91
wants to merge
1
commit into
main
Choose a base branch
from
test-is-awaitable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Execution utilities for Strawberry GraphQL.""" | ||
|
|
||
| from .is_awaitable import optimized_is_awaitable | ||
|
|
||
| __all__ = ["optimized_is_awaitable"] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Optimized is_awaitable implementation for GraphQL execution. | ||
|
|
||
| This module provides a highly optimized is_awaitable function that adds a fast path | ||
| for common synchronous types, significantly improving performance when dealing with | ||
| large result sets containing primitive values. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| from types import CoroutineType, GeneratorType | ||
| from typing import Any | ||
|
|
||
| __all__ = ["optimized_is_awaitable"] | ||
|
|
||
| CO_ITERABLE_COROUTINE = inspect.CO_ITERABLE_COROUTINE | ||
|
|
||
| # Common synchronous types that are never awaitable | ||
| # Using a frozenset for O(1) lookup | ||
| _NON_AWAITABLE_TYPES: frozenset[type] = frozenset( | ||
| { | ||
| type(None), | ||
| bool, | ||
| int, | ||
| float, | ||
| str, | ||
| bytes, | ||
| bytearray, | ||
| list, | ||
| tuple, | ||
| dict, | ||
| set, | ||
| frozenset, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def optimized_is_awaitable(value: Any) -> bool: | ||
| """Return true if object can be passed to an ``await`` expression. | ||
|
|
||
| This is an optimized version of graphql-core's is_awaitable that adds a fast path | ||
| for common synchronous types. For large result sets containing mostly primitive | ||
| values (ints, strings, lists, etc.), this can provide significant performance | ||
| improvements. | ||
|
|
||
| Performance characteristics: | ||
| - Fast path for primitives: O(1) type lookup | ||
| - Falls back to standard checks for other types | ||
| - Avoids expensive isinstance and hasattr calls for common types | ||
|
|
||
| Args: | ||
| value: The value to check | ||
|
|
||
| Returns: | ||
| True if the value is awaitable, False otherwise | ||
| """ | ||
| # Fast path: check if the type is a known non-awaitable type | ||
| # This single check replaces 3 checks (isinstance, isinstance, hasattr) | ||
| # for the most common case | ||
| value_type = type(value) | ||
| if value_type in _NON_AWAITABLE_TYPES: | ||
| return False | ||
|
|
||
| # For other types, use the standard graphql-core logic | ||
| # This handles coroutines, generators, and custom awaitable objects | ||
| return ( | ||
| # check for coroutine objects | ||
| isinstance(value, CoroutineType) | ||
| # check for old-style generator based coroutine objects | ||
| or ( | ||
| isinstance(value, GeneratorType) | ||
| and bool(value.gi_code.co_flags & CO_ITERABLE_COROUTINE) | ||
| ) | ||
| # check for other awaitables (e.g. futures) | ||
| or hasattr(value, "__await__") | ||
| ) | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: The difference between this and https://github.com/graphql-python/graphql-core/blob/main/src/graphql/pyutils/is_awaitable.py#L20-L20 is the fast path, right?
We can possibly just import it from graphql-core and
return graphql_core_is_awaitable(value)here, so we don't need to redefine this "not so obvious" code