@@ -34,6 +34,8 @@ def __init__(self, parent: BatchStore, nbatch: int, lazybatch: bytes) -> None:
3434 self ._nbatch = nbatch
3535 self ._lazybatch = lazybatch
3636 self ._items : list [Any ] | None = None
37+ self ._cached_block_index : int | None = None
38+ self ._cached_block : list [Any ] | None = None
3739 self ._nbytes , self ._cbytes , self ._nblocks = blosc2 .get_cbuffer_sizes (lazybatch )
3840
3941 def _normalize_index (self , index : int ) -> int :
@@ -51,6 +53,14 @@ def _decode_items(self) -> list[Any]:
5153 self ._items = [item for block in blocks for item in block ]
5254 return self ._items
5355
56+ def _get_block (self , block_index : int ) -> list [Any ]:
57+ if self ._cached_block_index == block_index and self ._cached_block is not None :
58+ return self ._cached_block
59+ block = msgpack_unpackb (self ._parent .schunk .get_vlblock (self ._nbatch , block_index ))
60+ self ._cached_block_index = block_index
61+ self ._cached_block = block
62+ return block
63+
5464 def __getitem__ (self , index : int | slice ) -> Any | list [Any ]:
5565 if isinstance (index , slice ):
5666 items = self ._decode_items ()
@@ -64,7 +74,7 @@ def __getitem__(self, index: int | slice) -> Any | list[Any]:
6474 block_index , item_index = divmod (index , blocksize_max )
6575 if block_index >= self ._nblocks :
6676 raise IndexError ("Batch index out of range" )
67- block = msgpack_unpackb ( self ._parent . schunk . get_vlblock ( self . _nbatch , block_index ) )
77+ block = self ._get_block ( block_index )
6878 try :
6979 return block [item_index ]
7080 except IndexError as exc :
0 commit comments