-
Notifications
You must be signed in to change notification settings - Fork 188
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
Cache the scope name prefix to prevent scope traversal in a tight loop #708
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,24 +217,12 @@ def _index(self) -> int: | |
return self.__index | ||
|
||
def get_qualified_names_for(self, full_name: str) -> Set[QualifiedName]: | ||
scope = self.scope | ||
name_prefixes = [] | ||
while scope: | ||
if isinstance(scope, ClassScope): | ||
name_prefixes.append(scope.name) | ||
elif isinstance(scope, FunctionScope): | ||
name_prefixes.append(f"{scope.name}.<locals>") | ||
elif isinstance(scope, ComprehensionScope): | ||
name_prefixes.append("<comprehension>") | ||
elif not isinstance(scope, (GlobalScope, BuiltinScope)): | ||
raise Exception(f"Unexpected Scope: {scope}") | ||
|
||
scope = scope.parent if scope.parent != scope else None | ||
|
||
parts = [*reversed(name_prefixes)] | ||
if full_name: | ||
parts.append(full_name) | ||
return {QualifiedName(".".join(parts), QualifiedNameSource.LOCAL)} | ||
return { | ||
QualifiedName( | ||
".".join(filter(None, [self.scope._name_prefix, full_name])), | ||
QualifiedNameSource.LOCAL, | ||
) | ||
} | ||
|
||
|
||
# even though we don't override the constructor. | ||
|
@@ -409,6 +397,7 @@ class Scope(abc.ABC): | |
_assignment_count: int | ||
_accesses_by_name: MutableMapping[str, Set[Access]] | ||
_accesses_by_node: MutableMapping[cst.CSTNode, Set[Access]] | ||
_name_prefix: str | ||
|
||
def __init__(self, parent: "Scope") -> None: | ||
super().__init__() | ||
|
@@ -418,6 +407,7 @@ def __init__(self, parent: "Scope") -> None: | |
self._assignment_count = 0 | ||
self._accesses_by_name = defaultdict(set) | ||
self._accesses_by_node = defaultdict(set) | ||
self._name_prefix = "" | ||
|
||
def record_assignment(self, name: str, node: cst.CSTNode) -> None: | ||
target = self._find_assignment_target(name) | ||
|
@@ -667,6 +657,8 @@ def __init__( | |
self.name = name | ||
self.node = node | ||
self._scope_overwrites = {} | ||
# pyre-fixme[4]: Attribute `_name_prefix` of class `LocalScope` has type `str` but no type is specified. | ||
self._name_prefix = self._make_name_prefix() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pyre is unhappy with this line. Any suggestions @zsol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a bug to me, might be worth raising with Pyre folks internally |
||
|
||
def record_global_overwrite(self, name: str) -> None: | ||
self._scope_overwrites[name] = self.globals | ||
|
@@ -695,6 +687,9 @@ def __getitem__(self, name: str) -> Set[BaseAssignment]: | |
else: | ||
return self.parent._getitem_from_self_or_parent(name) | ||
|
||
def _make_name_prefix(self) -> str: | ||
return ".".join(filter(None, [self.parent._name_prefix, self.name, "<locals>"])) | ||
|
||
|
||
# even though we don't override the constructor. | ||
class FunctionScope(LocalScope): | ||
|
@@ -741,6 +736,9 @@ def _contains_in_self_or_parent(self, name: str) -> bool: | |
""" | ||
return self.parent._contains_in_self_or_parent(name) | ||
|
||
def _make_name_prefix(self) -> str: | ||
return ".".join(filter(None, [self.parent._name_prefix, self.name])) | ||
|
||
|
||
# even though we don't override the constructor. | ||
class ComprehensionScope(LocalScope): | ||
|
@@ -755,7 +753,9 @@ class ComprehensionScope(LocalScope): | |
# TODO: Assignment expressions (Python 3.8) will complicate ComprehensionScopes, | ||
# and will require us to handle such assignments as non-local. | ||
# https://www.python.org/dev/peps/pep-0572/#scope-of-the-target | ||
pass | ||
|
||
def _make_name_prefix(self) -> str: | ||
return ".".join(filter(None, [self.parent._name_prefix, "<comprehension>"])) | ||
|
||
|
||
# Generates dotted names from an Attribute or Name node: | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither of these can be
None
, and this is a pretty hot function. This should be much fasterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter(None,...) also removes empty strings, which is what I need here. Happy to consider other ways to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"{a}.{b}" if a else b
is about 4x faster still