This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 35a974f1 perf(parquet): speed up delta byte array prefix matching
(#1388)
35a974f1 is described below
commit 35a974f1b9e988fd44736dbd4a3290638d0d16b5
Author: Minh Vu <[email protected]>
AuthorDate: Tue Oct 20 21:24:00 2026 +0200
perf(parquet): speed up delta byte array prefix matching (#1388)
## What
- Compare common prefixes eight bytes at a time.
## Why
- Reduce work for long shared prefixes in DELTA_BYTE_ARRAY encoding.
## Implementation
- Use little-endian word loads and trailing-zero count.
- Keep byte-wise handling for short tails.
## Benchmark
- AMD EPYC, isolated comparison, 0 allocations: 256-byte prefix 202 → 43
ns/op (4.7x); 900-byte prefix 565 → 155 ns/op (3.6x).
---
parquet/internal/encoding/delta_byte_array.go | 16 +++-
.../encoding/delta_byte_array_benchmark_test.go | 8 ++
.../encoding/delta_byte_array_prefix_test.go | 94 ++++++++++++++++++++++
3 files changed, 117 insertions(+), 1 deletion(-)
diff --git a/parquet/internal/encoding/delta_byte_array.go
b/parquet/internal/encoding/delta_byte_array.go
index 3452b505..f73fe83a 100644
--- a/parquet/internal/encoding/delta_byte_array.go
+++ b/parquet/internal/encoding/delta_byte_array.go
@@ -17,8 +17,10 @@
package encoding
import (
+ "encoding/binary"
"errors"
"fmt"
+ "math/bits"
"slices"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -50,7 +52,19 @@ const deltaByteArrayBatchSize = 256
func commonPrefixLength(left, right parquet.ByteArray) int {
maximum := min(left.Len(), right.Len())
- for i := 0; i < maximum; i++ {
+ if maximum == 0 || left[0] != right[0] {
+ return 0
+ }
+
+ i := 0
+ for ; i <= maximum-8; i += 8 {
+ diff := binary.LittleEndian.Uint64(left[i:]) ^
binary.LittleEndian.Uint64(right[i:])
+ if diff != 0 {
+ // The first differing byte occupies the least
significant bits.
+ return i + bits.TrailingZeros64(diff)/8
+ }
+ }
+ for ; i < maximum; i++ {
if left[i] != right[i] {
return i
}
diff --git a/parquet/internal/encoding/delta_byte_array_benchmark_test.go
b/parquet/internal/encoding/delta_byte_array_benchmark_test.go
index 37c677b6..ff30054b 100644
--- a/parquet/internal/encoding/delta_byte_array_benchmark_test.go
+++ b/parquet/internal/encoding/delta_byte_array_benchmark_test.go
@@ -18,6 +18,7 @@ package encoding
import (
"fmt"
+ "strings"
"testing"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -25,6 +26,7 @@ import (
)
func BenchmarkDeltaByteArrayEncoding(b *testing.B) {
+ longPrefix := strings.Repeat("warehouse/region/partition/", 12)
for _, test := range []struct {
name string
value func(int) []byte
@@ -35,6 +37,12 @@ func BenchmarkDeltaByteArrayEncoding(b *testing.B) {
return []byte(fmt.Sprintf("partition/%06d", i))
},
},
+ {
+ name: "long-prefix",
+ value: func(i int) []byte {
+ return []byte(fmt.Sprintf("%s%06d", longPrefix,
i))
+ },
+ },
{
name: "low-prefix",
value: func(i int) []byte {
diff --git a/parquet/internal/encoding/delta_byte_array_prefix_test.go
b/parquet/internal/encoding/delta_byte_array_prefix_test.go
new file mode 100644
index 00000000..6bfa98c8
--- /dev/null
+++ b/parquet/internal/encoding/delta_byte_array_prefix_test.go
@@ -0,0 +1,94 @@
+// 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 (
+ "testing"
+
+ "github.com/apache/arrow-go/v18/parquet"
+)
+
+func TestCommonPrefixLength(t *testing.T) {
+ if got := commonPrefixLength(nil, nil); got != 0 {
+ t.Fatalf("nil: got %d, want 0", got)
+ }
+ if got := commonPrefixLength(nil, parquet.ByteArray("x")); got != 0 {
+ t.Fatalf("nil versus value: got %d, want 0", got)
+ }
+
+ for length := 0; length <= 257; length++ {
+ // Shift the starting addresses to cover unaligned slices.
+ left := make([]byte, length+1)
+ right := make([]byte, length+2)
+ for i := 0; i < length; i++ {
+ left[i+1] = byte(i*17 + 0x81)
+ right[i+2] = left[i+1]
+ }
+ a, b := parquet.ByteArray(left[1:]),
parquet.ByteArray(right[2:])
+ for mismatch := 0; mismatch <= length; mismatch++ {
+ if mismatch < length {
+ b[mismatch] ^= 0x80
+ }
+ if got := commonPrefixLength(a, b); got != mismatch {
+ t.Fatalf("length=%d mismatch=%d: got %d",
length, mismatch, got)
+ }
+ if mismatch < length {
+ b[mismatch] ^= 0x80
+ }
+ }
+ if length > 0 {
+ if got := commonPrefixLength(a, b[:length-1]); got !=
length-1 {
+ t.Fatalf("right shorter, length=%d: got %d",
length, got)
+ }
+ if got := commonPrefixLength(a[:length-1], b); got !=
length-1 {
+ t.Fatalf("left shorter, length=%d: got %d",
length, got)
+ }
+ }
+ }
+}
+
+func BenchmarkCommonPrefixLength(b *testing.B) {
+ for _, tc := range []struct {
+ name string
+ length, prefix int
+ }{
+ {"different-first", 256, 0},
+ {"prefix-3", 8, 3},
+ {"prefix-7", 16, 7},
+ {"prefix-32", 64, 32},
+ {"prefix-128", 256, 128},
+ {"prefix-255", 256, 255},
+ {"equal-256", 256, 256},
+ {"prefix-900", 1024, 900},
+ } {
+ b.Run(tc.name, func(b *testing.B) {
+ left := make(parquet.ByteArray, tc.length)
+ right := make(parquet.ByteArray, tc.length)
+ for i := range left {
+ left[i] = byte(i*17 + 0x81)
+ right[i] = left[i]
+ }
+ if tc.prefix < tc.length {
+ right[tc.prefix] ^= 0x80
+ }
+ b.ReportAllocs()
+ for b.Loop() {
+ commonPrefixLength(left, right)
+ }
+ })
+ }
+}