This is an automated email from the ASF dual-hosted git repository.

laskoviymishka 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 503c6d0da perf(table): skip redundant metadata JSON scan (#2641)
503c6d0da is described below

commit 503c6d0daa1d0b7a44c52ee229e9bf58f4b82d5a
Author: Vu <[email protected]>
AuthorDate: Thu Nov 19 14:18:20 2026 +0100

    perf(table): skip redundant metadata JSON scan (#2641)
---
 table/metadata.go                      | 10 +++++++--
 table/metadata_preflight_bench_test.go | 27 ++++++++++++++---------
 table/metadata_preflight_test.go       | 39 ++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 12 deletions(-)

diff --git a/table/metadata.go b/table/metadata.go
index 7cd09167c..230142cfb 100644
--- a/table/metadata.go
+++ b/table/metadata.go
@@ -2181,7 +2181,10 @@ func ParseMetadataBytes(b []byte) (Metadata, error) {
                return nil, err
        }
 
-       var ret Metadata
+       var ret interface {
+               Metadata
+               json.Unmarshaler
+       }
        switch formatVersion {
        case 1:
                ret = &metadataV1{}
@@ -2198,7 +2201,10 @@ func ParseMetadataBytes(b []byte) (Metadata, error) {
                return nil, err
        }
 
-       if err := json.Unmarshal(normalized, ret); err != nil {
+       // Preflight already checked JSON syntax. Call the decoder directly to 
avoid
+       // scanning the entire document again before entering UnmarshalJSON.
+       start, end := trimJSONSpan(normalized, 0, len(normalized))
+       if err := ret.UnmarshalJSON(normalized[start:end]); err != nil {
                if errors.Is(err, ErrInvalidMetadata) {
                        return nil, err
                }
diff --git a/table/metadata_preflight_bench_test.go 
b/table/metadata_preflight_bench_test.go
index d064237be..a0028b692 100644
--- a/table/metadata_preflight_bench_test.go
+++ b/table/metadata_preflight_bench_test.go
@@ -20,6 +20,7 @@ package table
 import (
        "encoding/json"
        "fmt"
+       "strconv"
        "testing"
 )
 
@@ -27,18 +28,20 @@ var metadataParseBenchmarkSink int
 
 func BenchmarkParseMetadataBytes(b *testing.B) {
        for _, tc := range []struct {
-               name               string
-               schemaCount        int
-               partitionSpecCount int
-               snapshotCount      int
-               propertyCount      int
+               name                 string
+               schemaCount          int
+               partitionSpecCount   int
+               snapshotCount        int
+               propertyCount        int
+               summaryPropertyCount int
        }{
                {name: "schemas=1/specs=1/snapshots=10", schemaCount: 1, 
partitionSpecCount: 1, snapshotCount: 10},
+               {name: "schemas=1/specs=1/snapshots=64", schemaCount: 1, 
partitionSpecCount: 1, snapshotCount: 64, summaryPropertyCount: 14},
                {name: "schemas=32/specs=32/snapshots=1000", schemaCount: 32, 
partitionSpecCount: 32, snapshotCount: 1_000, propertyCount: 16},
                {name: "schemas=256/specs=256/snapshots=10000", schemaCount: 
256, partitionSpecCount: 256, snapshotCount: 10_000, propertyCount: 64},
        } {
                b.Run(tc.name, func(b *testing.B) {
-                       data := benchmarkMetadataJSON(tc.schemaCount, 
tc.partitionSpecCount, tc.snapshotCount, tc.propertyCount)
+                       data := benchmarkMetadataJSON(tc.schemaCount, 
tc.partitionSpecCount, tc.snapshotCount, tc.propertyCount, 
tc.summaryPropertyCount)
                        if _, err := ParseMetadataBytes(data); err != nil {
                                b.Fatal(err)
                        }
@@ -58,7 +61,7 @@ func BenchmarkParseMetadataBytes(b *testing.B) {
        }
 }
 
-func benchmarkMetadataJSON(schemaCount, partitionSpecCount, snapshotCount, 
propertyCount int) []byte {
+func benchmarkMetadataJSON(schemaCount, partitionSpecCount, snapshotCount, 
propertyCount, summaryPropertyCount int) []byte {
        const minuteInMS = int64(60_000)
 
        metadata := map[string]any{
@@ -76,7 +79,7 @@ func benchmarkMetadataJSON(schemaCount, partitionSpecCount, 
snapshotCount, prope
                "schemas":               benchmarkSchemas(schemaCount),
                "partition-specs":       
benchmarkPartitionSpecs(partitionSpecCount),
                "properties":            
benchmarkMetadataProperties(propertyCount),
-               "snapshots":             
benchmarkParseMetadataSnapshots(snapshotCount, minuteInMS),
+               "snapshots":             
benchmarkParseMetadataSnapshots(snapshotCount, minuteInMS, 
summaryPropertyCount),
                "snapshot-log":          
benchmarkParseMetadataSnapshotLogEntries(snapshotCount, minuteInMS),
                "metadata-log":          []any{map[string]any{"metadata-file": 
"s3://bucket/metadata/v1.json", "timestamp-ms": int64(0)}},
                "current-snapshot-id":   int64(snapshotCount),
@@ -135,15 +138,19 @@ func benchmarkMetadataProperties(count int) 
map[string]string {
        return properties
 }
 
-func benchmarkParseMetadataSnapshots(count int, minuteInMS int64) []any {
+func benchmarkParseMetadataSnapshots(count int, minuteInMS int64, 
summaryPropertyCount int) []any {
        snapshots := make([]any, count)
        for i := range snapshots {
                id := int64(i + 1)
+               summary := map[string]string{"operation": "append"}
+               for property := range summaryPropertyCount {
+                       summary[fmt.Sprintf("metric-%d", property)] = 
strconv.Itoa(i + property)
+               }
                snapshot := map[string]any{
                        "snapshot-id":     id,
                        "timestamp-ms":    id * minuteInMS,
                        "sequence-number": i + 1,
-                       "summary":         map[string]any{"operation": 
"append"},
+                       "summary":         summary,
                        "manifest-list":   
fmt.Sprintf("s3://bucket/manifests/%d.avro", id),
                }
                if i > 0 {
diff --git a/table/metadata_preflight_test.go b/table/metadata_preflight_test.go
index 22a2cff12..5b15ffd67 100644
--- a/table/metadata_preflight_test.go
+++ b/table/metadata_preflight_test.go
@@ -19,6 +19,7 @@ package table
 
 import (
        "encoding/json"
+       "fmt"
        "strings"
        "testing"
 
@@ -147,3 +148,41 @@ func 
TestParseMetadataBytesRejectsCaseFoldedFormatVersionCollision(t *testing.T)
        _, err := ParseMetadataBytes([]byte(data))
        require.ErrorIs(t, err, ErrInvalidMetadataFormatVersion)
 }
+
+func TestParseMetadataBytesPreservesTypeErrorOffset(t *testing.T) {
+       for _, version := range []struct {
+               number int
+               target json.Unmarshaler
+       }{
+               {number: 1, target: &metadataV1{}},
+               {number: 2, target: &metadataV2{}},
+               {number: 3, target: &metadataV3{}},
+       } {
+               t.Run(fmt.Sprintf("v%d", version.number), func(t *testing.T) {
+                       for _, spec := range []struct {
+                               name   string
+                               fields string
+                       }{
+                               {name: "whitespace"},
+                               {name: "normalized", fields: 
`,"partition-specs":[{"spec-id":0,"fields":[{"source-id":1,"name":"id","transform":"identity"}]}]`},
+                       } {
+                               t.Run(spec.name, func(t *testing.T) {
+                                       input := fmt.Appendf(nil, " 
\t\r\n"+`{"format-version":%d,"last-updated-ms":1,"last-column-id":"not-an-int"%s}`+"\n",
+                                               version.number, spec.fields)
+                                       normalized, err := 
assignMissingPartitionFieldIDs(input)
+                                       require.NoError(t, err)
+                                       if spec.fields != "" {
+                                               require.Contains(t, 
string(normalized), `"field-id":1000`)
+                                       }
+
+                                       var expected, actual 
*json.UnmarshalTypeError
+                                       require.ErrorAs(t, 
json.Unmarshal(normalized, version.target), &expected)
+                                       _, err = ParseMetadataBytes(input)
+                                       require.ErrorIs(t, err, 
ErrInvalidMetadata)
+                                       require.ErrorAs(t, err, &actual)
+                                       require.Equal(t, expected, actual)
+                               })
+                       }
+               })
+       }
+}

Reply via email to