-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SyntaxNodes: implement the hash function for syntax nodes. #22457
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
Conversation
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.
I have a recommendation to improve how the hash is calculated. Instead of concatenating strings you can use hash
's ability to hash a tuple and get the hash but "feeding" the values into the function, like this:
def hash_syntax_node(node):
# Hash into the syntax name and serialization code
result = hash((node.name, get_serialization_code(node.syntax_kind)))
for child in node.children:
# Hash into the expected child syntax
result = hash((result, child.syntax_kind))
# Hash into the child name
result = hash((result, child.name))
# Hash into whether the child is optional
result = hash((result, child.is_optional))
return result
This hash function will concatenate all interesting pieces of information of node definitions in a single string and call hash() on this string.
b25a4e2
to
0c28b4c
Compare
@akyrtzi Done. |
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.
👍
@swift-ci please smoke test |
@@ -141,5 +143,28 @@ def dedented_lines(description): | |||
return textwrap.dedent(description).split('\n') | |||
|
|||
|
|||
def hash_syntax_node(node): | |||
# Hash into the syntax name and serialization code | |||
result = hash((node.name, str(get_serialization_code(node.syntax_kind)))) |
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.
Oops sorry, missed a nitpick, you don't have to convert the integer to a string in order to hash it.
Also no need for str(child.is_optional)
and str(token.serialization_code)
below.
return "abc" | ||
result = 0 | ||
for node in SYNTAX_NODES: | ||
result = hash((result, hash_syntax_node(node=node))) |
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.
Recommend hash_syntax_node(node=node)
-> hash_syntax_node(node)
, named arguments are not required in python, you usually put them for readability when there's a lot of them, but it's not offering any value here
Also remove named argument for hash_token_syntax(token=token)
below.
Thanks! Addressed in #22469 |
This hash function will concatenate all interesting pieces of information
of node definitions in a single string and call hash() on this string.