You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/22 19:03:23 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2127: Lazily materialize the null buffer builder for all array builders.

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


##########
arrow/src/array/builder/mod.rs:
##########
@@ -53,6 +54,7 @@ pub use generic_binary_builder::GenericBinaryBuilder;
 pub use generic_list_builder::GenericListBuilder;
 pub use generic_string_builder::GenericStringBuilder;
 pub use map_builder::MapBuilder;
+pub(self) use null_buffer_builder::NullBufferBuilder;

Review Comment:
   How is `pub(self) use` different from just `use`



##########
arrow/src/array/builder/null_buffer_builder.rs:
##########
@@ -0,0 +1,185 @@
+// 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>,
+    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.
+    pub fn append_n_true(&mut self, n: usize) {
+        if let Some(buf) = self.bitmap_builder.as_mut() {
+            buf.append_n(n, true)
+        }
+        self.len += n;
+    }
+
+    /// Appends a `true` into the builder.
+    #[inline]
+    pub fn append_true(&mut self) {
+        self.append_n_true(1);
+    }
+
+    /// Appends `n` `false`s into the builder.
+    pub fn append_n_false(&mut self, n: usize) {
+        self.materialize_if_needed();
+        self.bitmap_builder.as_mut().unwrap().append_n(n, false);
+        self.len += n;
+    }
+
+    /// Appends a `false` into the builder.
+    #[inline]
+    pub fn append_false(&mut self) {
+        self.append_n_false(1);
+    }
+
+    /// Appends a boolean value into the builder.
+    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)
+        }
+        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());

Review Comment:
   Could use take here 



-- 
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-unsubscribe@arrow.apache.org

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