coerce_input_literal spends extra time on type checks and non-null recursion

未關閉 適合新手
#302 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
重構
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
performance

研究方向

閱讀 src/graphql/utilities/coerce_input_value.py 和 tests/benchmarks/test_execution_arguments.py 中的基準測試。檢查 coerce_input_literal 如何處理 NonNull 型別並呼叫三個型別檢查輔助函式,接著進行建議的變更並執行相關測試和基準測試。行為維持不變且基準測試顯示回報的效能提升時,即為完成。

由索引模型根據 Issue 內容生成。

描述

coerce_input_literal in src/graphql/utilities/coerce_input_value.py runs for every literal argument in a query. For example, it runs for multiplier: 2 once per item in tests/benchmarks/test_execution_arguments.py.

The function does two kinds of extra work:

  • For a NonNull type, it calls itself again with the inner type. The second call checks for a variable node and a non-null type again.
  • It uses the is_non_null_type, is_list_type and is_input_object_type helper functions. Each helper is one more function call around an isinstance check.

Measured on Python 3.12, median of 3 runs (time per call):

Situation Now With direct checks Change
Int! literal (multiplier: 2) 734 ns 479 ns 35% faster
[Int!] list of 10 ints 7557 ns 5089 ns 33% faster
input object {a: 1, b: "x"} 2355 ns 1717 ns 27% faster
variable $v 172 ns 156 ns no real change
execute_sync of the 1000-item benchmark query 5.32 ms 5.00 ms 6% faster

Proposed fix: unwrap the NonNull type in place instead of calling the function again. Use direct isinstance checks against GraphQLNonNull, GraphQLList and GraphQLInputObjectType in this one function. The results stay the same, and the full test suite passes with the change.

Environment: graphql-core main at 0aaea71, CPython 3.12.13, Linux x86_64.

主要語言
Python
星號
534
分支
159
平均合併
1 天 10 小時
30 天內合併 PR
11

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

graphql-python/graphql-core 的其他 Issue

查看 graphql-python/graphql-core 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。