Repository navigation
perf(deletion-vector): avoid payload copies and bitmap reserialization #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| _DV_BLOB_CRC = struct.Struct(">I") | ||
| _DV_BLOB_MAGIC_NUMBER = 1681511377 | ||
| _ROARING_BITMAP_COUNT_SIZE_BYTES = 8 | ||
| _ROARING_BITMAP_KEY_SIZE_BYTES = 4 | ||
| _DV_BLOB_MIN_SIZE_BYTES = _DV_BLOB_LENGTH.size + _DV_BLOB_MAGIC.size + _ROARING_BITMAP_COUNT_SIZE_BYTES + _DV_BLOB_CRC.size | ||
|
|
||
|
|
||
|
|
@@ -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. | ||
| offset += bitmap.__sizeof__() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another optimization: |
||
| bitmaps.append(bitmap) | ||
|
|
||
| last_key = key | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requires PyRoaring ≥1.1.0 for |
||
| "cachetools>=5.5", | ||
| "zstandard>=0.13.0" | ||
| ] | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
memoryviewis the main optimization here: it lets us advance through the payload without repeatedly copying the remaining bytes.