Skip to content

Commit

Permalink
speed up endianess determination
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener committed Jan 16, 2025
1 parent 3046e99 commit 63e0455
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class RecommendedHashNotFoundWarning(UserWarning):
_HAS_ATTRS = True


IS_BIGENDIAN = sys.byteorder == "big"


logger = logging.getLogger(__name__)

# NOTE: not always available so they get hardcoded here
Expand Down Expand Up @@ -271,6 +274,8 @@ def update_for_type(self, key_hash: Hash, key: type) -> None:

def update_for_int(self, key_hash: Hash, key: int) -> None:
sz = 8
# Note: this must match the hash for numpy integers, since
# np.int64(1) == 1
while True:
try:
key_hash.update(key.to_bytes(sz, byteorder="little", signed=True))
Expand Down Expand Up @@ -326,7 +331,7 @@ def update_for_specific_dtype(self, key_hash: Hash, key: Any) -> None:

def update_for_numpy_scalar(self, key_hash: Hash, key: Any) -> None:
import numpy as np
if sys.byteorder == "big":
if IS_BIGENDIAN:
key = key.byteswap()
if hasattr(np, "complex256") and key.dtype == np.dtype("complex256"):
key_hash.update(repr(complex(key)).encode("utf8"))
Expand Down

0 comments on commit 63e0455

Please sign in to comment.