Skip to content

Commit 896ccdf

Browse files
committed
Can't use cache for non-unit steps
1 parent 24a26a8 commit 896ccdf

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/blosc2/lazyexpr.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ def slices_eval( # noqa: C901
14751475
# Get the starts and stops for the slice
14761476
starts = [s.start if s.start is not None else 0 for s in cslice]
14771477
stops = [s.stop if s.stop is not None else sh for s, sh in zip(cslice, cslice_shape, strict=True)]
1478-
stepper = tuple(slice(None, None, s.step) for s in cslice)
1478+
unit_steps = np.all([s.step == 1 for s in cslice])
14791479

14801480
# Get the slice of each operand
14811481
for key, value in operands.items():
@@ -1495,9 +1495,9 @@ def slices_eval( # noqa: C901
14951495
key in chunk_operands
14961496
and cslice_shape == chunk_operands[key].shape
14971497
and isinstance(value, blosc2.NDArray)
1498+
and unit_steps
14981499
):
14991500
value.get_slice_numpy(chunk_operands[key], (starts, stops))
1500-
chunk_operands[key] = chunk_operands[key][stepper]
15011501
continue
15021502

15031503
chunk_operands[key] = value[cslice]
@@ -1871,8 +1871,8 @@ def reduce_slices( # noqa: C901
18711871
# get intersection of chunk and target
18721872
cslice = step_handler(cslice, _slice)
18731873
chunks_ = tuple(s.stop - s.start for s in cslice)
1874-
fast_path = False if any((s.step != 1 or s.step is not None) for s in cslice) else fast_path
1875-
if _slice == () and fast_path:
1874+
unit_steps = np.all([s.step == 1 for s in cslice])
1875+
if _slice == () and fast_path and unit_steps:
18761876
# Fast path
18771877
full_chunk = chunks_ == chunks
18781878
fill_chunk_operands(
@@ -1882,7 +1882,6 @@ def reduce_slices( # noqa: C901
18821882
# Get the starts and stops for the slice
18831883
starts = [s.start if s.start is not None else 0 for s in cslice]
18841884
stops = [s.stop if s.stop is not None else sh for s, sh in zip(cslice, chunks_, strict=True)]
1885-
stepper = tuple(slice(None, None, s.step) for s in cslice)
18861885
# Get the slice of each operand
18871886
for key, value in operands.items():
18881887
if np.isscalar(value):
@@ -1901,9 +1900,9 @@ def reduce_slices( # noqa: C901
19011900
key in chunk_operands
19021901
and chunks_ == chunk_operands[key].shape
19031902
and isinstance(value, blosc2.NDArray)
1903+
and unit_steps
19041904
):
19051905
value.get_slice_numpy(chunk_operands[key], (starts, stops))
1906-
chunk_operands[key] = chunk_operands[key][stepper]
19071906
continue
19081907
chunk_operands[key] = value[cslice]
19091908

tests/ndarray/test_lazyexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ def test_eval_getitem2():
10881088
np.testing.assert_allclose(expr[0], nres[0])
10891089
np.testing.assert_allclose(expr[1:, :7], nres[1:, :7])
10901090
np.testing.assert_allclose(expr[0:10:2], nres[0:10:2])
1091-
# This works, but it is not very efficient since it relies on blosc2.ndarray.slice for non-unit steps
1091+
# Now relies on inefficient blosc2.ndarray.slice for non-unit steps but only per chunk (not for whole result)
10921092
np.testing.assert_allclose(expr.slice((slice(None, None, None), slice(0, 10, 2)))[:], nres[:, 0:10:2])
10931093

10941094
# Small test for broadcasting
@@ -1097,7 +1097,7 @@ def test_eval_getitem2():
10971097
np.testing.assert_allclose(expr[0], nres[0])
10981098
np.testing.assert_allclose(expr[1:, :7], nres[1:, :7])
10991099
np.testing.assert_allclose(expr[:, 0:10:2], nres[:, 0:10:2])
1100-
# This works, but it is not very efficient since it relies on blosc2.ndarray.slice for non-unit steps
1100+
# Now relies on inefficient blosc2.ndarray.slice for non-unit steps but only per chunk (not for whole result)
11011101
np.testing.assert_allclose(expr.slice((slice(None, None, None), slice(0, 10, 2)))[:], nres[:, 0:10:2])
11021102

11031103

0 commit comments

Comments
 (0)