natural_comparison_key: faster sort keys for names without digits

オープン 初心者向け
#310 コメント 0 件 リアクション 0 件 担当者 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時間
マージ済み PR(30日)
9

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

graphql-python/graphql-core のほかの issue

graphql-python/graphql-core の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。