natural_comparison_key: faster sort keys for names without digits
维护者通常 1 天内回复
评估
- 难度
- 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
mainat 1d505c1 - CPython 3.12, Linux x86_64
- 主要语言
- Python
- 星标
- 534
- 派生
- 159
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 9
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/graphql-core 的其他 Issue
-
collect_fields spends extra time on selections without directives可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
graphql-python/graphql-core#315 ·
维护者通常 1 天内回复
-
is_printable_as_block_string checks each character in a Python loop可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 83/100
graphql-python/graphql-core#312 ·
维护者通常 1 天内回复
-
Make is_awaitable faster for values that are not awaitable可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
graphql-python/graphql-core#308 ·
维护者通常 1 天内回复
-
assert_name checks each character in a Python generator可能已有人在做 @swayamdudhela 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
graphql-python/graphql-core#304 · 4 条评论 ·
维护者通常 1 天内回复
-
coerce_input_literal spends extra time on type checks and non-null recursion可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
graphql-python/graphql-core#302 ·
维护者通常 1 天内回复
查看 graphql-python/graphql-core 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 67/100
bigbio/sdrf-pipelines#372 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 84/100
521xueweihan/HelloGitHub#4297 ·
-
难度 2/5 1-3 小时 新手友好度 83/100
aws-samples/sample-per-model-bedrock#822 ·
维护者通常 1 天内回复
-
area:study-data p2-high ready-for-agent type:chore
难度 2/5 1-3 小时 新手友好度 78/100
fderuiter/deruiter.dev#3706 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 74/100
awslabs/visual-asset-management-system#1234 ·
维护者通常 1 天内回复