natural_comparison_key: faster sort keys for names without digits

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

維護者通常 1 天內回覆

@dexhunter 已經在處理了。

開始於 2026年11月7日。

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

評估

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

研究方向

從 src/graphql/pyutils/natural_compare.py 中的 natural_comparison_key 開始,並閱讀 tests/pyutils/test_natural_compare.py。檢查提議的快速路徑和切片指定是否保留現有鍵,然後執行指定的測試檔案。測試通過且最佳化維持鍵不變,即表示完成。

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

描述

natural_comparison_key in src/graphql/pyutils/natural_compare.py runs for every name when lexicographic_sort_schema sorts a schema. It is also used by suggestion_list and sort_value_node.

Problem

The function always splits the name with a regex. Then it walks the parts with zip and itertools.cycle inside a generator. Most GraphQL names (userName, createdAt) have no digits. For these names, this work always gives the same one-item tuple (name,).

Numbers

I measured the current code and a small change with timeit (CPython 3.12, Linux). The table shows the time to compute the keys for each list of names.

Situation Current Proposed Change
GitHub schema names (3323 names) 2.11 ms 0.34 ms 84% faster
Letters only, userName (5000 names) 3.00 ms 0.38 ms 88% faster
Underscore names, USER_NAME (5000 names) 3.09 ms 1.83 ms 41% faster
Names with numbers, field12x3 (5000 names) 5.61 ms 3.72 ms 34% faster

Proposed fix

Return (key,) early when key.isalpha() is true, because such a name cannot contain a decimal digit. For other names, keep the regex split and replace the digit parts in one slice assignment instead of the zip/cycle generator. The keys stay exactly the same, and the existing tests in tests/pyutils/test_natural_compare.py pass.

Environment

  • graphql-core main at 1d505c1
  • CPython 3.12, Linux x86_64
主要語言
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 摘要。