HaoYang670 commented on code in PR #2127:
URL: https://github.com/apache/arrow-rs/pull/2127#discussion_r928684036


##########
arrow/src/array/builder/null_buffer_builder.rs:
##########
@@ -0,0 +1,199 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::buffer::Buffer;
+
+use super::BooleanBufferBuilder;
+
+/// Builder for creating the null bit buffer.
+/// This builder only materializes the buffer when we append `false`.
+/// If you only append `true`s to the builder, what you get will be
+/// `None` when calling [`finish`](#method.finish).
+/// This optimization is **very** important for the performance.
+#[derive(Debug)]
+pub(super) struct NullBufferBuilder {
+    bitmap_builder: Option<BooleanBufferBuilder>,
+    /// Store the length of the buffer before materilaizing.
+    len: usize,
+    capacity: usize,
+}
+
+impl NullBufferBuilder {
+    /// Creates a new empty builder.
+    /// `capacity` is the number of bits in the null buffer.
+    pub fn new(capacity: usize) -> Self {
+        Self {
+            bitmap_builder: None,
+            len: 0,
+            capacity,
+        }
+    }
+
+    /// Appends `n` `true`s into the builder.
+    #[inline]
+    pub fn append_n_true(&mut self, n: usize) {
+        if let Some(buf) = self.bitmap_builder.as_mut() {
+            buf.append_n(n, true)
+        } else {
+            self.len += n;
+        }
+    }
+
+    /// Appends a `true` into the builder.
+    #[inline]
+    pub fn append_true(&mut self) {
+        if let Some(buf) = self.bitmap_builder.as_mut() {
+            buf.append(true)
+        } else {
+            self.len += 1;
+        }
+    }
+
+    /// Appends `n` `false`s into the builder.
+    #[inline]
+    pub fn append_n_false(&mut self, n: usize) {
+        self.materialize_if_needed();
+        self.bitmap_builder.as_mut().unwrap().append_n(n, false);
+    }
+
+    /// Appends a `false` into the builder.
+    #[inline]
+    pub fn append_false(&mut self) {
+        self.materialize_if_needed();
+        self.bitmap_builder.as_mut().unwrap().append(false);
+    }
+
+    /// Appends a boolean value into the builder.
+    #[inline]
+    pub fn append(&mut self, v: bool) {
+        if v {
+            self.append_true()
+        } else {
+            self.append_false()
+        }
+    }
+
+    /// Appends a boolean slice into the builder.
+    pub fn append_slice(&mut self, slice: &[bool]) {
+        if slice.iter().any(|v| !v) {
+            self.materialize_if_needed()
+        }
+        if let Some(buf) = self.bitmap_builder.as_mut() {
+            buf.append_slice(slice)
+        } else {
+            self.len += slice.len();
+        }
+    }
+
+    /// Builds the null buffer and resets the builder.
+    /// Returns `None` if the builder only contains `true`s.
+    pub fn finish(&mut self) -> Option<Buffer> {
+        let buf = self.bitmap_builder.as_mut().map(|b| b.finish());
+        self.bitmap_builder = None;
+        self.len = 0;
+        buf
+    }
+
+    #[inline]
+    fn materialize_if_needed(&mut self) {
+        if self.bitmap_builder.is_none() {
+            self.materialize()
+        }
+    }
+
+    #[cold]
+    fn materialize(&mut self) {
+        if self.bitmap_builder.is_none() {
+            let mut b = BooleanBufferBuilder::new(self.len.max(self.capacity));

Review Comment:
   Thank you @jhorstmann. That's a good catch, which I think we could gain some 
performance improvement from it.



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to