Skip to content

Hash consing#385

Open
ricky122-5 wants to merge 3 commits into
mainfrom
hash-consing
Open

Hash consing#385
ricky122-5 wants to merge 3 commits into
mainfrom
hash-consing

Conversation

@ricky122-5
Copy link
Copy Markdown
Collaborator

Adds hash consing to finch-lite.

Implements hash consing across all 5 IR node hierarchies, so structurally equal nodes now return the same object via a WeakValueDictionary intern table. Intercepts construction in new so both direct calls and rewriter paths are covered. Fixed a few subtle bugs along the way: Literal(0) and Literal(0.0) colliding due to Python's hash(0) == hash(0.0), a GC bug from storing into a weak dict without a local strong ref, and a traversal test that assumed unique node identities.

Copy link
Copy Markdown
Member

@willow-ahrens willow-ahrens left a comment

Choose a reason for hiding this comment

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

Term should inherit from hashCons, and TermTree should define hash_key

hash_cons_table[key] = obj
return obj
except TypeError:
return object.__new__(cls)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Define eq and hash as id

return (type(a), a)


class HashCons:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add an abstractmethod called Hash_keys which defines which keys are used in the hashcons object

_sig_cache: dict = {}


def _key_part(a: object) -> object:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This method would be disaggregated across child classes of HashCons

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