From 71ccd6d7bd05c6095ace606934dea746c04ac429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= <42793+vmg@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:46:15 +0100 Subject: [PATCH] sizegen: do not ignore type aliases (#17556) Signed-off-by: Vicent Marti --- go/mysql/collations/colldata/cached_size.go | 8 ++++++++ go/sqltypes/cached_size.go | 8 ++++++++ go/tools/sizegen/sizegen.go | 8 ++++++-- go/vt/proto/query/cached_size.go | 20 ++++++++++++++++++++ go/vt/proto/topodata/cached_size.go | 8 ++++++++ go/vt/proto/vttime/cached_size.go | 6 ++++++ 6 files changed, 56 insertions(+), 2 deletions(-) diff --git a/go/mysql/collations/colldata/cached_size.go b/go/mysql/collations/colldata/cached_size.go index b348baaaed8..190e1731651 100644 --- a/go/mysql/collations/colldata/cached_size.go +++ b/go/mysql/collations/colldata/cached_size.go @@ -19,6 +19,10 @@ package colldata import hack "vitess.io/vitess/go/hack" +type cachedObject interface { + CachedSize(alloc bool) int64 +} + func (cached *eightbitWildcard) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -59,6 +63,10 @@ func (cached *unicodeWildcard) CachedSize(alloc bool) int64 { if alloc { size += int64(48) } + // field charset vitess.io/vitess/go/mysql/collations/charset.Charset + if cc, ok := cached.charset.(cachedObject); ok { + size += cc.CachedSize(true) + } // field pattern []rune { size += hack.RuntimeAllocSize(int64(cap(cached.pattern)) * int64(4)) diff --git a/go/sqltypes/cached_size.go b/go/sqltypes/cached_size.go index 632c8249455..53bc407278d 100644 --- a/go/sqltypes/cached_size.go +++ b/go/sqltypes/cached_size.go @@ -37,6 +37,14 @@ func (cached *Result) CachedSize(alloc bool) int64 { // field Rows []vitess.io/vitess/go/sqltypes.Row { size += hack.RuntimeAllocSize(int64(cap(cached.Rows)) * int64(24)) + for _, elem := range cached.Rows { + { + size += hack.RuntimeAllocSize(int64(cap(elem)) * int64(32)) + for _, elem := range elem { + size += elem.CachedSize(false) + } + } + } } // field SessionStateChanges string size += hack.RuntimeAllocSize(int64(len(cached.SessionStateChanges))) diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go index 7ecd50e3d8c..17b155ad3f4 100644 --- a/go/tools/sizegen/sizegen.go +++ b/go/tools/sizegen/sizegen.go @@ -163,6 +163,8 @@ func (sizegen *sizegen) generateTyp(tt types.Type) { sizegen.generateKnownType(tt) case *types.Alias: sizegen.generateTyp(types.Unalias(tt)) + default: + panic(fmt.Sprintf("unhandled type: %v (%T)", tt, tt)) } } @@ -490,9 +492,11 @@ func (sizegen *sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty // assume that function pointers do not allocate (although they might, if they're closures) return nil, 0 + case *types.Alias: + return sizegen.sizeStmtForType(fieldName, types.Unalias(node), alloc) + default: - log.Printf("unhandled type: %T", node) - return nil, 0 + panic(fmt.Sprintf("unhandled type: %v (%T)", node, node)) } } diff --git a/go/vt/proto/query/cached_size.go b/go/vt/proto/query/cached_size.go index 5b613317294..4436594681a 100644 --- a/go/vt/proto/query/cached_size.go +++ b/go/vt/proto/query/cached_size.go @@ -27,6 +27,10 @@ func (cached *BindVariable) CachedSize(alloc bool) int64 { if alloc { size += int64(96) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Value []byte { size += hack.RuntimeAllocSize(int64(cap(cached.Value))) @@ -48,6 +52,10 @@ func (cached *Field) CachedSize(alloc bool) int64 { if alloc { size += int64(160) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Name string size += hack.RuntimeAllocSize(int64(len(cached.Name))) // field Table string @@ -70,6 +78,10 @@ func (cached *QueryWarning) CachedSize(alloc bool) int64 { if alloc { size += int64(64) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Message string size += hack.RuntimeAllocSize(int64(len(cached.Message))) return size @@ -82,6 +94,10 @@ func (cached *Target) CachedSize(alloc bool) int64 { if alloc { size += int64(96) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Keyspace string size += hack.RuntimeAllocSize(int64(len(cached.Keyspace))) // field Shard string @@ -98,6 +114,10 @@ func (cached *Value) CachedSize(alloc bool) int64 { if alloc { size += int64(80) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Value []byte { size += hack.RuntimeAllocSize(int64(cap(cached.Value))) diff --git a/go/vt/proto/topodata/cached_size.go b/go/vt/proto/topodata/cached_size.go index 94b7fc6818c..3feead01bae 100644 --- a/go/vt/proto/topodata/cached_size.go +++ b/go/vt/proto/topodata/cached_size.go @@ -27,6 +27,10 @@ func (cached *KeyRange) CachedSize(alloc bool) int64 { if alloc { size += int64(96) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Start []byte { size += hack.RuntimeAllocSize(int64(cap(cached.Start))) @@ -45,6 +49,10 @@ func (cached *ThrottledAppRule) CachedSize(alloc bool) int64 { if alloc { size += int64(80) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } // field Name string size += hack.RuntimeAllocSize(int64(len(cached.Name))) // field ExpiresAt *vitess.io/vitess/go/vt/proto/vttime.Time diff --git a/go/vt/proto/vttime/cached_size.go b/go/vt/proto/vttime/cached_size.go index 62a6366ba3c..f2b69dbefae 100644 --- a/go/vt/proto/vttime/cached_size.go +++ b/go/vt/proto/vttime/cached_size.go @@ -17,6 +17,8 @@ limitations under the License. package vttime +import hack "vitess.io/vitess/go/hack" + func (cached *Time) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -25,5 +27,9 @@ func (cached *Time) CachedSize(alloc bool) int64 { if alloc { size += int64(64) } + // field unknownFields google.golang.org/protobuf/runtime/protoimpl.UnknownFields + { + size += hack.RuntimeAllocSize(int64(cap(cached.unknownFields))) + } return size }