Skip to content

Commit 852649b

Browse files
committed
Only cache hashes on *lazy* arrays.
1 parent 3445020 commit 852649b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/iris/tests/unit/coords/test_AuxCoord.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -775,23 +775,29 @@ def test_nanbounds_eq_self(self):
775775
co1 = AuxCoord([15.0, 25.0], bounds=[[14.0, 16.0], [24.0, np.nan]])
776776
assert co1 == co1
777777

778-
def test_lazy_compares_via_hash(self):
778+
@pytest.mark.parametrize("bothlazy", [True, False], ids=["bothlazy", "onelazy"])
779+
def test_lazy_compares_via_hash(self, bothlazy):
779780
def lazify(coord):
780781
coord.bounds = coord.lazy_bounds()
781782

782783
co1 = AuxCoord([15.0, 25.0])
783784
co2 = AuxCoord([15.0, 25.001])
785+
784786
co1.points = co1.lazy_points()
785-
co2.points = co2.lazy_points()
787+
if bothlazy:
788+
co2.points = co2.lazy_points()
786789
assert co1.has_lazy_points()
787-
assert co2.has_lazy_points()
790+
assert co2.has_lazy_points() == bothlazy
791+
788792
assert not hasattr(co1.core_points(), "_iris_array_hash")
789-
assert not hasattr(co2.core_points(), "_iris_array_hash")
793+
if bothlazy:
794+
assert not hasattr(co2.core_points(), "_iris_array_hash")
790795

791796
eq = co1 == co2
792797
assert not eq
793798

794799
assert co1.has_lazy_points()
795-
assert co2.has_lazy_points()
796800
assert hasattr(co1.core_points(), "_iris_array_hash")
797-
assert hasattr(co2.core_points(), "_iris_array_hash")
801+
if bothlazy:
802+
assert co2.has_lazy_points()
803+
assert hasattr(co2.core_points(), "_iris_array_hash")

lib/iris/util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,16 @@ def normalise_array(array):
511511
if is_lazy_data(array1) or is_lazy_data(array2):
512512
# Compare lazy arrays by hashing, and cache the hashes...
513513
def array_hash(array):
514-
if not hasattr(array, "_iris_array_hash"):
514+
if hasattr(array, "_iris_array_hash"):
515+
hash = array._iris_array_hash
516+
else:
515517
from iris._concatenate import _hash_array
516518

517519
hash = _hash_array(array)
518-
array._iris_array_hash = hash
519-
return array._iris_array_hash
520+
if isinstance(array, da.Array):
521+
# Can't save hashes on a numpy array, but CAN on a Dask array
522+
array._iris_array_hash = hash
523+
return hash
520524

521525
eq = array_hash(array1) == array_hash(array2)
522526
# # Use a separate map and reduce operation to avoid running out of memory.

0 commit comments

Comments
 (0)