coerce_input_literal spends extra time on type checks and non-null recursion

未关闭 适合新手
#302 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
performance

调研方向

阅读 src/graphql/utilities/coerce_input_value.py 和 tests/benchmarks/test_execution_arguments.py 中的基准测试。检查 coerce_input_literal 如何处理 NonNull 类型并调用三个类型检查辅助函数,然后进行建议的更改并运行相关测试和基准测试。行为保持不变且基准测试显示报告中的性能提升时,即为完成。

由索引模型根据 Issue 内容生成。

描述

coerce_input_literal in src/graphql/utilities/coerce_input_value.py runs for every literal argument in a query. For example, it runs for multiplier: 2 once per item in tests/benchmarks/test_execution_arguments.py.

The function does two kinds of extra work:

  • For a NonNull type, it calls itself again with the inner type. The second call checks for a variable node and a non-null type again.
  • It uses the is_non_null_type, is_list_type and is_input_object_type helper functions. Each helper is one more function call around an isinstance check.

Measured on Python 3.12, median of 3 runs (time per call):

Situation Now With direct checks Change
Int! literal (multiplier: 2) 734 ns 479 ns 35% faster
[Int!] list of 10 ints 7557 ns 5089 ns 33% faster
input object {a: 1, b: "x"} 2355 ns 1717 ns 27% faster
variable $v 172 ns 156 ns no real change
execute_sync of the 1000-item benchmark query 5.32 ms 5.00 ms 6% faster

Proposed fix: unwrap the NonNull type in place instead of calling the function again. Use direct isinstance checks against GraphQLNonNull, GraphQLList and GraphQLInputObjectType in this one function. The results stay the same, and the full test suite passes with the change.

Environment: graphql-core main at 0aaea71, CPython 3.12.13, Linux x86_64.

主要语言
Python
星标
534
派生
159
平均合并
1 天 10 小时
30 天内合并 PR
11

环境准备

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