collect_fields spends extra time on selections without directives

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

維護者通常 1 天內回覆

@dexhunter 已經在處理了。

開始於 2026年11月8日。

  • #316 來自 @dexhunter —— 未關閉

評估

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

研究方向

從 src/graphql/execution/collect_fields.py 中的 visit_selection_sets() 開始,追蹤它在有和沒有指令的選擇中對 should_include_node() 的呼叫。變更迴圈前,先檢查現有的欄位收集和指令行為測試。最佳化完成的標準是:保留結果、錯誤和禁止指令清單,同時避免不必要的指令檢查;將耗時與 issue 中的基準測試案例進行比較。

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

描述

visit_selection_sets() in src/graphql/execution/collect_fields.py runs for every selection set before resolvers run.
For each selection, it calls should_include_node(), even when the selection has no directives.
should_include_node() then builds two generator expressions to look for @skip and @include.

Most fields in real queries have no directives, so this work is repeated for almost every field.

Situation Now With the proposed fix Change
collect_fields on 4,000 plain fields 4458.7 µs 4113.4 µs 7.7% less time
collect_fields on 4,000 fields with @include(if: $t) 10988.9 µs 8593.5 µs 21.8% less time
collect_fields on 40 inline fragments with 100 fields each 4561.1 µs 4165.7 µs 8.7% less time
collect_fields on 400 named fragment spreads with 1 field each 960.0 µs 889.9 µs 7.3% less time

These are medians of 31 timing rounds, and the median of 3 alternating processes per version.

Proposed fix: check selection.directives once in the loop and skip the should_include_node() call when it is empty.
Find @skip and @include in one plain loop instead of two generator expressions.
Results, errors and forbidden-directive lists stay the same.

Environment: CPython 3.12.13, Linux x86-64, AMD EPYC 9R14, graphql-core main at 1d505c1.

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

環境準備

  • 沒有 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 摘要。