Skip to content

Cache node hashing #150

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

Merged
merged 2 commits into from
Dec 11, 2021
Merged
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
10 changes: 8 additions & 2 deletions src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Node:
"""AST nodes"""

# allow custom attributes and weak references (not used internally)
__slots__ = "__dict__", "__weakref__", "loc"
__slots__ = "__dict__", "__weakref__", "loc", "_hash"

loc: Optional[Location]

Expand Down Expand Up @@ -255,7 +255,13 @@ def __eq__(self, other: Any) -> bool:
)

def __hash__(self) -> int:
return hash(tuple(getattr(self, key) for key in self.keys))
if getattr(self, "_hash", None) is None:
self._hash = hash(tuple(getattr(self, key) for key in self.keys))
return self._hash

def __setattr__(self, key, value):
object.__setattr__(self, "_hash", None)
super().__setattr__(key, value)

def __copy__(self) -> "Node":
"""Create a shallow copy of the node."""
Expand Down