VanshajPoonia commented on code in PR #12307:
URL: https://github.com/apache/arrow-rs/pull/12307#discussion_r4805389279


##########
arrow-select/src/coalesce/byte_view.rs:
##########
@@ -107,11 +107,19 @@ impl<B: ByteViewType> InProgressByteViewArray<B> {
 
     /// Finishes in progress buffer, if any
     fn finish_current(&mut self) {
-        let Some(next_buffer) = self.current.take() else {
-            return;
-        };
-        let buffer: Buffer = next_buffer.into();
+        if let Some(current) = self.current.take() {
+            self.push_completed(current);
+        }
+    }
 
+    /// Appends `buffer` to the completed buffers, shrinking it first if less
+    /// than half of it is used: blocks are often closed early, and the output
+    /// array would otherwise keep (and report) capacity it never uses.
+    fn push_completed(&mut self, mut buffer: Vec<u8>) {
+        if buffer.len() < buffer.capacity() / 2 {

Review Comment:
   Question. shrink_to_fit is a realloc and may copy. The benchmark numbers in 
the PR are noisy as the PR says. Has coalesce_kernels been run on a quieter 
machine? Also len < capacity / 2 allows up to 2x slack on a block that is 50 to 
99 percent full. Is 2x the intended bound?



##########
arrow-select/src/coalesce/byte_view.rs:
##########
@@ -953,44 +950,31 @@ impl<B: ByteViewType> InProgressArray for 
InProgressByteViewArray<B> {
     }
 }
 
-const STARTING_BLOCK_SIZE: usize = 4 * 1024; // (note the first size used is 
actually 8KiB)
+const MIN_BLOCK_SIZE: usize = 64 * 1024; // 64KiB

Review Comment:
   Question about the start size. Each output batch now begins at 64 KiB and 
doubles to 1 MiB. For a batch of about 1.6 MB of copied strings that is about 5 
buffers where 1 MiB blocks would need 2. Was the buffer count measured with 
take or filter on the output? Also a coalescer per partition and column now 
holds up to 64 KiB while it accumulates. Did you try a smaller start such as 16 
KiB?



##########
arrow-select/src/coalesce.rs:
##########
@@ -1821,6 +1791,43 @@ mod tests {
             .run();
     }
 
+    /// Copied strings must not be put in blocks sized by earlier batches
+    /// (<https://github.com/apache/arrow-rs/issues/12305>)
+    #[test]
+    fn test_string_view_copied_blocks_track_each_batch() {
+        let source = StringViewArray::from_iter_values(
+            (0..100_000).map(|i| format!("this string is longer than 12 bytes 
{i}")),
+        );
+        let schema = Arc::new(Schema::new(vec![Field::new(
+            "c0",
+            DataType::Utf8View,
+            false,
+        )]));
+        let mut coalescer = BatchCoalescer::new(Arc::clone(&schema), 1000);
+        let mut output = vec![];
+        // small takes of a large array point into buffers they barely use, so
+        // the coalescer copies their strings
+        for start in (0..100_000).step_by(10) {
+            let indices = UInt32Array::from_iter_values(start..start + 10);
+            let column = take(&source, &indices, None).unwrap();
+            let batch = RecordBatch::try_new(Arc::clone(&schema), 
vec![column]).unwrap();
+            coalescer.push_batch(batch).unwrap();
+            while let Some(batch) = coalescer.next_completed_batch() {
+                output.push(batch);
+            }
+        }
+        assert_eq!(output.len(), 100);
+        for batch in &output {
+            let array = col_as_string_view("c0", batch);
+            let len: usize = array.data_buffers().iter().map(|b| 
b.len()).sum();
+            let capacity: usize = array.data_buffers().iter().map(|b| 
b.capacity()).sum();
+            assert!(

Review Comment:
   In this test each batch holds about 40,000 bytes in a 64 KiB block. That is 
more than half. So from reading the code push_completed never shrinks here and 
the assertion would pass without the shrink. The test does cover the per batch 
reset. Could one case use a smaller batch size so a block closes under half 
full?



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