Skip to content

Commit f53d840

Browse files
⚡️ Speed up function process_result by 12% in PR #3819 (feature/defer)
Here is an optimized version. Improvements. - Removed the unnecessary intermediate variables (`errors`, `extensions`) that are only used once. - Avoided repeated dictionary unpacking by building the dictionary with direct assignments and only adding keys if necessary. - Used `if-else` statements for error and extensions blocks for slight speed-ups over dictionary unpacking on small dicts. Rewritten code. This version allocates less intermediate data and does not do work unless necessary.
1 parent 108812c commit f53d840

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

strawberry/http/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ def process_result(result: ResultType) -> GraphQLHTTPResponse:
2020
if isinstance(result, GraphQLIncrementalExecutionResults):
2121
return result
2222

23-
errors, extensions = result.errors, result.extensions
24-
data: GraphQLHTTPResponse = {
25-
"data": result.data,
26-
**({"errors": [err.formatted for err in errors]} if errors else {}),
27-
**({"extensions": extensions} if extensions else {}),
28-
}
23+
data: GraphQLHTTPResponse = {"data": result.data}
24+
if result.errors:
25+
data["errors"] = [err.formatted for err in result.errors]
26+
if result.extensions:
27+
data["extensions"] = result.extensions
2928

3029
return data
3130

0 commit comments

Comments
 (0)