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


##########
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 materializing.
+    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;

Review Comment:
   This causes the inconsistency between `self.len` and `bitmap_builder.len`. 
Also, I don't want to the struct to store `self.len` and `self.capacity` after 
materializing.
   
   My thought is to create a private enum, named `NullBufferBuilderVariant`:
   ```rust
   enum NullBufferBuilderVariant {
       PreMaterialized {len: usize, capacity: usize},
       Materialized(builder: BooleanBufferBuilder)
   }
   
   pub(super) struct NullBufferBuilder {
       variant: NullBufferBuilderVariant,
   }
   ...
   ```
   Hope this will not introduce performance regression.
   I will file a follow-up to track this. 



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