Skip to content

Commit 23596c1

Browse files
authored
perf:remove isinstance check (#3704)
* remove isinstance check inside bytescodec * remove isinstance check inside bytescodec * changelog
1 parent e03cfc8 commit 23596c1

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

changes/3704.misc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove an expensive `isinstance` check from the bytes codec decoding routine.

src/zarr/codecs/bytes.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
from enum import Enum
66
from typing import TYPE_CHECKING
77

8-
import numpy as np
9-
108
from zarr.abc.codec import ArrayBytesCodec
11-
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
9+
from zarr.core.buffer import Buffer, NDBuffer
1210
from zarr.core.common import JSON, parse_enum, parse_named_configuration
1311
from zarr.core.dtype.common import HasEndianness
1412

@@ -72,20 +70,15 @@ async def _decode_single(
7270
chunk_bytes: Buffer,
7371
chunk_spec: ArraySpec,
7472
) -> NDBuffer:
75-
assert isinstance(chunk_bytes, Buffer)
7673
# TODO: remove endianness enum in favor of literal union
7774
endian_str = self.endian.value if self.endian is not None else None
7875
if isinstance(chunk_spec.dtype, HasEndianness):
7976
dtype = replace(chunk_spec.dtype, endianness=endian_str).to_native_dtype() # type: ignore[call-arg]
8077
else:
8178
dtype = chunk_spec.dtype.to_native_dtype()
8279
as_array_like = chunk_bytes.as_array_like()
83-
if isinstance(as_array_like, NDArrayLike):
84-
as_nd_array_like = as_array_like
85-
else:
86-
as_nd_array_like = np.asanyarray(as_array_like)
8780
chunk_array = chunk_spec.prototype.nd_buffer.from_ndarray_like(
88-
as_nd_array_like.view(dtype=dtype)
81+
as_array_like.view(dtype=dtype) # type: ignore[attr-defined]
8982
)
9083

9184
# ensure correct chunk shape

0 commit comments

Comments
 (0)