Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some optimizations for union formation #521

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Optimize local variables with very complex inferred types (#521)

## Version 0.7.0 (April 13, 2022)

Release highlights:
Expand Down
2 changes: 2 additions & 0 deletions pyanalyze/stacked_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,8 @@ def _get_value_from_nodes(
ctx: _LookupContext,
constraints: Iterable[Constraint] = (),
) -> Value:
# Deduplicate nodes to gain some performance.
nodes = OrderedDict.fromkeys(nodes)
# If the variable is a nonlocal or composite, "uninitialized" doesn't make sense;
# instead use an empty constraint to point to the parent scope.
should_use_unconstrained = (
Expand Down
12 changes: 6 additions & 6 deletions pyanalyze/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,7 +2171,6 @@ def unite_values(*values: Value) -> Value:
# sets have unpredictable iteration order.
hashable_vals = OrderedDict()
unhashable_vals = []
saw_unreachable = False
for value in values:
if isinstance(value, MultiValuedValue):
subvals = value.vals
Expand All @@ -2184,21 +2183,22 @@ def unite_values(*values: Value) -> Value:
else:
subvals = [value]
for subval in subvals:
if _is_unreachable(subval):
saw_unreachable = True
continue
try:
# Don't readd it to preserve original ordering.
if subval not in hashable_vals:
hashable_vals[subval] = None
except Exception:
unhashable_vals.append(subval)
existing = list(hashable_vals) + unhashable_vals
num = len(existing)
reachabilities = [_is_unreachable(val) for val in existing]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This defers the _is_unreachable call until after we have deduplicated the values, so we don't call it repeatedly if we have lots of duplicates.

num_unreachable = sum(reachabilities)
num = len(existing) - num_unreachable
if num == 0:
if saw_unreachable:
if num_unreachable:
return AnyValue(AnySource.unreachable)
return NO_RETURN_VALUE
if num_unreachable:
existing = [val for i, val in enumerate(existing) if not reachabilities[i]]
if num == 1:
return existing[0]
else:
Expand Down