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: ########## File path: go/parquet/internal/encoding/boolean_decoder.go ########## @@ -0,0 +1,98 @@ +// 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" +) + +// PlainBooleanDecoder is for the Plain Encoding type, there is no +// dictionary decoding for bools. +type PlainBooleanDecoder struct { + decoder + + bitOffset int +} + +// Type for the PlainBooleanDecoder is parquet.Types.Boolean +func (PlainBooleanDecoder) Type() parquet.Type { + return parquet.Types.Boolean +} + +// Decode fills out with bools decoded from the data at the current point +// or until we reach the end of the data. +// +// Returns the number of values decoded +func (dec *PlainBooleanDecoder) Decode(out []bool) (int, error) { + max := utils.MinInt(len(out), dec.nvals) + + // if we aren't at a byte boundary, then get bools until we hit + // a byte boundary with the bit offset. + i := 0 + for dec.bitOffset != 0 && dec.bitOffset < 8 && i < max { + out[i] = (dec.data[0] & byte(1<<dec.bitOffset)) != 0 + dec.bitOffset++ + i++ + } + if dec.bitOffset == 8 { + dec.bitOffset = 0 + } + + // determine the number of full bytes worth of bits we can decode + // given the number of values we want to decode. + bitsRemain := max - i + batch := bitsRemain / 8 * 8 + if batch > 0 { // only go in here if there's at least one full byte to decode + if i > 0 { // skip our data forward if we decoded anything above + dec.data = dec.data[1:] + out = out[i:] + } + // determine the number of aligned bytes we can grab using SIMD optimized + // functions to improve performance. + alignedBytes := bitutil.BytesForBits(int64(batch)) + utils.BytesToBools(dec.data[:alignedBytes], out) + dec.data = dec.data[alignedBytes:] + out = out[alignedBytes*8:] + } + + // grab any trailing bits now that we've got our aligned bytes. + for ; dec.bitOffset < (bitsRemain - batch); dec.bitOffset++ { + out[dec.bitOffset] = (dec.data[0] & byte(1<<dec.bitOffset)) != 0 + } + + dec.nvals -= max + return max, nil +} + +// DecodeSpaced is like Decode except it expands the values to leave spaces for null +// as determined by the validBits bitmap. +func (dec *PlainBooleanDecoder) DecodeSpaced(out []bool, nullCount int, validBits []byte, validBitsOffset int64) (int, error) { + if nullCount > 0 { + toRead := len(out) - nullCount + valuesRead, err := dec.Decode(out[:toRead]) + if err != nil { + return 0, err + } + if valuesRead != toRead { + panic("parquet: number of values / definition levels read did not match") Review comment: updated to return an error, that's a good point. ########## File path: go/parquet/internal/encoding/boolean_decoder.go ########## @@ -0,0 +1,98 @@ +// 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" +) + +// PlainBooleanDecoder is for the Plain Encoding type, there is no +// dictionary decoding for bools. +type PlainBooleanDecoder struct { + decoder + + bitOffset int +} + +// Type for the PlainBooleanDecoder is parquet.Types.Boolean +func (PlainBooleanDecoder) Type() parquet.Type { + return parquet.Types.Boolean +} + +// Decode fills out with bools decoded from the data at the current point +// or until we reach the end of the data. +// +// Returns the number of values decoded +func (dec *PlainBooleanDecoder) Decode(out []bool) (int, error) { + max := utils.MinInt(len(out), dec.nvals) + + // if we aren't at a byte boundary, then get bools until we hit + // a byte boundary with the bit offset. + i := 0 + for dec.bitOffset != 0 && dec.bitOffset < 8 && i < max { + out[i] = (dec.data[0] & byte(1<<dec.bitOffset)) != 0 + dec.bitOffset++ + i++ + } + if dec.bitOffset == 8 { + dec.bitOffset = 0 + } + + // determine the number of full bytes worth of bits we can decode + // given the number of values we want to decode. + bitsRemain := max - i + batch := bitsRemain / 8 * 8 + if batch > 0 { // only go in here if there's at least one full byte to decode + if i > 0 { // skip our data forward if we decoded anything above + dec.data = dec.data[1:] + out = out[i:] + } + // determine the number of aligned bytes we can grab using SIMD optimized + // functions to improve performance. + alignedBytes := bitutil.BytesForBits(int64(batch)) + utils.BytesToBools(dec.data[:alignedBytes], out) + dec.data = dec.data[alignedBytes:] + out = out[alignedBytes*8:] + } + + // grab any trailing bits now that we've got our aligned bytes. + for ; dec.bitOffset < (bitsRemain - batch); dec.bitOffset++ { + out[dec.bitOffset] = (dec.data[0] & byte(1<<dec.bitOffset)) != 0 Review comment: done. ########## File path: go/parquet/internal/encoding/byte_array_decoder.go ########## @@ -0,0 +1,84 @@ +// 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 ( + "encoding/binary" + + "github.com/apache/arrow/go/parquet" + "github.com/apache/arrow/go/parquet/internal/utils" + "golang.org/x/xerrors" +) + +// PlainByteArrayDecoder decodes a data chunk for bytearrays according to +// the plain encoding. The byte arrays will use slices to reference the +// data rather than copying it. +type PlainByteArrayDecoder struct { + decoder +} + +// Type returns parquet.Types.ByteArray for this decoder +func (PlainByteArrayDecoder) Type() parquet.Type { + return parquet.Types.ByteArray +} + +// Decode will populate the slice of bytearrays in full or until the number +// of values is emptied. +// +// Returns the number of values that were decoded. +func (pbad *PlainByteArrayDecoder) Decode(out []parquet.ByteArray) (int, error) { + max := utils.MinInt(len(out), pbad.nvals) + + for i := 0; i < max; i++ { + // there should always be at least four bytes which is the length of the + // next value in the data. + if len(pbad.data) < 4 { + return i, xerrors.New("parquet: eof reading bytearray") + } + + // the first 4 bytes are a little endian uint32 length Review comment: So the parquet-format spec only says "length in 4 bytes little endian" and does not specify signed or unsigned and the C++ implementation uses an unsigned int32 which is why I used uint32. ########## File path: go/parquet/internal/encoding/byte_array_decoder.go ########## @@ -0,0 +1,84 @@ +// 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 ( + "encoding/binary" + + "github.com/apache/arrow/go/parquet" + "github.com/apache/arrow/go/parquet/internal/utils" + "golang.org/x/xerrors" +) + +// PlainByteArrayDecoder decodes a data chunk for bytearrays according to +// the plain encoding. The byte arrays will use slices to reference the +// data rather than copying it. +type PlainByteArrayDecoder struct { + decoder +} + +// Type returns parquet.Types.ByteArray for this decoder +func (PlainByteArrayDecoder) Type() parquet.Type { + return parquet.Types.ByteArray +} + +// Decode will populate the slice of bytearrays in full or until the number +// of values is emptied. +// +// Returns the number of values that were decoded. +func (pbad *PlainByteArrayDecoder) Decode(out []parquet.ByteArray) (int, error) { + max := utils.MinInt(len(out), pbad.nvals) + + for i := 0; i < max; i++ { + // there should always be at least four bytes which is the length of the + // next value in the data. + if len(pbad.data) < 4 { + return i, xerrors.New("parquet: eof reading bytearray") + } + + // the first 4 bytes are a little endian uint32 length + nbytes := int32(binary.LittleEndian.Uint32(pbad.data[:4])) + if nbytes < 0 { + return i, xerrors.New("parquet: invalid BYTE_ARRAY value") + } + + if int64(len(pbad.data)) < int64(nbytes)+4 { + return i, xerrors.New("parquet: eof reading bytearray") + } + + out[i] = pbad.data[4 : nbytes+4] Review comment: this is a view (in Go terms it's called a *slice*). It essentially creates an object which contains the pointer to the first byte, then length of the slice (so `nbytes`), and the capacity (the rest of the capacity that exists in `pbad.data`. In theory it might actually be better to specify the capacity in this slice so that a consumer couldn't expand the individual slice and clobber other data. I'll make that update. -- 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]
