adriangb commented on PR #12184:
URL: https://github.com/apache/arrow-rs/pull/12184#issuecomment-7225259659

   @Jefffrey one design question that popped up by reviewing 
https://github.com/apache/datafusion/pull/27391
   
   Should `minify` merge many small view data buffers? Today it only copies a 
view data buffer if some of its bytes are unreferenced, or its allocation is 
oversized. A buffer that is fully used is kept as is. That means this case is a 
no-op:
   
   ```rust
   // 50 one-row arrays, each owning one small data buffer
   let parts: Vec<StringViewArray> = ...;
   let merged = concat(&parts)?;
   ```
   
   | | data buffers |
   |---|---|
   | after `concat` | 50 |
   | after `minify` | 50 |
   | after `gc()` | 1 |
   
   No bytes are wasted, so by the current definition there is nothing to do. 
But arrays like this are expensive in other ways: every `Buffer` has its own 
allocation and refcount, and anything that copies the buffer list per row goes 
quadratic. That is exactly what bit DataFusion in 
https://github.com/apache/datafusion/issues/27390 (5.9 s vs 30 ms). The fix 
there (https://github.com/apache/datafusion/pull/27391) adds a recursive `gc` 
helper, and DataFusion has a second one for `array_agg`. I'd like `minify` to 
be able to replace both, and right now it can't.
   
   Options I see:
   
   | | Behavior | Cost |
   |---|---|---|
   | A | Keep as is, list it under "Limitations" | Callers that care about 
buffer count still need their own recursive `gc` |
   | B | Also merge small buffers: if an array has 2+ data buffers under some 
size, copy those into one | Copies bytes that weren't wasted. Bounded to the 
small buffers, big ones are still reused |
   | C | Same as B but behind an option (`minify_with_options`) | More API 
surface for a new kernel |
   
   I lean towards B. With a size threshold it keeps the properties we / callers 
of `minify` care about:
   
   - still a no-op on arrays that are already in good shape (at most one small 
buffer left, so a second call copies nothing)
   - large, fully used buffers are never copied
   - the extra CPU is proportional to the bytes in small buffers, which is the 
case where the copy pays for itself
   
   The downsides are that the threshold is a magic number (builder block size, 
8 KiB? something else?), and that if another array still holds the old buffers, 
memory goes up until it drops them. The second one is already true for 
oversized shared buffers and is documented.
   
   If B feels like too much policy for a kernel, we can do A in this PR and C 
as a follow-up. Adding `minify_with_options` later doesn't break anything. 
Changing the default later is the part that would be awkward, which is why I'm 
asking now.
   
   What do you think?


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

Reply via email to