Skip to content

Commit 9f99259

Browse files
committed
Remove check for top speed (simplify code)
1 parent 61ef8f4 commit 9f99259

1 file changed

Lines changed: 49 additions & 55 deletions

File tree

src/blosc2/blosc2_ext.pyx

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,64 +1945,58 @@ cdef int aux_miniexpr(me_udata *udata, int64_t nchunk, int32_t nblock,
19451945
for i in range(udata.ninputs):
19461946
ndarr = udata.inputs[i]
19471947
input_buffers[i] = malloc(ndarr.sc.blocksize)
1948-
# A way to check for top speed
1949-
if False:
1950-
buf = <float *>input_buffers[i]
1951-
for j in range(ndarr.blocknitems):
1952-
buf[j] = 1.
1948+
if ndarr.sc.storage.urlpath == NULL:
1949+
src = ndarr.sc.data[nchunk]
19531950
else:
1954-
if ndarr.sc.storage.urlpath == NULL:
1955-
src = ndarr.sc.data[nchunk]
1956-
else:
1957-
# We need to get the chunk from disk/network
1951+
# We need to get the chunk from disk/network
1952+
if ndarr.chunk_cache.nchunk != nchunk:
1953+
PyThread_acquire_lock(chunk_cache_lock, 1)
19581954
if ndarr.chunk_cache.nchunk != nchunk:
1959-
PyThread_acquire_lock(chunk_cache_lock, 1)
1960-
if ndarr.chunk_cache.nchunk != nchunk:
1961-
if ndarr.chunk_cache.data != NULL:
1962-
free(ndarr.chunk_cache.data)
1963-
ndarr.chunk_cache.data = NULL
1964-
rc = blosc2_schunk_get_chunk(ndarr.sc, nchunk, &chunk, &needs_free)
1965-
if rc < 0:
1955+
if ndarr.chunk_cache.data != NULL:
1956+
free(ndarr.chunk_cache.data)
1957+
ndarr.chunk_cache.data = NULL
1958+
rc = blosc2_schunk_get_chunk(ndarr.sc, nchunk, &chunk, &needs_free)
1959+
if rc < 0:
1960+
PyThread_release_lock(chunk_cache_lock)
1961+
raise ValueError("miniexpr: error getting chunk")
1962+
if not needs_free:
1963+
src = <uint8_t*> malloc(rc)
1964+
if src == NULL:
19661965
PyThread_release_lock(chunk_cache_lock)
1967-
raise ValueError("miniexpr: error getting chunk")
1968-
if not needs_free:
1969-
src = <uint8_t*> malloc(rc)
1970-
if src == NULL:
1971-
PyThread_release_lock(chunk_cache_lock)
1972-
raise MemoryError("miniexpr: cannot allocate chunk copy")
1973-
memcpy(src, chunk, rc)
1974-
else:
1975-
src = chunk
1976-
ndarr.chunk_cache.data = src
1977-
ndarr.chunk_cache.nchunk = nchunk
1978-
PyThread_release_lock(chunk_cache_lock)
1979-
src = ndarr.chunk_cache.data
1980-
rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, &block_nbytes)
1981-
if rc < 0:
1982-
raise ValueError("miniexpr: error getting cbuffer sizes")
1983-
input_typesize = ndarr.sc.typesize
1984-
blocknitems = block_nbytes // input_typesize
1985-
if expected_blocknitems == -1:
1986-
expected_blocknitems = blocknitems
1987-
elif blocknitems != expected_blocknitems:
1988-
raise ValueError("miniexpr: inconsistent block element counts across inputs")
1989-
start = nblock * blocknitems
1990-
# A way to check for top speed
1991-
if False:
1992-
# Unsafe, but it works for special arrays (e.g. blosc2.ones), and can be fast
1993-
dctx = ndarr.sc.dctx
1994-
else:
1995-
# This is needed for thread safety, but adds a pretty low overhead (< 400ns on a modern CPU)
1996-
# In the future, perhaps one can create a specific (serial) context just for
1997-
# blosc2_getitem_ctx, but this is probably never going to be necessary.
1998-
dctx = blosc2_create_dctx(BLOSC2_DPARAMS_DEFAULTS)
1999-
if valid_nitems > blocknitems:
2000-
raise ValueError("miniexpr: valid items exceed padded block size")
2001-
rc = blosc2_getitem_ctx(dctx, src, chunk_cbytes, start, blocknitems,
2002-
input_buffers[i], block_nbytes)
2003-
blosc2_free_ctx(dctx)
2004-
if rc < 0:
2005-
raise ValueError("miniexpr: error decompressing the chunk")
1966+
raise MemoryError("miniexpr: cannot allocate chunk copy")
1967+
memcpy(src, chunk, rc)
1968+
else:
1969+
src = chunk
1970+
ndarr.chunk_cache.data = src
1971+
ndarr.chunk_cache.nchunk = nchunk
1972+
PyThread_release_lock(chunk_cache_lock)
1973+
src = ndarr.chunk_cache.data
1974+
rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, &block_nbytes)
1975+
if rc < 0:
1976+
raise ValueError("miniexpr: error getting cbuffer sizes")
1977+
input_typesize = ndarr.sc.typesize
1978+
blocknitems = block_nbytes // input_typesize
1979+
if expected_blocknitems == -1:
1980+
expected_blocknitems = blocknitems
1981+
elif blocknitems != expected_blocknitems:
1982+
raise ValueError("miniexpr: inconsistent block element counts across inputs")
1983+
start = nblock * blocknitems
1984+
# A way to check for top speed
1985+
if False:
1986+
# Unsafe, but it works for special arrays (e.g. blosc2.ones), and can be fast
1987+
dctx = ndarr.sc.dctx
1988+
else:
1989+
# This is needed for thread safety, but adds a pretty low overhead (< 400ns on a modern CPU)
1990+
# In the future, perhaps one can create a specific (serial) context just for
1991+
# blosc2_getitem_ctx, but this is probably never going to be necessary.
1992+
dctx = blosc2_create_dctx(BLOSC2_DPARAMS_DEFAULTS)
1993+
if valid_nitems > blocknitems:
1994+
raise ValueError("miniexpr: valid items exceed padded block size")
1995+
rc = blosc2_getitem_ctx(dctx, src, chunk_cbytes, start, blocknitems,
1996+
input_buffers[i], block_nbytes)
1997+
blosc2_free_ctx(dctx)
1998+
if rc < 0:
1999+
raise ValueError("miniexpr: error decompressing the chunk")
20062000
# For reduction operations, we need to track which block we're processing
20072001
# The linear_block_index should be based on the INPUT array structure, not the output array
20082002
# Get the first input array's chunk and block structure

0 commit comments

Comments
 (0)