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

Remove zstandard dependency in favor of numcodecs #1838

Merged
merged 8 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 4 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies = [
'numpy>=1.24',
'fasteners',
'numcodecs>=0.10.0',
'crc32c',
'typing_extensions',
]
dynamic = [
"version",
Expand Down Expand Up @@ -52,17 +54,11 @@ docs = [
'pydata-sphinx-theme',
'numpydoc',
'numcodecs[msgpack]',
"msgpack",
"lmdb",
"zstandard",
"crc32c",
'msgpack',
'lmdb',
]
extra = [
'attrs',
'cattrs',
'msgpack',
'crc32c',
'zstandard'
]
optional = [
'lmdb',
Expand Down Expand Up @@ -101,10 +97,7 @@ extra-dependencies = [
"pytest-cov",
"msgpack",
"lmdb",
"zstandard",
"crc32c",
"pytest-asyncio",
"typing_extensions",
"mypy"
]
features = ["extra"]
Expand Down
24 changes: 12 additions & 12 deletions src/zarr/codecs/zstd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations
from functools import cached_property
from typing import TYPE_CHECKING
from dataclasses import dataclass


from zstandard import ZstdCompressor, ZstdDecompressor
from numcodecs.zstd import Zstd

from zarr.abc.codec import BytesBytesCodec
from zarr.codecs.registry import register_codec
Expand All @@ -15,6 +16,8 @@
from zarr.config import RuntimeConfiguration
from zarr.common import BytesLike, JSON, ArraySpec

DEFAULT_ZSTD_LEVEL = 3
normanrz marked this conversation as resolved.
Show resolved Hide resolved


def parse_zstd_level(data: JSON) -> int:
if isinstance(data, int):
Expand All @@ -34,10 +37,10 @@ def parse_checksum(data: JSON) -> bool:
class ZstdCodec(BytesBytesCodec):
is_fixed_size = True

level: int = 0
level: int = DEFAULT_ZSTD_LEVEL
checksum: bool = False

def __init__(self, *, level: int = 0, checksum: bool = False) -> None:
def __init__(self, *, level: int = DEFAULT_ZSTD_LEVEL, checksum: bool = False) -> None:
level_parsed = parse_zstd_level(level)
checksum_parsed = parse_checksum(checksum)

Expand All @@ -52,29 +55,26 @@ def from_dict(cls, data: Dict[str, JSON]) -> Self:
def to_dict(self) -> Dict[str, JSON]:
return {"name": "zstd", "configuration": {"level": self.level, "checksum": self.checksum}}

def _compress(self, data: bytes) -> bytes:
ctx = ZstdCompressor(level=self.level, write_checksum=self.checksum)
return ctx.compress(data)

def _decompress(self, data: bytes) -> bytes:
ctx = ZstdDecompressor()
return ctx.decompress(data)
@cached_property
def _zstd_codec(self) -> Zstd:
config_dict = {"level": self.level, "checksum": self.checksum}
return Zstd.from_config(config_dict)

async def decode(
self,
chunk_bytes: bytes,
_chunk_spec: ArraySpec,
_runtime_configuration: RuntimeConfiguration,
) -> BytesLike:
return await to_thread(self._decompress, chunk_bytes)
return await to_thread(self._zstd_codec.decode, chunk_bytes)

async def encode(
self,
chunk_bytes: bytes,
_chunk_spec: ArraySpec,
_runtime_configuration: RuntimeConfiguration,
) -> Optional[BytesLike]:
return await to_thread(self._compress, chunk_bytes)
return await to_thread(self._zstd_codec.encode, chunk_bytes)

def compute_encoded_size(self, _input_byte_length: int, _chunk_spec: ArraySpec) -> int:
raise NotImplementedError
Expand Down
Loading