Skip to content
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

KeyBuilder: change int to direct str representation #261

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 1 addition & 16 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ class KeyBuilder:
may stop working as early as 2022.

.. versionadded:: 2021.2

.. note::

Some key-building uses system byte order, so the built keys may not match
across different systems. It would be desirable to fix this, but this is
not yet done.
"""

# this exists so that we can (conceivably) switch algorithms at some point
Expand Down Expand Up @@ -283,16 +277,7 @@ def update_for_type(key_hash: Hash, key: type) -> None:

@staticmethod
def update_for_int(key_hash: Hash, key: int) -> None:
sz = 8
while True:
try:
# Must match system byte order so that numpy and this
# generate the same string of bytes.
# https://github.com/inducer/pytools/issues/259
key_hash.update(key.to_bytes(sz, byteorder=sys.byteorder, signed=True))
return
except OverflowError:
sz *= 2
key_hash.update(str(key).encode("utf-8"))

@classmethod
def update_for_enum(cls, key_hash: Hash, key: Enum) -> None:
Expand Down
13 changes: 10 additions & 3 deletions pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,16 @@ def test_scalar_hashing() -> None:
assert keyb(np.int32(1)) == keyb(np.int32(1))
assert keyb(np.int32(2)) != keyb(np.int32(1))
assert keyb(np.int64(1)) == keyb(np.int64(1))
assert keyb(1) == keyb(np.int64(1))
assert keyb(1) != keyb(np.int64(1))
Copy link
Owner

Choose a reason for hiding this comment

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

Wait, I thought the goal was to keep them the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From your answer to my question in #260, my interpretation was that they don't need to be the same. Changing the code like in this PR perhaps also addresses #262.

assert keyb(1) != keyb(np.int32(1))

assert keyb(1) == keyb("1")
assert keyb(1.0) != keyb("1.0") # '1.0' uses hex representation
assert keyb(1+1j) == keyb("(1+1j)")
Comment on lines +438 to +440
Copy link
Contributor Author

@matthiasdiener matthiasdiener Sep 3, 2024

Choose a reason for hiding this comment

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

Not sure if this is an issue. If it is, we could change the KeyBuilder to (int, str(key)).


assert keyb(np.float64(1.0)) != keyb(1.0)
assert keyb(np.complex128(1.0+2.0j)) != keyb(1.0+2.0j)

assert keyb(np.longlong(1)) == keyb(np.longlong(1))

assert keyb(np.float16(1.1)) == keyb(np.float16(1.1))
Expand Down Expand Up @@ -566,7 +573,7 @@ class MyDC:
name: str
value: int

assert keyb(MyDC("hi", 1)) == "d1a1079f1c10aa4f"
assert keyb(MyDC("hi", 1)) == "72c25f5d0ac2bda7"

assert keyb(MyDC("hi", 1)) == keyb(MyDC("hi", 1))
assert keyb(MyDC("hi", 1)) != keyb(MyDC("hi", 2))
Expand All @@ -590,7 +597,7 @@ class MyAttrs:
name: str
value: int

assert (keyb(MyAttrs("hi", 1)) == "5b6c5da60eb2bd0f") # type: ignore[call-arg]
assert (keyb(MyAttrs("hi", 1)) == "6e8bfc4394f82985") # type: ignore[call-arg]

assert keyb(MyAttrs("hi", 1)) == keyb(MyAttrs("hi", 1)) # type: ignore[call-arg]
assert keyb(MyAttrs("hi", 1)) != keyb(MyAttrs("hi", 2)) # type: ignore[call-arg]
Expand Down
Loading