kevinjqliu commented on code in PR #4181:
URL: https://github.com/apache/iceberg-python/pull/4181#discussion_r4568979021
##########
pyiceberg/table/deletion_vector.py:
##########
@@ -49,30 +50,32 @@ def __init__(self, referenced_data_file: str, bitmaps:
list[BitMap]) -> None:
self._bitmaps = bitmaps
@staticmethod
- def _deserialize_bitmap(pl: bytes) -> list[BitMap]:
- number_of_bitmaps = int.from_bytes(pl[0:8], byteorder="little")
- pl = pl[8:]
+ def _deserialize_bitmap(payload: bytes) -> list[BitMap]:
+ payload_view = memoryview(payload)
+ offset = _ROARING_BITMAP_COUNT_SIZE_BYTES
+ number_of_bitmaps = int.from_bytes(payload_view[:offset],
byteorder="little")
bitmaps = []
last_key = -1
+
for _ in range(number_of_bitmaps):
- key = int.from_bytes(pl[0:4], byteorder="little")
+ key = int.from_bytes(payload_view[offset : offset +
_ROARING_BITMAP_KEY_SIZE_BYTES], byteorder="little")
if key < 0:
raise ValueError(f"Invalid unsigned key: {key}")
if key <= last_key:
raise ValueError("Keys must be sorted in ascending order")
if key > MAX_JAVA_SIGNED:
raise ValueError(f"Key {key} is too large, max
{MAX_JAVA_SIGNED} to maintain compatibility with Java impl")
- pl = pl[4:]
+ offset += _ROARING_BITMAP_KEY_SIZE_BYTES
while last_key < key - 1:
bitmaps.append(EMPTY_BITMAP)
last_key += 1
- bm = BitMap().deserialize(pl)
- # TODO: Optimize this
- pl = pl[len(bm.serialize()) :]
- bitmaps.append(bm)
+ bitmap = BitMap.deserialize(payload_view[offset:])
+ # PyRoaring's __sizeof__ returns the portable serialized size
without allocating bytes.
+ offset += bitmap.__sizeof__()
Review Comment:
another optimization: `__sizeof__()` uses the [same size calculation as
`serialize()`](https://github.com/Ezibenroc/PyRoaringBitMap/blob/1.1.0/pyroaring/abstract_bitmap.pxi#L775-L812),
without allocating or serializing bytes.
##########
pyproject.toml:
##########
@@ -40,7 +40,7 @@ dependencies = [
"fsspec>=2023.1.0",
"pyparsing>=3.1.0",
"tenacity>=8.2.3",
- "pyroaring>=1.0.0",
+ "pyroaring>=1.1.0",
Review Comment:
Requires PyRoaring ≥1.1.0 for `memoryview` support; see the [updated
`deserialize()`
signature](https://github.com/Ezibenroc/PyRoaringBitMap/blob/1.1.0/pyroaring/abstract_bitmap.pxi#L790-L798).
##########
pyiceberg/table/deletion_vector.py:
##########
@@ -49,30 +50,32 @@ def __init__(self, referenced_data_file: str, bitmaps:
list[BitMap]) -> None:
self._bitmaps = bitmaps
@staticmethod
- def _deserialize_bitmap(pl: bytes) -> list[BitMap]:
- number_of_bitmaps = int.from_bytes(pl[0:8], byteorder="little")
- pl = pl[8:]
+ def _deserialize_bitmap(payload: bytes) -> list[BitMap]:
+ payload_view = memoryview(payload)
Review Comment:
Using `memoryview` is the main optimization here: it lets us advance through
the payload without repeatedly copying the remaining bytes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]