Skip to content

Commit b639672

Browse files
dodamihclaude
andcommitted
optim: Store numpy arrays directly in LRU cache instead of re-encoding
When lru_encoding is "raw", store decoded numpy arrays directly in the LRU cache instead of re-serializing them via encode_raw (which does np.asfortranarray + tobytes). This avoids significant overhead when reading non-"raw" data. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c62e2c3 commit b639672

File tree

1 file changed

+32
-30
lines changed
  • cloudvolume/datasource/precomputed/image

1 file changed

+32
-30
lines changed

cloudvolume/datasource/precomputed/image/rx.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -466,26 +466,23 @@ def repopulate_lru_from_shm(
466466
))
467467

468468
encoding = lru_encoding
469-
if encoding == "same":
470-
# Since the parallel version populates the LRU via an image and
471-
# you don't get the benefit of accessing the raw downloaded bytes,
472-
# there will be a performance regression for "same" since e.g.
473-
# jpeg -> img -> jpeg will instead of decode -> img,lru you'll
474-
# have decode -> img -> encode -> lru. Therefore, this is hacky,
475-
# but backwards compatible and strictly expands the capabilities
476-
# of the LRU.
477-
encoding = "raw" # would ordinarily be: meta.encoding(mip)
478-
479-
for chunkname in core_chunks[-lru.size:]:
480-
bbx = Bbox.from_filename(chunkname)
481-
bbx -= requested_bbox.minpt
482-
img3d = renderbuffer[ bbx.to_slices() ]
483-
binary = chunks.encode(
484-
img3d, encoding,
485-
meta.compressed_segmentation_block_size(mip),
486-
compression_params=meta.compression_params(mip),
487-
)
488-
lru[chunkname] = (encoding, binary)
469+
if encoding in ("same", "raw"):
470+
for chunkname in core_chunks[-lru.size:]:
471+
bbx = Bbox.from_filename(chunkname)
472+
bbx -= requested_bbox.minpt
473+
img3d = np.copy(renderbuffer[ bbx.to_slices() ])
474+
lru[chunkname] = ("numpy", img3d)
475+
else:
476+
for chunkname in core_chunks[-lru.size:]:
477+
bbx = Bbox.from_filename(chunkname)
478+
bbx -= requested_bbox.minpt
479+
img3d = renderbuffer[ bbx.to_slices() ]
480+
binary = chunks.encode(
481+
img3d, encoding,
482+
meta.compressed_segmentation_block_size(mip),
483+
compression_params=meta.compression_params(mip),
484+
)
485+
lru[chunkname] = (encoding, binary)
489486

490487
def child_process_download(
491488
meta, cache,
@@ -580,27 +577,32 @@ def download_chunk(
580577
if lru is not None:
581578
lru[filename] = (encoding, content)
582579

583-
img3d = decode_fn(
584-
meta, filename, content,
585-
fill_missing, mip,
586-
background_color=background_color,
587-
encoding=encoding,
588-
)
580+
if encoding == "numpy":
581+
img3d = content
582+
else:
583+
img3d = decode_fn(
584+
meta, filename, content,
585+
fill_missing, mip,
586+
background_color=background_color,
587+
encoding=encoding,
588+
)
589589

590-
if lru is not None and full_decode:
591-
if lru_encoding not in [ "same", encoding ]:
590+
if lru is not None and full_decode:
591+
if lru_encoding == "raw" and encoding != "numpy":
592+
lru[filename] = ("numpy", img3d)
593+
elif lru_encoding not in [ "same", "raw", encoding ]:
592594
content = None
593595
if img3d is not None:
594596
block_size = meta.compressed_segmentation_block_size(mip)
595597
if block_size is None:
596598
block_size = (8,8,8)
597599

598600
content = chunks.encode(
599-
img3d, lru_encoding,
601+
img3d, lru_encoding,
600602
block_size,
601603
compression_params=meta.compression_params(mip),
602604
)
603-
605+
604606
lru[filename] = (lru_encoding, content)
605607

606608
return img3d, bbox

0 commit comments

Comments
 (0)