You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by ze...@apache.org on 2026/08/27 22:05:18 UTC

(iceberg-go) branch main updated: perf(table): avoid heap scratch in canonical partition keys (#1921)

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/iceberg-go.git


The following commit(s) were added to refs/heads/main by this push:
     new f1f16dbf4 perf(table): avoid heap scratch in canonical partition keys (#1921)
f1f16dbf4 is described below

commit f1f16dbf494cd07c7d6b50aed68830c79a705df2
Author: Minh Vu <vu...@gmail.com>
AuthorDate: Fri Aug 28 00:05:13 2026 +0200

    perf(table): avoid heap scratch in canonical partition keys (#1921)
---
 table/conflict_validation.go            |  9 +++-
 table/conflict_validation_bench_test.go | 82 +++++++++++++++++++++++++++++++++
 table/conflict_validation_test.go       | 16 +++++++
 3 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/table/conflict_validation.go b/table/conflict_validation.go
index f7c46e8ba..8bc57bda9 100644
--- a/table/conflict_validation.go
+++ b/table/conflict_validation.go
@@ -754,13 +754,18 @@ var filePathFieldID = func() int {
 // Java's DeleteFileIndex keys its PartitionMap by specId, and field IDs
 // are only meaningful within a single spec.
 func canonicalPartitionKey(specID int32, partition map[int]any) (string, error) {
-	ids := make([]int, 0, len(partition))
+	var idStorage [8]int
+	ids := idStorage[:0]
+	if len(partition) > len(idStorage) {
+		ids = make([]int, 0, len(partition))
+	}
 	for id := range partition {
 		ids = append(ids, id)
 	}
 	slices.Sort(ids)
 
-	buf := strconv.AppendInt(nil, int64(specID), 10)
+	var bufStorage [256]byte
+	buf := strconv.AppendInt(bufStorage[:0], int64(specID), 10)
 	buf = append(buf, '|')
 	for _, id := range ids {
 		buf = strconv.AppendInt(buf, int64(id), 10)
diff --git a/table/conflict_validation_bench_test.go b/table/conflict_validation_bench_test.go
new file mode 100644
index 000000000..f3f0bbab7
--- /dev/null
+++ b/table/conflict_validation_bench_test.go
@@ -0,0 +1,82 @@
+// 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 table
+
+import (
+	"testing"
+)
+
+var canonicalPartitionKeyBenchmarkSink string
+
+func BenchmarkCanonicalPartitionKey(b *testing.B) {
+	for _, tc := range []struct {
+		name      string
+		partition map[int]any
+	}{
+		{
+			name:      "fields=1/int32",
+			partition: map[int]any{1: int32(1)},
+		},
+		{
+			name: "fields=2/int32",
+			partition: map[int]any{
+				1: int32(1), 2: int32(2),
+			},
+		},
+		{
+			name: "fields=4/int32",
+			partition: map[int]any{
+				1: int32(1), 2: int32(2), 3: int32(3), 4: int32(4),
+			},
+		},
+		{
+			name: "fields=8/mixed",
+			partition: map[int]any{
+				1: int32(1), 2: "two", 3: int64(3), 4: "four",
+				5: int32(5), 6: "six", 7: int64(7), 8: "eight",
+			},
+		},
+		{
+			name: "fields=4/binary-string",
+			partition: map[int]any{
+				1: []byte("one"), 2: "two", 3: []byte("three"), 4: "four",
+			},
+		},
+		{
+			name: "fields=16/mixed",
+			partition: map[int]any{
+				1: int32(1), 2: "two", 3: int64(3), 4: "four",
+				5: int32(5), 6: "six", 7: int64(7), 8: "eight",
+				9: int32(9), 10: "ten", 11: int64(11), 12: "twelve",
+				13: int32(13), 14: "fourteen", 15: int64(15), 16: "sixteen",
+			},
+		},
+	} {
+		b.Run(tc.name, func(b *testing.B) {
+			b.ReportAllocs()
+			b.ResetTimer()
+			for b.Loop() {
+				key, err := canonicalPartitionKey(7, tc.partition)
+				if err != nil {
+					b.Fatal(err)
+				}
+				canonicalPartitionKeyBenchmarkSink = key
+			}
+		})
+	}
+}
diff --git a/table/conflict_validation_test.go b/table/conflict_validation_test.go
index ae064485d..1909142a6 100644
--- a/table/conflict_validation_test.go
+++ b/table/conflict_validation_test.go
@@ -585,6 +585,22 @@ func TestCanonicalPartitionKey(t *testing.T) {
 			assert.Contains(t, err.Error(), "unsupported partition value type")
 		}
 	})
+
+	t.Run("wide partition uses fallback storage", func(t *testing.T) {
+		first := make(map[int]any, 16)
+		second := make(map[int]any, 16)
+		for id := 1; id <= 16; id++ {
+			value := fmt.Sprintf("field-%d-012345678901234567890123456789", id)
+			first[id] = value
+		}
+		for id := 16; id >= 1; id-- {
+			second[id] = first[id]
+		}
+
+		assert.Equal(t,
+			mustCanonicalPartitionKey(t, 0, first),
+			mustCanonicalPartitionKey(t, 0, second))
+	})
 }
 
 // TestValidateNoNewDeletesForRewrittenFiles_PosDeleteMatching drives the