Dedup ImportVar entries in merge_imports / merge_parsed_imports#6737
Open
Alek99 wants to merge 4 commits into
Open
Dedup ImportVar entries in merge_imports / merge_parsed_imports#6737Alek99 wants to merge 4 commits into
Alek99 wants to merge 4 commits into
Conversation
merge_imports extended per-lib lists without dedup. Every @var_operation merges the same operand VarData through both _args and _return, so chained operations (rx.cond chains, arithmetic) doubled the merged import entries per nesting level: depth 15 produced ~196k entries (29ms per merge), depth 18 ~1.57M (252ms). ImportVar is a frozen (hashable) dataclass, so both merge functions now accumulate per-lib insertion-ordered dicts, dropping exact duplicates while preserving first-seen order and the distinctness of entries that differ in alias/default/etc. This keeps merged VarData constant-sized for chained operations and linear for component trees. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jy8uHH11KircGa2MbTrR8g
Contributor
Greptile SummaryThis PR reduces duplicate import entries during var-data import merging. The main changes are:
Confidence Score: 5/5This looks safe to merge.
Important Files Changed
Reviews (3): Last reviewed commit: "Type the chained-ops regression test via..." | Re-trigger Greptile |
Merging this PR will not alter performance
Comparing Footnotes
|
CodSpeed flagged test_get_all_imports regressing ~40% with dedup in the component-tree fold: per-entry Python-level hashing outweighs list extend there, duplication is only linear, and the result is collapsed downstream anyway. The compounding-duplication path (VarData.merge for chained var operations) goes through merge_imports, which keeps the dedup. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jy8uHH11KircGa2MbTrR8g
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jy8uHH11KircGa2MbTrR8g
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jy8uHH11KircGa2MbTrR8g
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Type of change
Changes To Core Features:
What this does
merge_imports(andmerge_parsed_imports) extended per-lib lists without dedup. Every@var_operationmerges the same operandVarDatathrough both_argsand_return(CustomVarOperation._cached_get_all_var_data), so chained operations —rx.condchains, arithmetic likea + 1 + 1 + …— doubled the merged import-entry count per nesting level:Fix
ImportVaris a frozen (hashable) dataclass. Both merge functions now accumulate per-lib insertion-ordered dicts (dict[ImportVar, None]), which:collapse_imports, via an unorderedset);alias,is_default,package_path, …);VarDataconstant-sized for chained var operations and linear for component trees.Return type/shape is unchanged (
defaultdict[str, list[ImportVar]], empty-lib keys preserved).Verification
VarDataimport entries stay collapsed (pre-fix this blows up to ~1.57M entries).test_get_all_imports,test_collect_imports,test_compile_page*) measure the compile-path effect directly.Note: this session's sandbox has no PyPI egress, so local profiling numbers could not be produced here; the CodSpeed instrumentation run on this PR is the authoritative before/after CPU measurement.
Part of a compiler-performance series; tracked in Linear as ENG-10102.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Jy8uHH11KircGa2MbTrR8g
Generated by Claude Code