ranflarion opened a new pull request, #27818:
URL: https://github.com/apache/datafusion/pull/27818

   ## Which issue does this PR close?
   
   - Closes #27817.
   
   ## Rationale for this change
   
   Since #26960 the spill writer copies each run of IPC buffers under 64 KiB 
into one `Vec` before writing it, with no bound on the `Vec`. A batch whose 
buffers are all small is copied whole, in memory the pool does not account for, 
while an operator is spilling. The issue has the measurements and a reproducer.
   
   ## What changes are included in this PR?
   
   `coalesce_buffers` becomes an iterator, `CoalescedBuffers`, that 
`IPCStreamWriter` and `AsyncIPCStreamWriter` consume as they write. It copies a 
run of small buffers in pieces of at most 1 MiB, each allocated at its exact 
size when the writer reaches it and dropped after its write. Buffers of 64 KiB 
or more are written directly as before, and a small buffer with no small 
neighbour is passed through instead of copied.
   
   Peak live heap during one `append_batch`, above the level just before the 
call, for batches of 4,096-row `Int64` columns, main → this PR:
   
   | batch | uncompressed | zstd | lz4_frame |
   |---|---:|---:|---:|
   | 206 columns, 6.8 MB | 6.8 → 1.1 MB | 10.2 → 7.9 MB | 14.2 → 15.2 MB |
   | 512 columns, 16.8 MB | 27.0 → 1.2 MB | 30.4 → 18.1 MB | 34.5 → 35.4 MB |
   
   With a codec, what remains is the compressed buffers the encoder holds until 
they are written. `lz4_frame` gains up to 1 MiB because those buffers already 
peaked above the old copy, which was built while they were being freed.
   
   The 1 MiB bound is the smallest I measured that does not cost time. Spilling 
30 of the 206-column batches uncompressed (Apple M4 Pro, best of 25 runs, 
alternating builds) takes 36 ms on main, 38 ms with pieces capped at 64 KiB, 27 
to 28 ms at 256 KiB and 25 ms at 1 MiB. With the 1 MiB bound the other shapes I 
timed (8,192-row batches, string columns, 64-row batches, all three codecs) are 
within run-to-run spread of main or faster, and the zstd and lz4_frame cases of 
`spill_io` are within 3% of main. I have only measured on macOS, where a 
`write` call is relatively expensive, and the uncompressed `spill_io` cases 
were too noisy on my machine to compare.
   
   ## What is the testing strategy for this PR?
   
   `test_coalesced_buffers_bound_each_copy` passes 1.2 MB of small buffers 
followed by a large one through `CoalescedBuffers` and checks the size of each 
copy, that the large buffer is not copied, and the bytes. 
`test_async_ipc_stream_writer` still checks that a small batch takes one write, 
and the existing spill round-trip tests cover both writers.
   
   ## Are there any user-facing changes?
   
   No.
   


-- 
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]

Reply via email to