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 摘要。