Skip to content

Commit aa75677

Browse files
authored
Revert "Revert "Use zstandard to decompress chunks to save memory (#977)" (…" (#981)
This reverts commit 07a1168.
1 parent 7b64aae commit aa75677

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

.github/workflows/pytest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ jobs:
4242
- name: Install requirements for Python 3.11
4343
if: matrix.python-version == '3.11'
4444
run: pip install git+https://github.com/XENONnT/base_environment.git --force-reinstall
45+
- name: Install strax
46+
run: pip install .
4547
- name: Start MongoDB
4648
uses: supercharge/mongodb-github-action@1.11.0
4749
with:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ scipy = "*"
4343
tqdm = ">=4.46.0"
4444
zarr = "<3.0.0"
4545
zstd = "*"
46+
zstandard = "*"
4647
sphinx = { version = "*", optional = true }
4748
sphinx_rtd_theme = { version = "*", optional = true }
4849
nbsphinx = { version = "*", optional = true }

strax/io.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import blosc
99
import zstd
10+
import zstandard
1011
import lz4.frame as lz4
1112
from ast import literal_eval
1213

@@ -40,9 +41,11 @@ def _bz2_decompress(f, buffer_size=DECOMPRESS_BUFFER_SIZE):
4041
_zstd_compress = lambda data: zstd.compress(data, 3, 1)
4142

4243

43-
def _zstd_decompress(f):
44-
data = f.read()
45-
data = zstd.decompress(data)
44+
def _zstd_decompress(f, chunk_size=64 * 1024 * 1024):
45+
decompressor = zstandard.ZstdDecompressor().decompressobj()
46+
data = bytearray() # Efficient mutable storage
47+
for d in iter(lambda: f.read(chunk_size), b""):
48+
data.extend(decompressor.decompress(d))
4649
return data
4750

4851

@@ -181,5 +184,10 @@ def load_chunk(chunk_info):
181184
x = load_chunk(chunk_info)
182185
x = strax.apply_selection(x, **kwargs)
183186
results.append(x)
184-
results = np.hstack(results)
187+
188+
# No need to hstack if only one chunk is loaded
189+
if len(results) == 1:
190+
results = results[0]
191+
else:
192+
results = np.hstack(results)
185193
return results if len(results) else np.empty(0, dtype)

0 commit comments

Comments
 (0)