Skip to content

Commit

Permalink
Use siphash for persistent hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jul 3, 2024
1 parent 444c4b6 commit 376d6d0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions .pylintrc-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- arg: ignored-modules
val:
- matplotlib
- siphash24
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ classifiers = [
dependencies = [
"platformdirs>=2.2",
"typing-extensions>=4; python_version<'3.11'",
"siphash24",
]

[project.optional-dependencies]
Expand Down
11 changes: 7 additions & 4 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"""


import hashlib
import logging
import os
import pickle
Expand All @@ -52,6 +51,8 @@
cast,
)

from siphash24 import siphash13


if TYPE_CHECKING:
from _typeshed import ReadableBuffer
Expand Down Expand Up @@ -160,7 +161,7 @@ class KeyBuilder:

# this exists so that we can (conceivably) switch algorithms at some point
# down the road
new_hash: Callable[..., Hash] = hashlib.sha256
new_hash: Callable[..., Hash] = siphash13

def rec(self, key_hash: Hash, key: Any) -> Hash:
"""
Expand Down Expand Up @@ -301,7 +302,8 @@ def update_for_frozenset(self, key_hash: Hash, key: FrozenSet[Any]) -> None:

unordered_hash(
key_hash,
(self.rec(self.new_hash(), key_i).digest() for key_i in key))
(self.rec(self.new_hash(), key_i).digest() for key_i in key),
hash_constructor=self.new_hash)

update_for_FrozenOrderedSet = update_for_frozenset # noqa: N815

Expand Down Expand Up @@ -351,7 +353,8 @@ def update_for_frozendict(self, key_hash: Hash, key: Mapping[Any, Any]) -> None:

unordered_hash(
key_hash,
(self.rec(self.new_hash(), (k, v)).digest() for k, v in key.items()))
(self.rec(self.new_hash(), (k, v)).digest() for k, v in key.items()),
hash_constructor=self.new_hash)

update_for_immutabledict = update_for_frozendict
update_for_constantdict = update_for_frozendict
Expand Down

0 comments on commit 376d6d0

Please sign in to comment.