Repository navigation
perf(values): depth check keeps one frame per level - #318
Conversation
input_is_nested_too_deeply pushed one tuple per list item onto its stack before it popped any, so its memory grew with the width of the list. Keep one frame per nesting level (values, type, depth, next index) and stop the walk of a level when its item type cannot nest. Coercing a [Int] variable of 50,000 ints has a tracemalloc peak of 818,981 bytes, down from 4,091,392 bytes. The returned bool is unchanged. Co-Authored-By: Aiden <aiden@weco.ai>
The depth check keeps one frame per nesting level. No test used a list with several nested items or an empty list, so one line and one branch were not covered and the 100% coverage gate failed on Python 3.14. Co-Authored-By: Aiden <aiden@weco.ai>
Reuse the deep list type of the existing test instead of building it again in a second test. Co-Authored-By: Aiden <aiden@weco.ai>
Cito
left a comment
There was a problem hiding this comment.
Thanks, the gain for lists of leaf values is real and worth having. I would prefer a smaller change, though. The index frames make the loop harder to follow, and the gain comes almost entirely from not walking the items of a list whose item type cannot nest.
Could you change only the list branch, like this?
if is_list_type(type_):
items = value if isinstance(value, (list, tuple)) else [value]
item_type = type_.of_type
if not is_leaf_type(get_nullable_type(item_type)):
stack.extend((item, item_type, depth + 1) for item in items)
elif items and depth >= MAX_NESTING_DEPTH:
return True # the leaf items would be nested too deeplyget_nullable_type and is_leaf_type need to be imported from ..type.
I compared this with the current function on 800,000 random values and types with several values of MAX_NESTING_DEPTH, and the result was the same in every case. Best time of one call on CPython 3.14 (current / this PR / variant above):
| Situation | Current | This PR | Variant |
|---|---|---|---|
[Int], 50,000 ints |
5.78 ms | 0.0003 ms | 0.0002 ms |
[[Int]], 500 lists of 100 ints |
5.35 ms | 0.127 ms | 0.092 ms |
[Point], 5,000 input objects |
2.74 ms | 3.35 ms | 2.70 ms |
Lists of input objects still push one entry per item, but each item is a dict that is much larger than its stack entry, so I don't think the extra bookkeeping pays off there.
Please keep the test cases you added, they are useful.
Keep the leaf-list shortcut and restore the original stack entries, as requested in review. Preserve the added depth-limit tests. Fixes graphql-python#317 Co-Authored-By: Aiden <aiden@weco.ai>
|
86e84dc uses your proposed list branch and adds the two imports. It removes the index frames and keeps all added tests. Only the list branch and imports now differ from the original function. Lists of leaf values skip item checks but still check the depth limit. Other values use the original traversal. I measured these results on CPython 3.12.13. Times are the best of nine runs of 20 calls to
The full variable-coercion call with 50,000 ints also keeps the memory gain: Old -> new (mean): 3.9 MiB -> 799.1 KiB. New/old ratio: 0.200 (95% CI 0.1998–0.2002). Change per input: -3.1 MiB ± 842 bytes SD (95% CI -3.1 MiB to -3.1 MiB). n = 5 fixed inputs, each measured in 3 runs. This is 80.0% less peak memory (95% CI 79.98–80.02%).
The repository's Ruff checks passed for |
TL;DR
input_is_nested_too_deeply, the depth check for variable values.tracemallocpeak for a[Int]variable of 50,000 ints: 4,091,392 bytes -> 818,981 bytes, 80.0% less.PYTHONPATH=src python3 -m pytest tests/execution/test_variables.py -qpassed 106 tests.Refs #317
What & Why
Before, the check pushed one
(item, type, depth)tuple per list item onto its stack before it popped any.The memory for the check grew with the width of the list, about 70 bytes per item.
Now each stack frame holds one nesting level: the values, their type, the depth, and the next index.
The check also stops the walk of a level when the item type is not a list or an input object.
Such items cannot nest, so one depth check covers the full level.
The output stays identical because the check still visits each followed value at the same depth.
Empty lists still add no depth, and values under scalars are still not followed.
Validation
Peak traced memory (
tracemalloc) and best time of one call to the check:[Int], 50,000 ints[Int], 10,000 ints[[Int]], 500 lists of 100 ints[Point], 5,000 input objectsInt, one valueThe last row shows a case where the change does not help, and it shows no slowdown.
The number in the TL;DR is for the full
get_variable_valuescall, so it includes the coerced result.Measured on CPython 3.12.13, Linux x86-64, AMD EPYC 9R14, on one isolated core.
Script for the first row; run it on the base commit and on this change:
The complete suite also passed with
PYTHONPATH=src python3 -m pytest tests -q: 3,821 passed, 18 skipped.ruff check,ruff format --check(ruff 0.17.4) andmypy(2.4.1) passed forsrc/graphql/execution/values.py.Why this is safe and easy to maintain
The change is small and easy to read: 13 added lines in one private function, with no new imports.
The function keeps its loop, its explicit stack, and its signature.
Behaviour is unchanged, and the returned bool is the same for every value and type.
The tests in
tests/execution/test_variables.pycover the depth limit for lists, input objects, and custom scalars.Scope
This changes Python bookkeeping in a private helper only.
It adds no feature and does not change GraphQL behavior or the public API.
AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/yE6YtGpMB4NV3RCoYfCM3tHnAk6srhv_