-
Notifications
You must be signed in to change notification settings - Fork 99
[Parquet] Add SIMD-accelerated byte-stream-split decoding #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
daniel-adam-tfs
wants to merge
12
commits into
apache:main
Choose a base branch
from
daniel-adam-tfs:feature/optimize-bss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8c82e7
Add SIMD-accelerated byte-stream-split decoding
daniel-adam-tfs a390615
fixup! Add SIMD-accelerated byte-stream-split decoding
daniel-adam-tfs b135312
perf(parquet/encoding): add optimized FLBA width-2 byte stream split …
daniel-adam-tfs ac6d583
Update byte-stream split decoding tests
daniel-adam-tfs 2318a5c
Update byte-stream split decoding
daniel-adam-tfs 67a394b
Use SIMD implementations if available
daniel-adam-tfs 6f90a7e
Update suffix loop in _decodeByteStreamSplitWidth4AVX2 and _decodeByt…
daniel-adam-tfs 8f338f1
Fix assertions in byte-stream split decoding functions
daniel-adam-tfs d38b3b1
Fix byte-stream split decoding on big endian machines
daniel-adam-tfs cbeec7b
Simplify the assert messages to avoid allocations
daniel-adam-tfs 84110b5
Add test cases to byte-stream split benchmarks
daniel-adam-tfs 13c8c15
fixup! Simplify the assert messages to avoid allocations
daniel-adam-tfs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // 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. | ||
|
|
||
| //go:build !noasm | ||
|
|
||
| package encoding | ||
|
|
||
| import ( | ||
| "unsafe" | ||
|
|
||
| "github.com/apache/arrow-go/v18/parquet/internal/debug" | ||
| "golang.org/x/sys/cpu" | ||
| ) | ||
|
|
||
| func init() { | ||
| if cpu.X86.HasAVX2 { | ||
| decodeByteStreamSplitBatchWidth4InByteOrder = decodeByteStreamSplitBatchWidth4AVX2 | ||
| decodeByteStreamSplitBatchWidth8InByteOrder = decodeByteStreamSplitBatchWidth8AVX2 | ||
| } | ||
| } | ||
|
|
||
| //go:noescape | ||
| func _decodeByteStreamSplitWidth4AVX2(data, out unsafe.Pointer, nValues, stride int) | ||
|
|
||
| //go:noescape | ||
| func _decodeByteStreamSplitWidth8AVX2(data, out unsafe.Pointer, nValues, stride int) | ||
|
|
||
| func decodeByteStreamSplitBatchWidth4AVX2(data []byte, nValues, stride int, out []byte) { | ||
| if nValues == 0 { | ||
| return | ||
| } | ||
| const width = 4 | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| debug.Assert(len(data) >= 3*stride+nValues, "not enough data for decoding") | ||
| _decodeByteStreamSplitWidth4AVX2(unsafe.Pointer(&data[0]), unsafe.Pointer(&out[0]), nValues, stride) | ||
| } | ||
|
|
||
| func decodeByteStreamSplitBatchWidth8AVX2(data []byte, nValues, stride int, out []byte) { | ||
| if nValues == 0 { | ||
| return | ||
| } | ||
| const width = 8 | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| debug.Assert(len(data) >= 7*stride+nValues, "not enough data for decoding") | ||
| _decodeByteStreamSplitWidth8AVX2(unsafe.Pointer(&data[0]), unsafe.Pointer(&out[0]), nValues, stride) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // 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. | ||
|
|
||
| //go:build !noasm | ||
|
|
||
| package encoding | ||
|
|
||
| import ( | ||
| "unsafe" | ||
|
|
||
| "github.com/apache/arrow-go/v18/parquet/internal/debug" | ||
| "golang.org/x/sys/cpu" | ||
| ) | ||
|
|
||
| func init() { | ||
| if cpu.ARM64.HasASIMD { | ||
| decodeByteStreamSplitBatchWidth4InByteOrder = decodeByteStreamSplitBatchWidth4NEON | ||
| decodeByteStreamSplitBatchWidth8InByteOrder = decodeByteStreamSplitBatchWidth8NEON | ||
| } | ||
| } | ||
|
|
||
| //go:noescape | ||
| func _decodeByteStreamSplitWidth4NEON(data, out unsafe.Pointer, nValues, stride int) | ||
|
|
||
| //go:noescape | ||
| func _decodeByteStreamSplitWidth8NEON(data, out unsafe.Pointer, nValues, stride int) | ||
|
|
||
| func decodeByteStreamSplitBatchWidth4NEON(data []byte, nValues, stride int, out []byte) { | ||
| if nValues == 0 { | ||
| return | ||
| } | ||
| const width = 4 | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| debug.Assert(len(data) >= 3*stride+nValues, "not enough data for decoding") | ||
| _decodeByteStreamSplitWidth4NEON(unsafe.Pointer(&data[0]), unsafe.Pointer(&out[0]), nValues, stride) | ||
| } | ||
|
|
||
| func decodeByteStreamSplitBatchWidth8NEON(data []byte, nValues, stride int, out []byte) { | ||
| if nValues == 0 { | ||
| return | ||
| } | ||
| const width = 8 | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| debug.Assert(len(data) >= 7*stride+nValues, "not enough data for decoding") | ||
| _decodeByteStreamSplitWidth8NEON(unsafe.Pointer(&data[0]), unsafe.Pointer(&out[0]), nValues, stride) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,43 +19,98 @@ | |
| package encoding | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "unsafe" | ||
|
|
||
| "github.com/apache/arrow-go/v18/parquet" | ||
| "github.com/apache/arrow-go/v18/parquet/internal/debug" | ||
| ) | ||
|
|
||
| // decodeByteStreamSplitBatchWidth4InByteOrder decodes the batch of nValues raw bytes representing a 4-byte datatype provided | ||
| // by 'data', into the output buffer 'out' using BYTE_STREAM_SPLIT encoding. The values are expected to be in little-endian | ||
| // by 'data', into the output buffer 'out' using BYTE_STREAM_SPLIT encoding. The values are expected to be in big-endian | ||
| // byte order and are be decoded into the 'out' array in machine's native endianness. | ||
| // 'out' must have space for at least len(data) bytes. | ||
| func decodeByteStreamSplitBatchWidth4InByteOrder(data []byte, nValues, stride int, out []byte) { | ||
| func decodeByteStreamSplitBatchWidth4InByteOrderDefault(data []byte, nValues, stride int, out []byte) { | ||
| const width = 4 | ||
| debug.Assert(len(out) >= nValues*width, fmt.Sprintf("not enough space in output buffer for decoding, out: %d bytes, data: %d bytes", len(out), len(data))) | ||
| for element := 0; element < nValues; element++ { | ||
| // Big Endian: most significant byte first | ||
| out[width*element+0] = data[3*stride+element] | ||
| out[width*element+1] = data[2*stride+element] | ||
| out[width*element+2] = data[stride+element] | ||
| out[width*element+3] = data[element] | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| // the beginning of the data slice can be truncated, but for valid encoding we need at least (width-1)*stride+nValues bytes | ||
| debug.Assert(len(data) >= 3*stride+nValues, "not enough data for decoding") | ||
| s0 := data[:nValues] | ||
| s1 := data[stride : stride+nValues] | ||
| s2 := data[2*stride : 2*stride+nValues] | ||
| s3 := data[3*stride : 3*stride+nValues] | ||
| out = out[:width*nValues] | ||
| out32 := unsafe.Slice((*uint32)(unsafe.Pointer(&out[0])), nValues) | ||
| for i := range nValues { | ||
| // Big-endian machine: put s0 as MSB, s3 as LSB | ||
| out32[i] = uint32(s3[i])<<24 | uint32(s2[i])<<16 | uint32(s1[i])<<8 | uint32(s0[i]) | ||
| } | ||
| } | ||
|
|
||
| // decodeByteStreamSplitBatchWidth8InByteOrder decodes the batch of nValues raw bytes representing a 8-byte datatype provided | ||
| // by 'data', into the output buffer 'out' using BYTE_STREAM_SPLIT encoding. The values are expected to be in little-endian | ||
| // by 'data', into the output buffer 'out' using BYTE_STREAM_SPLIT encoding. The values are expected to be in big-endian | ||
| // byte order and are be decoded into the 'out' array in machine's native endianness. | ||
| // 'out' must have space for at least len(data) bytes. | ||
| func decodeByteStreamSplitBatchWidth8InByteOrder(data []byte, nValues, stride int, out []byte) { | ||
| func decodeByteStreamSplitBatchWidth8InByteOrderDefault(data []byte, nValues, stride int, out []byte) { | ||
| const width = 8 | ||
| debug.Assert(len(out) >= nValues*width, fmt.Sprintf("not enough space in output buffer for decoding, out: %d bytes, data: %d bytes", len(out), len(data))) | ||
| debug.Assert(len(out) >= nValues*width, "not enough space in output buffer for decoding") | ||
| debug.Assert(len(data) >= 7*stride+nValues, "not enough data for decoding") | ||
| s0 := data[:nValues] | ||
| s1 := data[stride : stride+nValues] | ||
| s2 := data[2*stride : 2*stride+nValues] | ||
| s3 := data[3*stride : 3*stride+nValues] | ||
| s4 := data[4*stride : 4*stride+nValues] | ||
| s5 := data[5*stride : 5*stride+nValues] | ||
| s6 := data[6*stride : 6*stride+nValues] | ||
| s7 := data[7*stride : 7*stride+nValues] | ||
| out = out[:width*nValues] | ||
| out64 := unsafe.Slice((*uint64)(unsafe.Pointer(&out[0])), nValues) | ||
| for i := range nValues { | ||
| // Big-endian machine: put s0 as MSB, s7 as LSB | ||
| out64[i] = uint64(s7[i])<<56 | uint64(s6[i])<<48 | uint64(s5[i])<<40 | uint64(s4[i])<<32 | | ||
| uint64(s3[i])<<24 | uint64(s2[i])<<16 | uint64(s1[i])<<8 | uint64(s0[i]) | ||
| } | ||
| } | ||
|
|
||
| // decodeByteStreamSplitBatchFLBAWidth2 decodes the batch of nValues FixedLenByteArrays of length 2 provided by 'data', | ||
| // into the output slice 'out' using BYTE_STREAM_SPLIT encoding. | ||
| // 'out' must have space for at least nValues slices. | ||
| func decodeByteStreamSplitBatchFLBAWidth2(data []byte, nValues, stride int, out []parquet.FixedLenByteArray) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zeroshade what is the interpretation of BSS encoding when applied to a byte array? Since everything is little-endian in parquet, data is little endian. But what about the out array, should the bytes be reordered on big-endian? |
||
| debug.Assert(len(out) >= nValues, "not enough space in output slice for decoding") | ||
| debug.Assert(len(data) >= stride+nValues, "not enough data for decoding") | ||
| for element := 0; element < nValues; element++ { | ||
| out[element][0] = data[element] | ||
| out[element][1] = data[stride+element] | ||
| } | ||
| } | ||
|
|
||
| // decodeByteStreamSplitBatchFLBAWidth4 decodes the batch of nValues FixedLenByteArrays of length 4 provided by 'data', | ||
| // into the output slice 'out' using BYTE_STREAM_SPLIT encoding. | ||
| // 'out' must have space for at least nValues slices. | ||
| func decodeByteStreamSplitBatchFLBAWidth4(data []byte, nValues, stride int, out []parquet.FixedLenByteArray) { | ||
| debug.Assert(len(out) >= nValues, "not enough space in output slice for decoding") | ||
| debug.Assert(len(data) >= 3*stride+nValues, "not enough data for decoding") | ||
| for element := 0; element < nValues; element++ { | ||
| out[element][0] = data[element] | ||
| out[element][1] = data[stride+element] | ||
| out[element][2] = data[stride*2+element] | ||
| out[element][3] = data[stride*3+element] | ||
| } | ||
| } | ||
|
|
||
| // decodeByteStreamSplitBatchFLBAWidth8 decodes the batch of nValues FixedLenByteArrays of length 8 provided by 'data', | ||
| // into the output slice 'out' using BYTE_STREAM_SPLIT encoding. | ||
| // 'out' must have space for at least nValues slices. | ||
| func decodeByteStreamSplitBatchFLBAWidth8(data []byte, nValues, stride int, out []parquet.FixedLenByteArray) { | ||
| debug.Assert(len(out) >= nValues, "not enough space in output slice for decoding") | ||
| debug.Assert(len(data) >= 7*stride+nValues, "not enough data for decoding") | ||
| for element := 0; element < nValues; element++ { | ||
| // Big Endian: most significant byte first | ||
| out[width*element+0] = data[7*stride+element] | ||
| out[width*element+1] = data[6*stride+element] | ||
| out[width*element+2] = data[5*stride+element] | ||
| out[width*element+3] = data[4*stride+element] | ||
| out[width*element+4] = data[3*stride+element] | ||
| out[width*element+5] = data[2*stride+element] | ||
| out[width*element+6] = data[stride+element] | ||
| out[width*element+7] = data[element] | ||
| out[element][0] = data[element] | ||
| out[element][1] = data[stride+element] | ||
| out[element][2] = data[stride*2+element] | ||
| out[element][3] = data[stride*3+element] | ||
| out[element][4] = data[stride*4+element] | ||
| out[element][5] = data[stride*5+element] | ||
| out[element][6] = data[stride*6+element] | ||
| out[element][7] = data[stride*7+element] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // 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/v18/parquet" | ||
| "github.com/apache/arrow-go/v18/parquet/internal/debug" | ||
| ) | ||
|
|
||
| var ( | ||
| decodeByteStreamSplitBatchWidth4InByteOrder func(data []byte, nValues, stride int, out []byte) = decodeByteStreamSplitBatchWidth4InByteOrderDefault | ||
| decodeByteStreamSplitBatchWidth8InByteOrder func(data []byte, nValues, stride int, out []byte) = decodeByteStreamSplitBatchWidth8InByteOrderDefault | ||
| ) | ||
|
|
||
| // decodeByteStreamSplitBatchFLBA decodes the batch of nValues FixedLenByteArrays provided by 'data', | ||
| // into the output slice 'out' using BYTE_STREAM_SPLIT encoding. | ||
| // 'out' must have space for at least nValues slices. | ||
| func decodeByteStreamSplitBatchFLBA(data []byte, nValues, stride, width int, out []parquet.FixedLenByteArray) { | ||
| debug.Assert(len(out) >= nValues, "not enough space in output slice for decoding") | ||
| debug.Assert(len(data) >= (width-1)*stride+nValues, "not enough data for decoding") | ||
| for stream := 0; stream < width; stream++ { | ||
| for element := 0; element < nValues; element++ { | ||
| encLoc := stride*stream + element | ||
| out[element][stream] = data[encLoc] | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the allocations by fmt.Sprintf messed with the timings, I think the shorter messages are fine for the asserts