forked from cylc/cylc-uiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graphql-ws: optimise the "resolve" routine (cylc#548)
graphql-ws: optimise the "resolve" routine * Partially addresses cylc#547 * The resolve routine is implmented recursively in the graphql-ws library. * Because the function is async this results in a large number of async tasks being created when the library is used with large, deeply nested schema. * Async tasks have an overhead, above that of regular function calls. * For the example in cylc#547 this resulted in over 10 seconds of overheads. * websockets: avoid duplicate resolve call Due to inheritance, we were calling `resolve(execution_result.data)` twice. --------- Co-authored-by: Tim Pillinger <[email protected]>
- Loading branch information
1 parent
f2def73
commit c821ecc
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
Improve the performance of the GraphQL server. |
This file contains 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,67 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2017, Syrus Akbary | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
""" | ||
This file contains an implementation of "resolve" derived from the one | ||
found in the graphql-ws library with the above license. | ||
This is temporary code until the change makes its way upstream. | ||
""" | ||
|
||
# NOTE: transient dependency from graphql-ws purposefully not | ||
# reflected in cylc-uiserver dependencies | ||
from promise import Promise | ||
|
||
from graphql_ws.base_async import is_awaitable | ||
|
||
|
||
async def resolve( | ||
data, | ||
_container=None, | ||
_key=None, | ||
): | ||
""" | ||
Wait on any awaitable children of a data element and resolve | ||
any Promises. | ||
""" | ||
stack = [(data, _container, _key)] | ||
|
||
while stack: | ||
_data, _container, _key = stack.pop() | ||
|
||
if is_awaitable(_data): | ||
_data = await _data | ||
if isinstance(_data, Promise): | ||
_data = _data.value | ||
if _container is not None: | ||
_container[_key] = _data | ||
if isinstance(_data, dict): | ||
items = _data.items() | ||
elif isinstance(_data, list): | ||
items = enumerate(_data) | ||
else: | ||
items = None | ||
if items is not None: | ||
stack.extend([ | ||
(child, _data, key) | ||
for key, child in items | ||
]) |
This file contains 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