This is an automated email from the ASF dual-hosted git repository.

Jefffrey pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new b5349cbc36 perf: avoid re-allocation if buffer is not shared during 
`BooleanArray::take_n_true` (#10438)
b5349cbc36 is described below

commit b5349cbc3667998b0281dfad3287680e4882d4c8
Author: RIchard Baah <[email protected]>
AuthorDate: Wed Aug 19 21:27:59 2026 -0400

    perf: avoid re-allocation if buffer is not shared during 
`BooleanArray::take_n_true` (#10438)
    
    # Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax.
    -->
    
    - Closes #10251.
    
    # Rationale for this change
    If a boolean buffer is not shared we should re-use its allocation
    instead of building a new builder from scratch.
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    # What changes are included in this PR?
    match on `into_mutable`, in the error case the old path is taken. If the
    buffer can be re-used we use it directly.
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    # Are these changes tested?
    yes, existing test cover this behavior
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    
    If this PR claims a performance improvement, please include evidence
    such as benchmark results.
    -->
    
    # Are there any user-facing changes?
    no
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    
    If there are any breaking changes to public APIs, please call them out.
    -->
    
    ---------
    
    Co-authored-by: rich-T-kid <[email protected]>
    Co-authored-by: Jeffrey Vo <[email protected]>
---
 arrow-array/src/array/boolean_array.rs | 43 ++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/arrow-array/src/array/boolean_array.rs 
b/arrow-array/src/array/boolean_array.rs
index 4b0a8304d4..0e5df12825 100644
--- a/arrow-array/src/array/boolean_array.rs
+++ b/arrow-array/src/array/boolean_array.rs
@@ -607,10 +607,35 @@ impl BooleanArray {
             return self;
         };
 
-        let mut builder = BooleanBufferBuilder::new(len);
-        builder.append_buffer(&self.values.slice(0, end));
-        builder.append_n(len - end, false);
-        BooleanArray::new(builder.finish(), self.nulls)
+        let bit_offset = self.values.offset();
+        let inner_buf = self.values.into_inner();
+
+        match inner_buf.into_mutable() {
+            Ok(mut mutable_buffer) => {
+                // Unique ownership: zero trailing bits in place.
+                let actual_end = bit_offset + end;
+                let raw_bytes = mutable_buffer.as_slice_mut();
+                let byte_idx = actual_end / 8;
+                let bits_to_keep = actual_end % 8;
+                if bits_to_keep == 0 {
+                    raw_bytes[byte_idx..].fill(0);
+                } else {
+                    raw_bytes[byte_idx] &= (1_u8 << bits_to_keep) - 1;
+                    raw_bytes[byte_idx + 1..].fill(0);
+                }
+                BooleanArray::new(
+                    BooleanBuffer::new(mutable_buffer.into(), bit_offset, len),
+                    self.nulls,
+                )
+            }
+            Err(buf) => {
+                // Shared buffer: copy the retained prefix then pad with false.
+                let mut builder = BooleanBufferBuilder::new(len);
+                builder.append_buffer(&BooleanBuffer::new(buf, bit_offset, 
end));
+                builder.append_n(len - end, false);
+                BooleanArray::new(builder.finish(), self.nulls)
+            }
+        }
     }
 
     /// Deconstruct this array into its constituent parts
@@ -1715,4 +1740,14 @@ mod tests {
         assert_eq!(r.len(), 0);
         assert_eq!(r.true_count(), 0);
     }
+
+    #[test]
+    fn test_take_n_true_unique_buffer() {
+        // unique buffer ownership -> mutable in-place path.
+        let arr = BooleanArray::from(vec![true, false, true, true, false, 
true, true]);
+        let result = arr.take_n_true(3);
+        assert_eq!(result.true_count(), 3);
+        let values: Vec<bool> = (0..result.len()).map(|i| 
result.value(i)).collect();
+        assert_eq!(values, vec![true, false, true, true, false, false, false]);
+    }
 }

Reply via email to