Skip to content

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

Merged
merged 1 commit into from
Feb 8, 2019

Conversation

nkcsgexi
Copy link
Contributor

@nkcsgexi nkcsgexi commented Feb 7, 2019

This hash function will concatenate all interesting pieces of information
of node definitions in a single string and call hash() on this string.

@nkcsgexi nkcsgexi requested a review from akyrtzi February 7, 2019 20:58
Copy link
Contributor

@akyrtzi akyrtzi left a 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.
@nkcsgexi nkcsgexi force-pushed the hash-imple-syntax-nodes branch from b25a4e2 to 0c28b4c Compare February 8, 2019 01:00
@nkcsgexi
Copy link
Contributor Author

nkcsgexi commented Feb 8, 2019

@akyrtzi Done.

Copy link
Contributor

@akyrtzi akyrtzi left a comment

Choose a reason for hiding this comment

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

👍

@nkcsgexi
Copy link
Contributor Author

nkcsgexi commented Feb 8, 2019

@swift-ci please smoke test

@nkcsgexi nkcsgexi merged commit 26bf98f into swiftlang:master Feb 8, 2019
@nkcsgexi nkcsgexi deleted the hash-imple-syntax-nodes branch February 8, 2019 03:54
@@ -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))))
Copy link
Contributor

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)))
Copy link
Contributor

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.

@nkcsgexi
Copy link
Contributor Author

nkcsgexi commented Feb 8, 2019

Thanks! Addressed in #22469

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants