zeroshade commented on a change in pull request #10379:
URL: https://github.com/apache/arrow/pull/10379#discussion_r644345648



##########
File path: go/parquet/internal/encoding/boolean_encoder.go
##########
@@ -0,0 +1,112 @@
+// 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.
+
+package encoding
+
+import (
+       "github.com/apache/arrow/go/arrow/bitutil"
+       "github.com/apache/arrow/go/parquet"
+       "github.com/apache/arrow/go/parquet/internal/utils"
+)
+
+const boolBufSize = 1024
+
+// PlainBooleanEncoder encodes bools as a bitmap as per the Plain Encoding
+type PlainBooleanEncoder struct {
+       encoder
+       nbits      int
+       bitsBuffer []byte
+}
+
+// Type for the PlainBooleanEncoder is parquet.Types.Boolean
+func (PlainBooleanEncoder) Type() parquet.Type {
+       return parquet.Types.Boolean
+}
+
+// Put encodes the contents of in into the underlying data buffer.
+func (enc *PlainBooleanEncoder) Put(in []bool) {
+       if enc.bitsBuffer == nil {
+               enc.bitsBuffer = make([]byte, boolBufSize)
+       }
+
+       bitOffset := 0
+       // first check if we are in the middle of a byte due to previous
+       // encoding of data and finish out that byte's bits.
+       if enc.nbits > 0 {
+               bitsToWrite := utils.MinInt(enc.nbits, len(in))
+               beg := (boolBufSize * 8) - enc.nbits
+               for i, val := range in[:bitsToWrite] {
+                       bitmask := uint8(1 << uint((beg+i)%8))
+                       if val {
+                               enc.bitsBuffer[(beg+i)/8] |= bitmask

Review comment:
       So the combination of the changes I made to how I handle the 
bitmapwriter and utilize an `AppendBools` function resulted in about a 40% 
improvement in speed of encoding bools! It also turns out that Go definitely 
does optimize the /8 and the *8 as far as i can tell since there was very 
little performance difference between using them or the shifts directly. 
   
   The primary benefits that resulted in the performance improvements came from 
switching to use `bitutil.SetBit` / `bitutil.ClearBit` since they used the 
constant slices with the bitmasks that we index into rather than constructing 
them on the fly as it converted a modulus+shift operation into a simple index 
lookup inside of a tight loop.
   
   The big reason why it needed an AppendBools function instead of just using a 
loop with `Next()`, `Set()`, and `Clear()` is because `Next()` has to check for 
bitmask being 0 to update it along with the offset and position tracking, 
whereas having the function inside there allows for a single update of the 
bookkeeping and just looping through to set the bits. Either way, the result is 
a significant performance improvement so I'm happy :smiley:




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to