Skip to content

Commit

Permalink
Some optimizations for union formation (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Apr 13, 2022
1 parent 21bff91 commit 0f995f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
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]
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

0 comments on commit 0f995f4

Please sign in to comment.