Fokko commented on code in PR #4505:
URL: https://github.com/apache/iceberg-python/pull/4505#discussion_r4759022587
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3233,14 +3245,43 @@ def bin_pack_record_batches(batches:
Iterable[pa.RecordBatch], target_file_size:
``ParquetWriter`` with ``OutputStream.tell()`` (#2998).
"""
buffer: list[pa.RecordBatch] = []
- buffer_bytes = 0
+ buffer_bytes = 0.0
for batch in batches:
- buffer.append(batch)
- buffer_bytes += batch.nbytes
- if buffer_bytes >= target_file_size:
- yield buffer
- buffer = []
- buffer_bytes = 0
+ if batch.num_rows == 0:
+ buffer.append(batch)
+ continue
+ offset = 0
+ while offset < batch.num_rows:
+ remaining_rows = batch.num_rows - offset
+ space = target_file_size - buffer_bytes
+ fit = remaining_rows if batch.nbytes == 0 else int(space *
batch.num_rows / batch.nbytes)
+ if fit >= remaining_rows:
+ buffer.append(batch.slice(offset))
+ buffer_bytes += batch.nbytes * remaining_rows / batch.num_rows
+ offset = batch.num_rows
+ if buffer_bytes >= target_file_size:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ elif fit > 0:
+ buffer.append(batch.slice(offset, fit))
+ buffer_bytes += batch.nbytes * fit / batch.num_rows
+ offset += fit
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ elif buffer:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ else:
+ buffer.append(batch.slice(offset, 1))
+ buffer_bytes += batch.nbytes / batch.num_rows
+ offset += 1
+ if buffer_bytes >= target_file_size:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
Review Comment:
The four branches share most of their logic, and the flush is repeated four
times. They can be collapsed into a single path. On 3,000 random inputs this
produces the same bins as the current loop. It keeps the `space * num_rows /
nbytes` order of operations, so the exact-target case still lands exactly on
the target:
```suggestion
for batch in batches:
batch_bytes = batch.nbytes
if batch.num_rows == 0 or batch_bytes == 0:
buffer.append(batch)
continue
offset = 0
while offset < batch.num_rows:
rows = min(batch.num_rows - offset, int((target_file_size -
buffer_bytes) * batch.num_rows / batch_bytes))
if rows == 0 and buffer:
yield buffer
buffer, buffer_bytes = [], 0.0
continue
# A single row larger than the target is kept whole in its own
bin.
rows = max(rows, 1)
buffer.append(batch.slice(offset, rows))
buffer_bytes += batch_bytes * rows / batch.num_rows
offset += rows
if offset < batch.num_rows or buffer_bytes >= target_file_size:
yield buffer
buffer, buffer_bytes = [], 0.0
```
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3233,14 +3245,43 @@ def bin_pack_record_batches(batches:
Iterable[pa.RecordBatch], target_file_size:
``ParquetWriter`` with ``OutputStream.tell()`` (#2998).
"""
buffer: list[pa.RecordBatch] = []
- buffer_bytes = 0
+ buffer_bytes = 0.0
for batch in batches:
- buffer.append(batch)
- buffer_bytes += batch.nbytes
- if buffer_bytes >= target_file_size:
- yield buffer
- buffer = []
- buffer_bytes = 0
+ if batch.num_rows == 0:
+ buffer.append(batch)
+ continue
+ offset = 0
+ while offset < batch.num_rows:
+ remaining_rows = batch.num_rows - offset
+ space = target_file_size - buffer_bytes
+ fit = remaining_rows if batch.nbytes == 0 else int(space *
batch.num_rows / batch.nbytes)
Review Comment:
Nit: `batch.nbytes` isn't cached. Each access walks the buffers, which takes
about 2.5 µs for a small two-column batch, and this loop reads it up to three
times per pass. With 200k 10-row batches and a 128 MB target,
`bin_pack_record_batches` takes 1.52 s here versus 0.52 s on `main`. Reading it
once per batch brings that back to 0.60 s. The suggestion on the loop below
includes this.
--
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]