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 54a0ae42 perf(hashing): compare short binary memo keys by packed value
(#1660)
54a0ae42 is described below
commit 54a0ae42990bee658b5ef0afe4668a08162fec6e
Author: kurtoskalac <[email protected]>
AuthorDate: Mon Nov 23 19:04:43 2026 +0100
perf(hashing): compare short binary memo keys by packed value (#1660)
### Why
Binary dictionary lookups reread stored bytes when a hash matches.
### What
Compare keys up to seven bytes by packed value.
### Implementation
Pack the bytes and length into a tagged key. Longer keys use xxHash and
byte comparison.
---------
Co-authored-by: kurtoskalac <[email protected]>
---
.../hashing/short_binary_keys_benchmark_test.go | 54 +++++++++++++++
internal/hashing/xxh3_memo_table.go | 79 ++++++++++------------
internal/hashing/xxh3_memo_table_test.go | 16 +++++
3 files changed, 106 insertions(+), 43 deletions(-)
diff --git a/internal/hashing/short_binary_keys_benchmark_test.go
b/internal/hashing/short_binary_keys_benchmark_test.go
new file mode 100644
index 00000000..ee1765a1
--- /dev/null
+++ b/internal/hashing/short_binary_keys_benchmark_test.go
@@ -0,0 +1,54 @@
+// 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 hashing_test
+
+import (
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "github.com/apache/arrow-go/v18/internal/hashing"
+)
+
+func BenchmarkBinaryMemoTableShortKeys(b *testing.B) {
+ const batchSize = 1 << 16
+ const distinct = 256
+ keys := make([][4]byte, distinct)
+ table := hashing.NewBinaryMemoTable(distinct, distinct*4,
+ array.NewBinaryBuilder(memory.DefaultAllocator,
arrow.BinaryTypes.Binary))
+ defer table.Release()
+ for i := range keys {
+ keys[i] = [4]byte{'k', '0' + byte(i/100), '0' + byte(i/10%10),
'0' + byte(i%10)}
+ if _, _, err := table.InsertOrGet(keys[i][:]); err != nil {
+ b.Fatal(err)
+ }
+ }
+ indices := make([]byte, batchSize)
+ state := uint32(1)
+ for i := range indices {
+ state = state*1664525 + 1013904223
+ indices[i] = byte(state >> 24)
+ }
+ b.ReportAllocs()
+ b.SetBytes(batchSize * 4)
+ for b.Loop() {
+ for _, index := range indices {
+ _, _, _ = table.InsertOrGet(keys[index][:])
+ }
+ }
+}
diff --git a/internal/hashing/xxh3_memo_table.go
b/internal/hashing/xxh3_memo_table.go
index c30575e6..d9c1c30a 100644
--- a/internal/hashing/xxh3_memo_table.go
+++ b/internal/hashing/xxh3_memo_table.go
@@ -21,6 +21,7 @@ package hashing
import (
"bytes"
+ "encoding/binary"
"unsafe"
)
@@ -231,61 +232,62 @@ func (BinaryMemoTable) valAsByteSlice(val interface{})
[]byte {
}
}
-// helper function to get the hash value regardless of the underlying binary
type
-func (BinaryMemoTable) getHash(val interface{}) uint64 {
- switch v := val.(type) {
- case string:
- return hashString(v, 0)
- case []byte:
- return Hash(v, 0)
- case ByteSlice:
- return Hash(v.Bytes(), 0)
- default:
- panic("invalid type for binarymemotable")
+const packedBinaryKey = uint64(1) << 63
+
+func binaryMemoHash(value []byte) uint64 {
+ if len(value) > 7 {
+ return Hash(value, 0) &^ packedBinaryKey
+ }
+ // Up to seven bytes and their length fit in 63 bits. The odd
+ // multiplication and xorshift are bijective in that space.
+ var key uint64
+ if len(value) == 4 {
+ key = uint64(binary.LittleEndian.Uint32(value)) | (4 << 56)
+ } else {
+ var packed [8]byte
+ copy(packed[:], value)
+ packed[7] = byte(len(value))
+ key = binary.LittleEndian.Uint64(packed[:])
}
+ key = (key * 0x9e3779b97f4a7c15) &^ packedBinaryKey
+ return (key ^ (key >> 31)) | packedBinaryKey
}
func (b *BinaryMemoTable) lookup(h uint64, val []byte) (*entry[int32], bool) {
- return b.tbl.Lookup(h, func(i int32) bool {
- return bytes.Equal(val, b.builder.Value(int(i)))
- })
-}
-
-func (b *BinaryMemoTable) Exists(val []byte) bool {
- _, ok := b.lookup(b.getHash(val), val)
- return ok
-}
-
-// ExistsDirect checks if val exists in the table by inlining the hash
-// table probe loop directly. This avoids the closure allocation that
-// occurs in the normal Exists -> lookup -> HashTable.Lookup path,
-// where the comparison closure captures val and causes it to escape
-// to the heap.
-func (b *BinaryMemoTable) ExistsDirect(val []byte) bool {
const perturbShift uint8 = 5
- h := Hash(val, 0)
v := b.tbl.fixHash(h)
idx := v & b.tbl.capMask
perturb := (v >> uint64(perturbShift)) + 1
for {
e := &b.tbl.entries[idx]
- if e.h == v && bytes.Equal(val,
b.builder.Value(int(e.payload.val))) {
- return true
+ if e.h == v && (v&packedBinaryKey != 0 || bytes.Equal(val,
b.builder.Value(int(e.payload.val)))) {
+ return e, true
}
if e.h == sentinel {
- return false
+ return e, false
}
idx = (idx + perturb) & b.tbl.capMask
perturb = (perturb >> uint64(perturbShift)) + 1
}
}
+func (b *BinaryMemoTable) Exists(val []byte) bool {
+ _, ok := b.lookup(binaryMemoHash(val), val)
+ return ok
+}
+
+// ExistsDirect checks whether val is present without inserting it.
+func (b *BinaryMemoTable) ExistsDirect(val []byte) bool {
+ return b.Exists(val)
+}
+
// Get returns the index of the specified value in the table or KeyNotFound,
// and a boolean indicating whether it was found in the table.
func (b *BinaryMemoTable) Get(val interface{}) (int, bool) {
- if p, ok := b.lookup(b.getHash(val), b.valAsByteSlice(val)); ok {
+ value := b.valAsByteSlice(val)
+ if p, ok := b.lookup(binaryMemoHash(value), value); ok {
return int(p.payload.val), ok
}
return KeyNotFound, false
@@ -295,16 +297,7 @@ func (b *BinaryMemoTable) Get(val interface{}) (int, bool)
{
// it is inserted into the table. The return value 'found' indicates whether
the value
// was found in the table (true) or inserted (false) along with any possible
error.
func (b *BinaryMemoTable) GetOrInsertBytes(val []byte) (idx int, found bool,
err error) {
- h := Hash(val, 0)
- p, found := b.lookup(h, val)
- if found {
- idx = int(p.payload.val)
- } else {
- idx = b.Size()
- b.builder.Append(val)
- b.tbl.Insert(p, h, int32(idx), -1)
- }
- return
+ return b.InsertOrGet(val)
}
func (b *BinaryMemoTable) GetOrInsert(val interface{}) (idx int, found bool,
err error) {
@@ -315,7 +308,7 @@ func (b *BinaryMemoTable) GetOrInsert(val interface{}) (idx
int, found bool, err
// it is inserted into the table. The return value 'found' indicates whether
the value
// was found in the table (true) or inserted (false) along with any possible
error.
func (b *BinaryMemoTable) InsertOrGet(val []byte) (idx int, found bool, err
error) {
- h := Hash(val, 0)
+ h := binaryMemoHash(val)
p, found := b.lookup(h, val)
if found {
idx = int(p.payload.val)
diff --git a/internal/hashing/xxh3_memo_table_test.go
b/internal/hashing/xxh3_memo_table_test.go
index d248fdc5..9c05134b 100644
--- a/internal/hashing/xxh3_memo_table_test.go
+++ b/internal/hashing/xxh3_memo_table_test.go
@@ -89,6 +89,22 @@ func assertGetMemoValue(t *testing.T, table
*hashing.Table[int32], value int32,
assert.Equal(t, want, got)
}
+func TestBinaryMemoTableShortKeys(t *testing.T) {
+ table := hashing.NewBinaryMemoTable(0, 0,
array.NewBinaryBuilder(memory.DefaultAllocator, arrow.BinaryTypes.Binary))
+ defer table.Release()
+ values := []string{"", "\x00", "a", "a\x00", "\x00a", "abcdefg",
"abcdefg\x00", "\x00\x00\x00\x00\x00\x00\xff"}
+ for i, val := range values {
+ insertBinaryMemoValue(t, table, val, i)
+ }
+ for i, val := range values {
+ assertGetBinaryMemoValue(t, table, val, i)
+ assert.True(t, table.ExistsDirect([]byte(val)))
+ }
+ table.Truncate(2)
+ assert.True(t, table.Exists(nil))
+ assert.False(t, table.Exists([]byte("a")))
+}
+
func TestBinaryMemoTableTruncate(t *testing.T) {
t.Run("reinsert discarded values", func(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())