From 3b793b1397d9f06eff91b2b12c23ccc4325d637a Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Mon, 25 Jan 2021 17:49:39 +0100 Subject: [PATCH 1/6] vt: implement 'sizegen' helper Signed-off-by: Vicent Marti --- go.mod | 2 +- go.sum | 6 +- go/tools/sizegen/sizegen.go | 473 ++++++++++++++++++++++++++++++++++++ 3 files changed, 476 insertions(+), 5 deletions(-) create mode 100644 go/tools/sizegen/sizegen.go diff --git a/go.mod b/go.mod index f074e7dbfc2..fa07aafe241 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect github.com/corpix/uarand v0.1.1 // indirect github.com/cyberdelia/go-metrics-graphite v0.0.0-20161219230853-39f87cc3b432 + github.com/dave/jennifer v1.4.1 github.com/evanphx/json-patch v4.5.0+incompatible github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab github.com/go-sql-driver/mysql v1.5.0 @@ -55,7 +56,6 @@ require ( github.com/klauspost/pgzip v1.2.4 github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/krishicks/yaml-patch v0.0.10 - github.com/looplab/fsm v0.2.0 github.com/magiconair/properties v1.8.1 github.com/martini-contrib/auth v0.0.0-20150219114609-fa62c19b7ae8 github.com/martini-contrib/gzip v0.0.0-20151124214156-6c035326b43f diff --git a/go.sum b/go.sum index efcf79e5849..41cc2b77507 100644 --- a/go.sum +++ b/go.sum @@ -154,6 +154,8 @@ github.com/cyberdelia/go-metrics-graphite v0.0.0-20161219230853-39f87cc3b432 h1: github.com/cyberdelia/go-metrics-graphite v0.0.0-20161219230853-39f87cc3b432/go.mod h1:xwIwAxMvYnVrGJPe2FKx5prTrnAjGOD8zvDOnxnrrkM= github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY= github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= +github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw= +github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA= github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -433,8 +435,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/krishicks/yaml-patch v0.0.10 h1:H4FcHpnNwVmw8u0MjPRjWyIXtco6zM2F78t+57oNM3E= github.com/krishicks/yaml-patch v0.0.10/go.mod h1:Sm5TchwZS6sm7RJoyg87tzxm2ZcKzdRE4Q7TjNhPrME= -github.com/looplab/fsm v0.2.0 h1:M8hf5EF4AYLcT1FNKVUX8nu7D0xfp291iGeuigSxfrw= -github.com/looplab/fsm v0.2.0/go.mod h1:p+IElwgCnAByqr2DWMuNbPjgMwqcHvTRZZn3dvKEke0= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= @@ -474,8 +474,6 @@ github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/ github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/mch1307/vaultlib v0.5.0 h1:+tI8YCG033aVI+kAKwo0fwrUylFs+wO6DB7DM5qXJzU= -github.com/mch1307/vaultlib v0.5.0/go.mod h1:phFbO1oIDL1xTqUrNXbrAG0VdcYEKP8TNa9FJd7hFic= github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.26 h1:gPxPSwALAeHJSjarOs00QjVdV9QoBvc1D2ujQUr5BzU= diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go new file mode 100644 index 00000000000..3e2c3b39844 --- /dev/null +++ b/go/tools/sizegen/sizegen.go @@ -0,0 +1,473 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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 main + +import ( + "flag" + "fmt" + "go/types" + "log" + "path" + "sort" + "strings" + + "github.com/dave/jennifer/jen" + "golang.org/x/tools/go/packages" +) + +const FileHeader = `Copyright 2021 The Vitess Authors. + +Licensed 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.` + +type Sizegen struct { + DebugTypes bool + mod *packages.Module + sizes types.Sizes + codegen map[string]*File + known map[*types.Named]*TypeState +} + +type Impl struct { + name string + code jen.Code +} + +type File struct { + pkg string + impls []Impl +} + +type TypeState struct { + generated bool + local bool + pod bool +} + +func NewSizegen(mod *packages.Module, sizes types.Sizes) *Sizegen { + return &Sizegen{ + DebugTypes: true, + mod: mod, + sizes: sizes, + known: make(map[*types.Named]*TypeState), + codegen: make(map[string]*File), + } +} + +func isPod(tt types.Type) bool { + switch tt := tt.(type) { + case *types.Struct: + for i := 0; i < tt.NumFields(); i++ { + if !isPod(tt.Field(i).Type()) { + return false + } + } + return true + + case *types.Basic: + switch tt.Kind() { + case types.String, types.UnsafePointer: + return false + } + return true + + default: + return false + } +} + +func (sizegen *Sizegen) getKnownType(named *types.Named) *TypeState { + ts := sizegen.known[named] + if ts == nil { + local := strings.HasPrefix(named.Obj().Pkg().Path(), sizegen.mod.Path) + ts = &TypeState{ + local: local, + pod: isPod(named.Underlying()), + } + sizegen.known[named] = ts + } + return ts +} + +func (sizegen *Sizegen) generateType(pkg *types.Package, file *File, named *types.Named) { + ts := sizegen.getKnownType(named) + if ts.generated { + return + } + ts.generated = true + + switch tt := named.Underlying().(type) { + case *types.Struct: + if impl := sizegen.sizeImplForStruct(named.Obj(), tt); impl != nil { + file.impls = append(file.impls, Impl{ + code: impl, + name: named.String(), + }) + } + case *types.Interface: + findImplementations(pkg.Scope(), tt, func(tt types.Type) { + if _, isStruct := tt.Underlying().(*types.Struct); isStruct { + sizegen.generateType(pkg, file, tt.(*types.Named)) + } + }) + default: + // no-op + } +} + +func (sizegen *Sizegen) GenerateKnownType(named *types.Named) { + pkgInfo := named.Obj().Pkg() + file := sizegen.codegen[pkgInfo.Path()] + if file == nil { + file = &File{pkg: pkgInfo.Name()} + sizegen.codegen[pkgInfo.Path()] = file + } + + sizegen.generateType(pkgInfo, file, named) +} + +func findImplementations(scope *types.Scope, iff *types.Interface, impl func(types.Type)) { + for _, name := range scope.Names() { + obj := scope.Lookup(name) + baseType := obj.Type() + if types.Implements(baseType, iff) || types.Implements(types.NewPointer(baseType), iff) { + impl(baseType) + } + } +} + +func (sizegen *Sizegen) GenerateForInterface(pkg *types.Package, iff *types.Interface) { + findImplementations(pkg.Scope(), iff, func(tt types.Type) { + if named, ok := tt.(*types.Named); ok { + sizegen.GenerateKnownType(named) + } + }) +} + +func (sizegen *Sizegen) Finalize() { + var complete bool + + for !complete { + complete = true + for tt, ts := range sizegen.known { + if ts.local && !ts.pod && !ts.generated { + sizegen.GenerateKnownType(tt) + complete = false + } + } + } + + for pkg, file := range sizegen.codegen { + if len(file.impls) == 0 { + continue + } + if !strings.HasPrefix(pkg, sizegen.mod.Path) { + log.Printf("failed to generate code for foreign package '%s'", pkg) + log.Printf("DEBUG:\n%#v", file) + continue + } + + out := jen.NewFile(file.pkg) + out.HeaderComment(FileHeader) + out.HeaderComment("Code generated by Sizegen. DO NOT EDIT.") + out.Add(jen.Type().Id("cachedObject").InterfaceFunc(func(i *jen.Group) { + i.Id("CachedSize").Params(jen.Id("alloc").Id("bool")).Int64() + })) + + sort.Slice(file.impls, func(i, j int) bool { + return strings.Compare(file.impls[i].name, file.impls[j].name) < 0 + }) + + for _, impl := range file.impls { + out.Add(impl.code) + } + + fullPath := path.Join(sizegen.mod.Dir, strings.TrimPrefix(pkg, sizegen.mod.Path), "cached_size.go") + if err := out.Save(fullPath); err != nil { + log.Printf("failed to save '%s': %v", fullPath, err) + continue + } + log.Printf("saved %s at '%s'", pkg, fullPath) + } +} + +func (sizegen *Sizegen) sizeImplForStruct(name *types.TypeName, st *types.Struct) jen.Code { + if sizegen.sizes.Sizeof(st) == 0 { + return nil + } + + var stmt []jen.Code + for i := 0; i < st.NumFields(); i++ { + field := st.Field(i) + fieldType := field.Type() + fieldName := jen.Id("cached").Dot(field.Name()) + if s := sizegen.sizeStmtForType(fieldName, fieldType, false); s != nil { + if sizegen.DebugTypes { + stmt = append(stmt, jen.Commentf("%s", field.String())) + } + stmt = append(stmt, s) + } + } + + f := jen.Func() + f.Params(jen.Id("cached").Op("*").Id(name.Name())) + f.Id("CachedSize").Params(jen.Id("alloc").Id("bool")).Int64() + f.BlockFunc(func(b *jen.Group) { + b.Add(jen.If(jen.Id("cached").Op("==").Nil()).Block(jen.Return(jen.Lit(int64(0))))) + b.Add(jen.Id("size").Op(":=").Lit(int64(0))) + b.Add(jen.If(jen.Id("alloc")).Block( + jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(st)), + )) + for _, s := range stmt { + b.Add(s) + } + b.Add(jen.Return(jen.Id("size"))) + }) + return f +} + +func (sizegen *Sizegen) sizeStmtForMap(fieldName *jen.Statement, m *types.Map) []jen.Code { + const bucketCnt = 8 + const sizeofHmap = int64(6 * 8) + + /* + type bmap struct { + // tophash generally contains the top byte of the hash value + // for each key in this bucket. If tophash[0] < minTopHash, + // tophash[0] is a bucket evacuation state instead. + tophash [bucketCnt]uint8 + // Followed by bucketCnt keys and then bucketCnt elems. + // NOTE: packing all the keys together and then all the elems together makes the + // code a bit more complicated than alternating key/elem/key/elem/... but it allows + // us to eliminate padding which would be needed for, e.g., map[int64]int8. + // Followed by an overflow pointer. + } + */ + sizeOfBucket := int( + bucketCnt + // tophash + bucketCnt*sizegen.sizes.Sizeof(m.Key()) + + bucketCnt*sizegen.sizes.Sizeof(m.Elem()) + + 8, // overflow pointer + ) + + return []jen.Code{ + jen.Id("size").Op("+=").Lit(sizeofHmap), + + jen.Id("hmap").Op(":=").Qual("reflect", "ValueOf").Call(fieldName), + + jen.Id("numBuckets").Op(":=").Id("int").Call( + jen.Qual("math", "Pow").Call(jen.Lit(2), jen.Id("float64").Call( + jen.Parens(jen.Op("*").Parens(jen.Op("*").Id("uint8")).Call( + jen.Qual("unsafe", "Pointer").Call(jen.Id("hmap").Dot("Pointer").Call(). + Op("+").Id("uintptr").Call(jen.Lit(9)))))))), + + jen.Id("numOldBuckets").Op(":=").Parens(jen.Op("*").Parens(jen.Op("*").Id("uint16")).Call( + jen.Qual("unsafe", "Pointer").Call( + jen.Id("hmap").Dot("Pointer").Call().Op("+").Id("uintptr").Call(jen.Lit(10))))), + + jen.Id("size").Op("+=").Id("int64").Call(jen.Id("numOldBuckets").Op("*").Lit(sizeOfBucket)), + + jen.If(jen.Id("len").Call(fieldName).Op(">").Lit(0).Op("||").Id("numBuckets").Op(">").Lit(1)).Block( + jen.Id("size").Op("+=").Id("int64").Call( + jen.Id("numBuckets").Op("*").Lit(sizeOfBucket))), + } +} + +func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Type, alloc bool) jen.Code { + if sizegen.sizes.Sizeof(field) == 0 { + return nil + } + + switch node := field.(type) { + case *types.Slice: + elemT := node.Elem() + elemSize := sizegen.sizes.Sizeof(elemT) + + switch elemSize { + case 0: + return nil + + case 1: + return jen.Id("size").Op("+=").Int64().Call(jen.Cap(fieldName)) + + default: + return jen.BlockFunc(func(b *jen.Group) { + b.Add( + jen.Id("size"). + Op("+="). + Int64().Call(jen.Cap(fieldName)). + Op("*"). + Lit(sizegen.sizes.Sizeof(elemT))) + + if stmt := sizegen.sizeStmtForType(jen.Id("elem"), elemT, false); stmt != nil { + b.Add(jen.For(jen.List(jen.Id("_"), jen.Id("elem")).Op(":=").Range().Add(fieldName))).Block(stmt) + } + }) + } + + case *types.Map: + return jen.If(fieldName.Clone().Op("!=").Nil()).BlockFunc(func(b *jen.Group) { + for _, stmt := range sizegen.sizeStmtForMap(fieldName, node) { + b.Add(stmt) + } + + var forLoopVars []jen.Code + keySize := sizegen.sizeStmtForType(jen.Id("k"), node.Key(), false) + valSize := sizegen.sizeStmtForType(jen.Id("v"), node.Elem(), false) + + switch { + case keySize != nil && valSize != nil: + forLoopVars = []jen.Code{jen.Id("k"), jen.Id("v")} + case keySize == nil && valSize != nil: + forLoopVars = []jen.Code{jen.Id("_"), jen.Id("v")} + case keySize != nil && valSize == nil: + forLoopVars = []jen.Code{jen.Id("k")} + case keySize == nil && valSize == nil: + return + } + + b.Add(jen.For(jen.List(forLoopVars...).Op(":=").Range().Add(fieldName))).BlockFunc(func(b *jen.Group) { + if keySize != nil { + b.Add(keySize) + } + if valSize != nil { + b.Add(valSize) + } + }) + }) + + case *types.Pointer: + return sizegen.sizeStmtForType(fieldName, node.Elem(), true) + + case *types.Named: + ts := sizegen.getKnownType(node) + if ts.pod || !ts.local { + if alloc { + if !ts.local { + log.Printf("WARNING: size of external type %s cannot be fully calculated", node) + } + return jen.If(fieldName.Clone().Op("!=").Nil()).Block( + jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(node.Underlying())), + ) + } + return nil + } + return sizegen.sizeStmtForType(fieldName, node.Underlying(), alloc) + + case *types.Interface: + if node.Empty() { + return nil + } + return jen.If( + jen.List( + jen.Id("cc"), jen.Id("ok")). + Op(":="). + Add(fieldName.Clone().Assert(jen.Id("cachedObject"))), + jen.Id("ok"), + ).Block( + jen.Id("size"). + Op("+="). + Id("cc"). + Dot("CachedSize"). + Call(jen.True()), + ) + + case *types.Struct: + return jen.Id("size").Op("+=").Add(fieldName.Clone().Dot("CachedSize").Call(jen.Lit(alloc))) + + case *types.Basic: + if !alloc { + if node.Info()&types.IsString != 0 { + return jen.Id("size").Op("+=").Int64().Call(jen.Len(fieldName)) + } + return nil + } + return jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(node)) + default: + log.Printf("unhandled type: %T", node) + } + + return nil +} + +type typePaths []string + +func (t *typePaths) String() string { + return fmt.Sprintf("%v", *t) +} + +func (t *typePaths) Set(path string) error { + *t = append(*t, path) + return nil +} + +func main() { + var patterns typePaths + var generate typePaths + flag.Var(&patterns, "in", "Go packages to load the generator") + flag.Var(&generate, "gen", "Typename of the Go struct to generate size info for") + flag.Parse() + + loaded, err := packages.Load(&packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule, + Logf: log.Printf, + }, patterns...) + + if err != nil { + log.Fatal(err) + } + + sizegen := NewSizegen(loaded[0].Module, loaded[0].TypesSizes) + + scopes := make(map[string]*types.Scope) + for _, pkg := range loaded { + scopes[pkg.PkgPath] = pkg.Types.Scope() + } + + for _, gen := range generate { + pos := strings.LastIndexByte(gen, '.') + if pos < 0 { + log.Fatalf("unexpected input type: %s", gen) + } + + pkgname := gen[:pos] + typename := gen[pos+1:] + + scope := scopes[pkgname] + if scope == nil { + log.Fatalf("no scope found for type '%s'", gen) + } + + tt := scope.Lookup(typename) + if tt == nil { + log.Fatalf("no type called '%s' found in '%s'", typename, pkgname) + } + + sizegen.GenerateKnownType(tt.Type().(*types.Named)) + } + + sizegen.Finalize() +} From 263d57732128060a9132ac13497af1a7cbfd5bd6 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 26 Jan 2021 18:18:27 +0100 Subject: [PATCH 2/6] go/vt: check in autogenerated sources from sizegen Signed-off-by: Vicent Marti --- go/sqltypes/cached_size.go | 58 + go/vt/key/cached_size.go | 47 + go/vt/proto/query/cached_size.go | 100 + go/vt/proto/topodata/cached_size.go | 39 + go/vt/sqlparser/cached_size.go | 1712 +++++++++++++++++ go/vt/srvtopo/cached_size.go | 39 + go/vt/tableacl/cached_size.go | 39 + go/vt/vtgate/engine/cached_size.go | 800 ++++++++ go/vt/vtgate/evalengine/cached_size.go | 91 + go/vt/vtgate/vindexes/cached_size.go | 477 +++++ go/vt/vttablet/tabletserver/cached_size.go | 51 + .../tabletserver/planbuilder/cached_size.go | 62 + .../tabletserver/rules/cached_size.go | 124 ++ .../tabletserver/schema/cached_size.go | 77 + 14 files changed, 3716 insertions(+) create mode 100644 go/sqltypes/cached_size.go create mode 100644 go/vt/key/cached_size.go create mode 100644 go/vt/proto/query/cached_size.go create mode 100644 go/vt/proto/topodata/cached_size.go create mode 100644 go/vt/sqlparser/cached_size.go create mode 100644 go/vt/srvtopo/cached_size.go create mode 100644 go/vt/tableacl/cached_size.go create mode 100644 go/vt/vtgate/engine/cached_size.go create mode 100644 go/vt/vtgate/evalengine/cached_size.go create mode 100644 go/vt/vtgate/vindexes/cached_size.go create mode 100644 go/vt/vttablet/tabletserver/cached_size.go create mode 100644 go/vt/vttablet/tabletserver/planbuilder/cached_size.go create mode 100644 go/vt/vttablet/tabletserver/rules/cached_size.go create mode 100644 go/vt/vttablet/tabletserver/schema/cached_size.go diff --git a/go/sqltypes/cached_size.go b/go/sqltypes/cached_size.go new file mode 100644 index 00000000000..0d47498fe8a --- /dev/null +++ b/go/sqltypes/cached_size.go @@ -0,0 +1,58 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package sqltypes + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *PlanValue) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(88) + } + // field Key string + size += int64(len(cached.Key)) + // field Value vitess.io/vitess/go/sqltypes.Value + size += cached.Value.CachedSize(false) + // field ListKey string + size += int64(len(cached.ListKey)) + // field Values []vitess.io/vitess/go/sqltypes.PlanValue + { + size += int64(cap(cached.Values)) * int64(88) + for _, elem := range cached.Values { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *Value) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field val []byte + size += int64(cap(cached.val)) + return size +} diff --git a/go/vt/key/cached_size.go b/go/vt/key/cached_size.go new file mode 100644 index 00000000000..6307a708071 --- /dev/null +++ b/go/vt/key/cached_size.go @@ -0,0 +1,47 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package key + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *DestinationExactKeyRange) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field KeyRange *vitess.io/vitess/go/vt/proto/topodata.KeyRange + size += cached.KeyRange.CachedSize(true) + return size +} +func (cached *DestinationKeyRange) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field KeyRange *vitess.io/vitess/go/vt/proto/topodata.KeyRange + size += cached.KeyRange.CachedSize(true) + return size +} diff --git a/go/vt/proto/query/cached_size.go b/go/vt/proto/query/cached_size.go new file mode 100644 index 00000000000..982298af411 --- /dev/null +++ b/go/vt/proto/query/cached_size.go @@ -0,0 +1,100 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package query + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *BindVariable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(84) + } + // field Value []byte + size += int64(cap(cached.Value)) + // field Values []*vitess.io/vitess/go/vt/proto/query.Value + { + size += int64(cap(cached.Values)) * int64(8) + for _, elem := range cached.Values { + size += elem.CachedSize(true) + } + } + // field XXX_unrecognized []byte + size += int64(cap(cached.XXX_unrecognized)) + return size +} +func (cached *Field) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(148) + } + // field Name string + size += int64(len(cached.Name)) + // field Table string + size += int64(len(cached.Table)) + // field OrgTable string + size += int64(len(cached.OrgTable)) + // field Database string + size += int64(len(cached.Database)) + // field OrgName string + size += int64(len(cached.OrgName)) + // field ColumnType string + size += int64(len(cached.ColumnType)) + // field XXX_unrecognized []byte + size += int64(cap(cached.XXX_unrecognized)) + return size +} +func (cached *Target) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(84) + } + // field Keyspace string + size += int64(len(cached.Keyspace)) + // field Shard string + size += int64(len(cached.Shard)) + // field Cell string + size += int64(len(cached.Cell)) + // field XXX_unrecognized []byte + size += int64(cap(cached.XXX_unrecognized)) + return size +} +func (cached *Value) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(60) + } + // field Value []byte + size += int64(cap(cached.Value)) + // field XXX_unrecognized []byte + size += int64(cap(cached.XXX_unrecognized)) + return size +} diff --git a/go/vt/proto/topodata/cached_size.go b/go/vt/proto/topodata/cached_size.go new file mode 100644 index 00000000000..877d02e475b --- /dev/null +++ b/go/vt/proto/topodata/cached_size.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package topodata + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *KeyRange) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(76) + } + // field Start []byte + size += int64(cap(cached.Start)) + // field End []byte + size += int64(cap(cached.End)) + // field XXX_unrecognized []byte + size += int64(cap(cached.XXX_unrecognized)) + return size +} diff --git a/go/vt/sqlparser/cached_size.go b/go/vt/sqlparser/cached_size.go new file mode 100644 index 00000000000..c79bdf1532f --- /dev/null +++ b/go/vt/sqlparser/cached_size.go @@ -0,0 +1,1712 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package sqlparser + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *AddColumns) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Columns []*vitess.io/vitess/go/vt/sqlparser.ColumnDefinition + { + size += int64(cap(cached.Columns)) * int64(8) + for _, elem := range cached.Columns { + size += elem.CachedSize(true) + } + } + // field First *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.First.CachedSize(true) + // field After *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.After.CachedSize(true) + return size +} +func (cached *AddConstraintDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field ConstraintDefinition *vitess.io/vitess/go/vt/sqlparser.ConstraintDefinition + size += cached.ConstraintDefinition.CachedSize(true) + return size +} +func (cached *AddIndexDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field IndexDefinition *vitess.io/vitess/go/vt/sqlparser.IndexDefinition + size += cached.IndexDefinition.CachedSize(true) + return size +} +func (cached *AliasedExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field As vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.As.CachedSize(false) + return size +} +func (cached *AliasedTableExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(64) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.SimpleTableExpr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Partitions vitess.io/vitess/go/vt/sqlparser.Partitions + { + size += int64(cap(cached.Partitions)) * int64(40) + for _, elem := range cached.Partitions { + size += elem.CachedSize(false) + } + } + // field As vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.As.CachedSize(false) + // field Hints *vitess.io/vitess/go/vt/sqlparser.IndexHints + size += cached.Hints.CachedSize(true) + return size +} +func (cached *AlterCharset) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field CharacterSet string + size += int64(len(cached.CharacterSet)) + // field Collate string + size += int64(len(cached.Collate)) + return size +} +func (cached *AlterColumn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Column *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.Column.CachedSize(true) + // field DefaultVal vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.DefaultVal.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *AlterTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(65) + } + // field Table vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Table.CachedSize(false) + // field AlterOptions []vitess.io/vitess/go/vt/sqlparser.AlterOption + { + size += int64(cap(cached.AlterOptions)) * int64(16) + for _, elem := range cached.AlterOptions { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field PartitionSpec *vitess.io/vitess/go/vt/sqlparser.PartitionSpec + size += cached.PartitionSpec.CachedSize(true) + return size +} +func (cached *AlterView) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field ViewName vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.ViewName.CachedSize(false) + // field Algorithm string + size += int64(len(cached.Algorithm)) + // field Definer string + size += int64(len(cached.Definer)) + // field Security string + size += int64(len(cached.Security)) + // field Columns vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.Columns)) * int64(40) + for _, elem := range cached.Columns { + size += elem.CachedSize(false) + } + } + // field Select vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Select.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field CheckOption string + size += int64(len(cached.CheckOption)) + return size +} +func (cached *AlterVschema) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field Table vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Table.CachedSize(false) + // field VindexSpec *vitess.io/vitess/go/vt/sqlparser.VindexSpec + size += cached.VindexSpec.CachedSize(true) + // field VindexCols []vitess.io/vitess/go/vt/sqlparser.ColIdent + { + size += int64(cap(cached.VindexCols)) * int64(40) + for _, elem := range cached.VindexCols { + size += elem.CachedSize(false) + } + } + // field AutoIncSpec *vitess.io/vitess/go/vt/sqlparser.AutoIncSpec + size += cached.AutoIncSpec.CachedSize(true) + return size +} +func (cached *AndExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *AutoIncSpec) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(72) + } + // field Column vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Column.CachedSize(false) + // field Sequence vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Sequence.CachedSize(false) + return size +} +func (cached *BinaryExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *BindVarNeeds) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(73) + } + // field NeedFunctionResult []string + { + size += int64(cap(cached.NeedFunctionResult)) * int64(16) + for _, elem := range cached.NeedFunctionResult { + size += int64(len(elem)) + } + } + // field NeedSystemVariable []string + { + size += int64(cap(cached.NeedSystemVariable)) * int64(16) + for _, elem := range cached.NeedSystemVariable { + size += int64(len(elem)) + } + } + // field NeedUserDefinedVariables []string + { + size += int64(cap(cached.NeedUserDefinedVariables)) * int64(16) + for _, elem := range cached.NeedUserDefinedVariables { + size += int64(len(elem)) + } + } + return size +} +func (cached *CaseExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Whens []*vitess.io/vitess/go/vt/sqlparser.When + { + size += int64(cap(cached.Whens)) * int64(8) + for _, elem := range cached.Whens { + size += elem.CachedSize(true) + } + } + // field Else vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Else.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ChangeColumn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field OldColumn *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.OldColumn.CachedSize(true) + // field NewColDefinition *vitess.io/vitess/go/vt/sqlparser.ColumnDefinition + size += cached.NewColDefinition.CachedSize(true) + // field First *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.First.CachedSize(true) + // field After *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.After.CachedSize(true) + return size +} +func (cached *CheckConstraintDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(17) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ColIdent) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field val string + size += int64(len(cached.val)) + // field lowered string + size += int64(len(cached.lowered)) + return size +} +func (cached *ColName) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(88) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Qualifier vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Qualifier.CachedSize(false) + return size +} +func (cached *CollateExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Charset string + size += int64(len(cached.Charset)) + return size +} +func (cached *ColumnDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(192) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Type vitess.io/vitess/go/vt/sqlparser.ColumnType + size += cached.Type.CachedSize(false) + return size +} +func (cached *ColumnType) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(152) + } + // field Type string + size += int64(len(cached.Type)) + // field Default vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Default.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field OnUpdate vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.OnUpdate.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Comment *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Comment.CachedSize(true) + // field Length *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Length.CachedSize(true) + // field Scale *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Scale.CachedSize(true) + // field Charset string + size += int64(len(cached.Charset)) + // field Collate string + size += int64(len(cached.Collate)) + // field EnumValues []string + { + size += int64(cap(cached.EnumValues)) * int64(16) + for _, elem := range cached.EnumValues { + size += int64(len(elem)) + } + } + return size +} +func (cached *ComparisonExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Escape vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Escape.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ConstraintDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Name string + size += int64(len(cached.Name)) + // field Details vitess.io/vitess/go/vt/sqlparser.ConstraintInfo + if cc, ok := cached.Details.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ConvertExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Type *vitess.io/vitess/go/vt/sqlparser.ConvertType + size += cached.Type.CachedSize(true) + return size +} +func (cached *ConvertType) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Type string + size += int64(len(cached.Type)) + // field Length *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Length.CachedSize(true) + // field Scale *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Scale.CachedSize(true) + // field Charset string + size += int64(len(cached.Charset)) + return size +} +func (cached *ConvertUsingExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Type string + size += int64(len(cached.Type)) + return size +} +func (cached *CreateTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(57) + } + // field Table vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Table.CachedSize(false) + // field TableSpec *vitess.io/vitess/go/vt/sqlparser.TableSpec + size += cached.TableSpec.CachedSize(true) + // field OptLike *vitess.io/vitess/go/vt/sqlparser.OptLike + size += cached.OptLike.CachedSize(true) + return size +} +func (cached *CreateView) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(137) + } + // field ViewName vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.ViewName.CachedSize(false) + // field Algorithm string + size += int64(len(cached.Algorithm)) + // field Definer string + size += int64(len(cached.Definer)) + // field Security string + size += int64(len(cached.Security)) + // field Columns vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.Columns)) * int64(40) + for _, elem := range cached.Columns { + size += elem.CachedSize(false) + } + } + // field Select vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Select.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field CheckOption string + size += int64(len(cached.CheckOption)) + return size +} +func (cached *CurTimeFuncExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Fsp vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Fsp.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Default) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field ColName string + size += int64(len(cached.ColName)) + return size +} +func (cached *DerivedTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Select vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Select.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *DropColumn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field Name *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.Name.CachedSize(true) + return size +} +func (cached *DropKey) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Name string + size += int64(len(cached.Name)) + return size +} +func (cached *DropTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(25) + } + // field FromTables vitess.io/vitess/go/vt/sqlparser.TableNames + { + size += int64(cap(cached.FromTables)) * int64(32) + for _, elem := range cached.FromTables { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *DropView) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(25) + } + // field FromTables vitess.io/vitess/go/vt/sqlparser.TableNames + { + size += int64(cap(cached.FromTables)) * int64(32) + for _, elem := range cached.FromTables { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *ExistsExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field Subquery *vitess.io/vitess/go/vt/sqlparser.Subquery + size += cached.Subquery.CachedSize(true) + return size +} +func (cached *ForeignKeyDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(96) + } + // field Source vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.Source)) * int64(40) + for _, elem := range cached.Source { + size += elem.CachedSize(false) + } + } + // field ReferencedTable vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.ReferencedTable.CachedSize(false) + // field ReferencedColumns vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.ReferencedColumns)) * int64(40) + for _, elem := range cached.ReferencedColumns { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *FuncExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(88) + } + // field Qualifier vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.Qualifier.CachedSize(false) + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Exprs vitess.io/vitess/go/vt/sqlparser.SelectExprs + { + size += int64(cap(cached.Exprs)) * int64(16) + for _, elem := range cached.Exprs { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + return size +} +func (cached *GroupConcatExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field Exprs vitess.io/vitess/go/vt/sqlparser.SelectExprs + { + size += int64(cap(cached.Exprs)) * int64(16) + for _, elem := range cached.Exprs { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field OrderBy vitess.io/vitess/go/vt/sqlparser.OrderBy + { + size += int64(cap(cached.OrderBy)) * int64(8) + for _, elem := range cached.OrderBy { + size += elem.CachedSize(true) + } + } + // field Separator string + size += int64(len(cached.Separator)) + // field Limit *vitess.io/vitess/go/vt/sqlparser.Limit + size += cached.Limit.CachedSize(true) + return size +} +func (cached *IndexColumn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(49) + } + // field Column vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Column.CachedSize(false) + // field Length *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Length.CachedSize(true) + return size +} +func (cached *IndexDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Info *vitess.io/vitess/go/vt/sqlparser.IndexInfo + size += cached.Info.CachedSize(true) + // field Columns []*vitess.io/vitess/go/vt/sqlparser.IndexColumn + { + size += int64(cap(cached.Columns)) * int64(8) + for _, elem := range cached.Columns { + size += elem.CachedSize(true) + } + } + // field Options []*vitess.io/vitess/go/vt/sqlparser.IndexOption + { + size += int64(cap(cached.Options)) * int64(8) + for _, elem := range cached.Options { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *IndexHints) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Indexes []vitess.io/vitess/go/vt/sqlparser.ColIdent + { + size += int64(cap(cached.Indexes)) * int64(40) + for _, elem := range cached.Indexes { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *IndexInfo) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(100) + } + // field Type string + size += int64(len(cached.Type)) + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field ConstraintName vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.ConstraintName.CachedSize(false) + return size +} +func (cached *IndexOption) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Name string + size += int64(len(cached.Name)) + // field Value *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Value.CachedSize(true) + // field String string + size += int64(len(cached.String)) + return size +} +func (cached *IntervalExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Unit string + size += int64(len(cached.Unit)) + return size +} +func (cached *IsExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *JoinCondition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field On vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.On.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Using vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.Using)) * int64(40) + for _, elem := range cached.Using { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *JoinTableExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field LeftExpr vitess.io/vitess/go/vt/sqlparser.TableExpr + if cc, ok := cached.LeftExpr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field RightExpr vitess.io/vitess/go/vt/sqlparser.TableExpr + if cc, ok := cached.RightExpr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Condition vitess.io/vitess/go/vt/sqlparser.JoinCondition + size += cached.Condition.CachedSize(false) + return size +} +func (cached *KeyState) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(1) + } + return size +} +func (cached *Limit) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Offset vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Offset.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Rowcount vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Rowcount.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Literal) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Val []byte + size += int64(cap(cached.Val)) + return size +} +func (cached *LockOption) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(1) + } + return size +} +func (cached *MatchExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(41) + } + // field Columns vitess.io/vitess/go/vt/sqlparser.SelectExprs + { + size += int64(cap(cached.Columns)) * int64(16) + for _, elem := range cached.Columns { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ModifyColumn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field NewColDefinition *vitess.io/vitess/go/vt/sqlparser.ColumnDefinition + size += cached.NewColDefinition.CachedSize(true) + // field First *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.First.CachedSize(true) + // field After *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.After.CachedSize(true) + return size +} +func (cached *Nextval) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *NotExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *OptLike) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field LikeTable vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.LikeTable.CachedSize(false) + return size +} +func (cached *OrExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Order) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(17) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *OrderByOption) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Cols vitess.io/vitess/go/vt/sqlparser.Columns + { + size += int64(cap(cached.Cols)) * int64(40) + for _, elem := range cached.Cols { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *ParenSelect) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Select vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Select.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ParenTableExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Exprs vitess.io/vitess/go/vt/sqlparser.TableExprs + { + size += int64(cap(cached.Exprs)) * int64(16) + for _, elem := range cached.Exprs { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + return size +} +func (cached *ParsedQuery) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Query string + size += int64(len(cached.Query)) + // field bindLocations []vitess.io/vitess/go/vt/sqlparser.bindLocation + { + size += int64(cap(cached.bindLocations)) * int64(16) + } + return size +} +func (cached *PartitionDefinition) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(57) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Limit vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Limit.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *PartitionSpec) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(112) + } + // field Names vitess.io/vitess/go/vt/sqlparser.Partitions + { + size += int64(cap(cached.Names)) * int64(40) + for _, elem := range cached.Names { + size += elem.CachedSize(false) + } + } + // field Number *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Number.CachedSize(true) + // field TableName vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.TableName.CachedSize(false) + // field Definitions []*vitess.io/vitess/go/vt/sqlparser.PartitionDefinition + { + size += int64(cap(cached.Definitions)) * int64(8) + for _, elem := range cached.Definitions { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *RangeCond) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field From vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.From.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field To vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.To.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *RenameIndex) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field OldName string + size += int64(len(cached.OldName)) + // field NewName string + size += int64(len(cached.NewName)) + return size +} +func (cached *RenameTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field TablePairs []*vitess.io/vitess/go/vt/sqlparser.RenameTablePair + { + size += int64(cap(cached.TablePairs)) * int64(8) + for _, elem := range cached.TablePairs { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *RenameTableName) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Table vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Table.CachedSize(false) + return size +} +func (cached *RenameTablePair) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(64) + } + // field FromTable vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.FromTable.CachedSize(false) + // field ToTable vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.ToTable.CachedSize(false) + return size +} +func (cached *Select) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(176) + } + // field Cache *bool + size += int64(1) + // field Comments vitess.io/vitess/go/vt/sqlparser.Comments + { + size += int64(cap(cached.Comments)) * int64(24) + for _, elem := range cached.Comments { + size += int64(cap(elem)) + } + } + // field SelectExprs vitess.io/vitess/go/vt/sqlparser.SelectExprs + { + size += int64(cap(cached.SelectExprs)) * int64(16) + for _, elem := range cached.SelectExprs { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field From vitess.io/vitess/go/vt/sqlparser.TableExprs + { + size += int64(cap(cached.From)) * int64(16) + for _, elem := range cached.From { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field Where *vitess.io/vitess/go/vt/sqlparser.Where + size += cached.Where.CachedSize(true) + // field GroupBy vitess.io/vitess/go/vt/sqlparser.GroupBy + { + size += int64(cap(cached.GroupBy)) * int64(16) + for _, elem := range cached.GroupBy { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field Having *vitess.io/vitess/go/vt/sqlparser.Where + size += cached.Having.CachedSize(true) + // field OrderBy vitess.io/vitess/go/vt/sqlparser.OrderBy + { + size += int64(cap(cached.OrderBy)) * int64(8) + for _, elem := range cached.OrderBy { + size += elem.CachedSize(true) + } + } + // field Limit *vitess.io/vitess/go/vt/sqlparser.Limit + size += cached.Limit.CachedSize(true) + // field Into *vitess.io/vitess/go/vt/sqlparser.SelectInto + size += cached.Into.CachedSize(true) + return size +} +func (cached *SelectInto) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(104) + } + // field FileName string + size += int64(len(cached.FileName)) + // field Charset string + size += int64(len(cached.Charset)) + // field FormatOption string + size += int64(len(cached.FormatOption)) + // field ExportOption string + size += int64(len(cached.ExportOption)) + // field Manifest string + size += int64(len(cached.Manifest)) + // field Overwrite string + size += int64(len(cached.Overwrite)) + return size +} +func (cached *StarExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field TableName vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.TableName.CachedSize(false) + return size +} +func (cached *Subquery) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Select vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Select.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *SubstrExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field Name *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.Name.CachedSize(true) + // field StrVal *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.StrVal.CachedSize(true) + // field From vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.From.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field To vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.To.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *TableIdent) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field v string + size += int64(len(cached.v)) + return size +} +func (cached *TableName) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Name vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.Name.CachedSize(false) + // field Qualifier vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.Qualifier.CachedSize(false) + return size +} +func (cached *TableOption) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(64) + } + // field Name string + size += int64(len(cached.Name)) + // field Value *vitess.io/vitess/go/vt/sqlparser.Literal + size += cached.Value.CachedSize(true) + // field String string + size += int64(len(cached.String)) + // field Tables vitess.io/vitess/go/vt/sqlparser.TableNames + { + size += int64(cap(cached.Tables)) * int64(32) + for _, elem := range cached.Tables { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *TableSpec) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(96) + } + // field Columns []*vitess.io/vitess/go/vt/sqlparser.ColumnDefinition + { + size += int64(cap(cached.Columns)) * int64(8) + for _, elem := range cached.Columns { + size += elem.CachedSize(true) + } + } + // field Indexes []*vitess.io/vitess/go/vt/sqlparser.IndexDefinition + { + size += int64(cap(cached.Indexes)) * int64(8) + for _, elem := range cached.Indexes { + size += elem.CachedSize(true) + } + } + // field Constraints []*vitess.io/vitess/go/vt/sqlparser.ConstraintDefinition + { + size += int64(cap(cached.Constraints)) * int64(8) + for _, elem := range cached.Constraints { + size += elem.CachedSize(true) + } + } + // field Options vitess.io/vitess/go/vt/sqlparser.TableOptions + { + size += int64(cap(cached.Options)) * int64(8) + for _, elem := range cached.Options { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *TablespaceOperation) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(1) + } + return size +} +func (cached *TimestampFuncExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(64) + } + // field Name string + size += int64(len(cached.Name)) + // field Expr1 vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr1.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Expr2 vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr2.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Unit string + size += int64(len(cached.Unit)) + return size +} +func (cached *TruncateTable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Table vitess.io/vitess/go/vt/sqlparser.TableName + size += cached.Table.CachedSize(false) + return size +} +func (cached *UnaryExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Union) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(73) + } + // field FirstStatement vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.FirstStatement.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field UnionSelects []*vitess.io/vitess/go/vt/sqlparser.UnionSelect + { + size += int64(cap(cached.UnionSelects)) * int64(8) + for _, elem := range cached.UnionSelects { + size += elem.CachedSize(true) + } + } + // field OrderBy vitess.io/vitess/go/vt/sqlparser.OrderBy + { + size += int64(cap(cached.OrderBy)) * int64(8) + for _, elem := range cached.OrderBy { + size += elem.CachedSize(true) + } + } + // field Limit *vitess.io/vitess/go/vt/sqlparser.Limit + size += cached.Limit.CachedSize(true) + return size +} +func (cached *UnionSelect) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Statement vitess.io/vitess/go/vt/sqlparser.SelectStatement + if cc, ok := cached.Statement.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Validation) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(1) + } + return size +} +func (cached *ValuesFuncExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field Name *vitess.io/vitess/go/vt/sqlparser.ColName + size += cached.Name.CachedSize(true) + return size +} +func (cached *VindexParam) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Key vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Key.CachedSize(false) + // field Val string + size += int64(len(cached.Val)) + return size +} +func (cached *VindexSpec) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(104) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + // field Type vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Type.CachedSize(false) + // field Params []vitess.io/vitess/go/vt/sqlparser.VindexParam + { + size += int64(cap(cached.Params)) * int64(56) + for _, elem := range cached.Params { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *When) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Cond vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Cond.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Val vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Val.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Where) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Expr vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *XorExpr) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Left vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/sqlparser.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} diff --git a/go/vt/srvtopo/cached_size.go b/go/vt/srvtopo/cached_size.go new file mode 100644 index 00000000000..03dd1ceb0da --- /dev/null +++ b/go/vt/srvtopo/cached_size.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package srvtopo + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *ResolvedShard) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Target *vitess.io/vitess/go/vt/proto/query.Target + size += cached.Target.CachedSize(true) + // field Gateway vitess.io/vitess/go/vt/srvtopo.Gateway + if cc, ok := cached.Gateway.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} diff --git a/go/vt/tableacl/cached_size.go b/go/vt/tableacl/cached_size.go new file mode 100644 index 00000000000..63935b92a4d --- /dev/null +++ b/go/vt/tableacl/cached_size.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package tableacl + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *ACLResult) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field ACL vitess.io/vitess/go/vt/tableacl/acl.ACL + if cc, ok := cached.ACL.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field GroupName string + size += int64(len(cached.GroupName)) + return size +} diff --git a/go/vt/vtgate/engine/cached_size.go b/go/vt/vtgate/engine/cached_size.go new file mode 100644 index 00000000000..90b5798255e --- /dev/null +++ b/go/vt/vtgate/engine/cached_size.go @@ -0,0 +1,800 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package engine + +import ( + "math" + "reflect" + "unsafe" +) + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *AggregateParams) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Alias string + size += int64(len(cached.Alias)) + return size +} +func (cached *AlterVSchema) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field AlterVschemaDDL *vitess.io/vitess/go/vt/sqlparser.AlterVschema + size += cached.AlterVschemaDDL.CachedSize(true) + return size +} +func (cached *Concatenate) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field Sources []vitess.io/vitess/go/vt/vtgate/engine.Primitive + { + size += int64(cap(cached.Sources)) * int64(16) + for _, elem := range cached.Sources { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + return size +} +func (cached *DDL) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field SQL string + size += int64(len(cached.SQL)) + // field DDL vitess.io/vitess/go/vt/sqlparser.DDLStatement + if cc, ok := cached.DDL.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field NormalDDL *vitess.io/vitess/go/vt/vtgate/engine.Send + size += cached.NormalDDL.CachedSize(true) + // field OnlineDDL *vitess.io/vitess/go/vt/vtgate/engine.OnlineDDL + size += cached.OnlineDDL.CachedSize(true) + return size +} +func (cached *DML) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(144) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Query string + size += int64(len(cached.Query)) + // field Vindex vitess.io/vitess/go/vt/vtgate/vindexes.SingleColumn + if cc, ok := cached.Vindex.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Values []vitess.io/vitess/go/sqltypes.PlanValue + { + size += int64(cap(cached.Values)) * int64(88) + for _, elem := range cached.Values { + size += elem.CachedSize(false) + } + } + // field KsidVindex vitess.io/vitess/go/vt/vtgate/vindexes.SingleColumn + if cc, ok := cached.KsidVindex.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Table *vitess.io/vitess/go/vt/vtgate/vindexes.Table + size += cached.Table.CachedSize(true) + // field OwnedVindexQuery string + size += int64(len(cached.OwnedVindexQuery)) + return size +} +func (cached *Delete) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(144) + } + // field DML vitess.io/vitess/go/vt/vtgate/engine.DML + size += cached.DML.CachedSize(false) + return size +} +func (cached *Distinct) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Source vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Source.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Generate) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(112) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field Query string + size += int64(len(cached.Query)) + // field Values vitess.io/vitess/go/sqltypes.PlanValue + size += cached.Values.CachedSize(false) + return size +} +func (cached *Insert) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(144) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field Query string + size += int64(len(cached.Query)) + // field VindexValues []vitess.io/vitess/go/sqltypes.PlanValue + { + size += int64(cap(cached.VindexValues)) * int64(88) + for _, elem := range cached.VindexValues { + size += elem.CachedSize(false) + } + } + // field Table *vitess.io/vitess/go/vt/vtgate/vindexes.Table + size += cached.Table.CachedSize(true) + // field Generate *vitess.io/vitess/go/vt/vtgate/engine.Generate + size += cached.Generate.CachedSize(true) + // field Prefix string + size += int64(len(cached.Prefix)) + // field Mid []string + { + size += int64(cap(cached.Mid)) * int64(16) + for _, elem := range cached.Mid { + size += int64(len(elem)) + } + } + // field Suffix string + size += int64(len(cached.Suffix)) + return size +} +func (cached *Join) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(72) + } + // field Left vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Cols []int + { + size += int64(cap(cached.Cols)) * int64(8) + } + // field Vars map[string]int + if cached.Vars != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.Vars) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.Vars) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + for k := range cached.Vars { + size += int64(len(k)) + } + } + return size +} +func (cached *Limit) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(192) + } + // field Count vitess.io/vitess/go/sqltypes.PlanValue + size += cached.Count.CachedSize(false) + // field Offset vitess.io/vitess/go/sqltypes.PlanValue + size += cached.Offset.CachedSize(false) + // field Input vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Input.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Lock) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Query string + size += int64(len(cached.Query)) + return size +} +func (cached *MemorySort) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field UpperLimit vitess.io/vitess/go/sqltypes.PlanValue + size += cached.UpperLimit.CachedSize(false) + // field OrderBy []vitess.io/vitess/go/vt/vtgate/engine.OrderbyParams + { + size += int64(cap(cached.OrderBy)) * int64(9) + } + // field Input vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Input.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *MergeSort) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field Primitives []vitess.io/vitess/go/vt/vtgate/engine.StreamExecutor + { + size += int64(cap(cached.Primitives)) * int64(16) + for _, elem := range cached.Primitives { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field OrderBy []vitess.io/vitess/go/vt/vtgate/engine.OrderbyParams + { + size += int64(cap(cached.OrderBy)) * int64(9) + } + return size +} +func (cached *OnlineDDL) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(72) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field DDL vitess.io/vitess/go/vt/sqlparser.DDLStatement + if cc, ok := cached.DDL.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field SQL string + size += int64(len(cached.SQL)) + // field Strategy vitess.io/vitess/go/vt/schema.DDLStrategy + size += int64(len(cached.Strategy)) + // field Options string + size += int64(len(cached.Options)) + return size +} +func (cached *OrderedAggregate) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field Aggregates []vitess.io/vitess/go/vt/vtgate/engine.AggregateParams + { + size += int64(cap(cached.Aggregates)) * int64(32) + for _, elem := range cached.Aggregates { + size += elem.CachedSize(false) + } + } + // field Keys []int + { + size += int64(cap(cached.Keys)) * int64(8) + } + // field Input vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Input.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Plan) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(96) + } + // field Original string + size += int64(len(cached.Original)) + // field Instructions vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Instructions.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field BindVarNeeds *vitess.io/vitess/go/vt/sqlparser.BindVarNeeds + size += cached.BindVarNeeds.CachedSize(true) + return size +} +func (cached *Projection) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(64) + } + // field Cols []string + { + size += int64(cap(cached.Cols)) * int64(16) + for _, elem := range cached.Cols { + size += int64(len(elem)) + } + } + // field Exprs []vitess.io/vitess/go/vt/vtgate/evalengine.Expr + { + size += int64(cap(cached.Exprs)) * int64(16) + for _, elem := range cached.Exprs { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field Input vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Input.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *PulloutSubquery) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(72) + } + // field SubqueryResult string + size += int64(len(cached.SubqueryResult)) + // field HasValues string + size += int64(len(cached.HasValues)) + // field Subquery vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Subquery.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Underlying vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Underlying.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Route) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(208) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Query string + size += int64(len(cached.Query)) + // field TableName string + size += int64(len(cached.TableName)) + // field FieldQuery string + size += int64(len(cached.FieldQuery)) + // field Vindex vitess.io/vitess/go/vt/vtgate/vindexes.SingleColumn + if cc, ok := cached.Vindex.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Values []vitess.io/vitess/go/sqltypes.PlanValue + { + size += int64(cap(cached.Values)) * int64(88) + for _, elem := range cached.Values { + size += elem.CachedSize(false) + } + } + // field OrderBy []vitess.io/vitess/go/vt/vtgate/engine.OrderbyParams + { + size += int64(cap(cached.OrderBy)) * int64(9) + } + // field SysTableTableSchema vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.SysTableTableSchema.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field SysTableTableName vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.SysTableTableName.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Rows) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field rows [][]vitess.io/vitess/go/sqltypes.Value + { + size += int64(cap(cached.rows)) * int64(24) + for _, elem := range cached.rows { + { + size += int64(cap(elem)) * int64(32) + for _, elem := range elem { + size += elem.CachedSize(false) + } + } + } + } + // field fields []*vitess.io/vitess/go/vt/proto/query.Field + { + size += int64(cap(cached.fields)) * int64(8) + for _, elem := range cached.fields { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *SQLCalcFoundRows) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field LimitPrimitive vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.LimitPrimitive.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field CountPrimitive vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.CountPrimitive.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Send) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(42) + } + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Query string + size += int64(len(cached.Query)) + return size +} +func (cached *Set) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Ops []vitess.io/vitess/go/vt/vtgate/engine.SetOp + { + size += int64(cap(cached.Ops)) * int64(16) + for _, elem := range cached.Ops { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + // field Input vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Input.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Subquery) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(40) + } + // field Cols []int + { + size += int64(cap(cached.Cols)) * int64(8) + } + // field Subquery vitess.io/vitess/go/vt/vtgate/engine.Primitive + if cc, ok := cached.Subquery.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *SysVarCheckAndIgnore) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Name string + size += int64(len(cached.Name)) + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Expr string + size += int64(len(cached.Expr)) + return size +} +func (cached *SysVarIgnore) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Name string + size += int64(len(cached.Name)) + // field Expr string + size += int64(len(cached.Expr)) + return size +} +func (cached *SysVarReservedConn) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Name string + size += int64(len(cached.Name)) + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field TargetDestination vitess.io/vitess/go/vt/key.Destination + if cc, ok := cached.TargetDestination.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Expr string + size += int64(len(cached.Expr)) + return size +} +func (cached *SysVarSetAware) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Name string + size += int64(len(cached.Name)) + // field Expr vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Update) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(152) + } + // field DML vitess.io/vitess/go/vt/vtgate/engine.DML + size += cached.DML.CachedSize(false) + // field ChangedVindexValues map[string]*vitess.io/vitess/go/vt/vtgate/engine.VindexValues + if cached.ChangedVindexValues != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.ChangedVindexValues) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.ChangedVindexValues) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + for k, v := range cached.ChangedVindexValues { + size += int64(len(k)) + size += v.CachedSize(true) + } + } + return size +} +func (cached *UpdateTarget) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Target string + size += int64(len(cached.Target)) + return size +} +func (cached *UserDefinedVariable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field Name string + size += int64(len(cached.Name)) + // field Expr vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *VindexFunc) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(160) + } + // field Fields []*vitess.io/vitess/go/vt/proto/query.Field + { + size += int64(cap(cached.Fields)) * int64(8) + for _, elem := range cached.Fields { + size += elem.CachedSize(true) + } + } + // field Cols []int + { + size += int64(cap(cached.Cols)) * int64(8) + } + // field Vindex vitess.io/vitess/go/vt/vtgate/vindexes.SingleColumn + if cc, ok := cached.Vindex.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Value vitess.io/vitess/go/sqltypes.PlanValue + size += cached.Value.CachedSize(false) + return size +} +func (cached *VindexValues) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field PvMap map[string]vitess.io/vitess/go/sqltypes.PlanValue + if cached.PvMap != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.PvMap) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 848) + if len(cached.PvMap) > 0 || numBuckets > 1 { + size += int64(numBuckets * 848) + } + for k, v := range cached.PvMap { + size += int64(len(k)) + size += v.CachedSize(false) + } + } + return size +} +func (cached *shardRoute) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field query string + size += int64(len(cached.query)) + // field rs *vitess.io/vitess/go/vt/srvtopo.ResolvedShard + size += cached.rs.CachedSize(true) + // field bv map[string]*vitess.io/vitess/go/vt/proto/query.BindVariable + if cached.bv != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.bv) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.bv) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + for k, v := range cached.bv { + size += int64(len(k)) + size += v.CachedSize(true) + } + } + return size +} diff --git a/go/vt/vtgate/evalengine/cached_size.go b/go/vt/vtgate/evalengine/cached_size.go new file mode 100644 index 00000000000..53fc71e4ffb --- /dev/null +++ b/go/vt/vtgate/evalengine/cached_size.go @@ -0,0 +1,91 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package evalengine + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *BinaryOp) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field Expr vitess.io/vitess/go/vt/vtgate/evalengine.BinaryExpr + if cc, ok := cached.Expr.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Left vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.Left.(cachedObject); ok { + size += cc.CachedSize(true) + } + // field Right vitess.io/vitess/go/vt/vtgate/evalengine.Expr + if cc, ok := cached.Right.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *BindVariable) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field Key string + size += int64(len(cached.Key)) + return size +} +func (cached *Column) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + return size +} +func (cached *EvalResult) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field bytes []byte + size += int64(cap(cached.bytes)) + return size +} +func (cached *Literal) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(56) + } + // field Val vitess.io/vitess/go/vt/vtgate/evalengine.EvalResult + size += cached.Val.CachedSize(false) + return size +} diff --git a/go/vt/vtgate/vindexes/cached_size.go b/go/vt/vtgate/vindexes/cached_size.go new file mode 100644 index 00000000000..4310740baea --- /dev/null +++ b/go/vt/vtgate/vindexes/cached_size.go @@ -0,0 +1,477 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package vindexes + +import ( + "math" + "reflect" + "unsafe" +) + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *AutoIncrement) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field Column vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Column.CachedSize(false) + // field Sequence *vitess.io/vitess/go/vt/vtgate/vindexes.Table + size += cached.Sequence.CachedSize(true) + return size +} +func (cached *Binary) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *BinaryMD5) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *Column) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(44) + } + // field Name vitess.io/vitess/go/vt/sqlparser.ColIdent + size += cached.Name.CachedSize(false) + return size +} +func (cached *ColumnVindex) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field Columns []vitess.io/vitess/go/vt/sqlparser.ColIdent + { + size += int64(cap(cached.Columns)) * int64(40) + for _, elem := range cached.Columns { + size += elem.CachedSize(false) + } + } + // field Type string + size += int64(len(cached.Type)) + // field Name string + size += int64(len(cached.Name)) + // field Vindex vitess.io/vitess/go/vt/vtgate/vindexes.Vindex + if cc, ok := cached.Vindex.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *ConsistentLookup) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field clCommon *vitess.io/vitess/go/vt/vtgate/vindexes.clCommon + size += cached.clCommon.CachedSize(true) + return size +} +func (cached *ConsistentLookupUnique) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field clCommon *vitess.io/vitess/go/vt/vtgate/vindexes.clCommon + size += cached.clCommon.CachedSize(true) + return size +} +func (cached *Hash) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *Keyspace) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(17) + } + // field Name string + size += int64(len(cached.Name)) + return size +} +func (cached *LookupHash) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *LookupHashUnique) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *LookupNonUnique) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *LookupUnicodeLooseMD5Hash) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *LookupUnicodeLooseMD5HashUnique) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *LookupUnique) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(136) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + return size +} +func (cached *Null) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *Numeric) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *NumericStaticMap) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field name string + size += int64(len(cached.name)) + // field lookup vitess.io/vitess/go/vt/vtgate/vindexes.NumericLookupTable + if cached.lookup != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.lookup) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 144) + if len(cached.lookup) > 0 || numBuckets > 1 { + size += int64(numBuckets * 144) + } + } + return size +} +func (cached *RegionExperimental) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *RegionJSON) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field name string + size += int64(len(cached.name)) + // field regionMap vitess.io/vitess/go/vt/vtgate/vindexes.RegionMap + if cached.regionMap != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.regionMap) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.regionMap) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + for k := range cached.regionMap { + size += int64(len(k)) + } + } + return size +} +func (cached *ReverseBits) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *Table) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(169) + } + // field Type string + size += int64(len(cached.Type)) + // field Name vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.Name.CachedSize(false) + // field Keyspace *vitess.io/vitess/go/vt/vtgate/vindexes.Keyspace + size += cached.Keyspace.CachedSize(true) + // field ColumnVindexes []*vitess.io/vitess/go/vt/vtgate/vindexes.ColumnVindex + { + size += int64(cap(cached.ColumnVindexes)) * int64(8) + for _, elem := range cached.ColumnVindexes { + size += elem.CachedSize(true) + } + } + // field Ordered []*vitess.io/vitess/go/vt/vtgate/vindexes.ColumnVindex + { + size += int64(cap(cached.Ordered)) * int64(8) + for _, elem := range cached.Ordered { + size += elem.CachedSize(true) + } + } + // field Owned []*vitess.io/vitess/go/vt/vtgate/vindexes.ColumnVindex + { + size += int64(cap(cached.Owned)) * int64(8) + for _, elem := range cached.Owned { + size += elem.CachedSize(true) + } + } + // field AutoIncrement *vitess.io/vitess/go/vt/vtgate/vindexes.AutoIncrement + size += cached.AutoIncrement.CachedSize(true) + // field Columns []vitess.io/vitess/go/vt/vtgate/vindexes.Column + { + size += int64(cap(cached.Columns)) * int64(44) + for _, elem := range cached.Columns { + size += elem.CachedSize(false) + } + } + // field Pinned []byte + size += int64(cap(cached.Pinned)) + return size +} +func (cached *UnicodeLooseMD5) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *UnicodeLooseXXHash) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *XXHash) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field name string + size += int64(len(cached.name)) + return size +} +func (cached *clCommon) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(256) + } + // field name string + size += int64(len(cached.name)) + // field lkp vitess.io/vitess/go/vt/vtgate/vindexes.lookupInternal + size += cached.lkp.CachedSize(false) + // field keyspace string + size += int64(len(cached.keyspace)) + // field ownerTable string + size += int64(len(cached.ownerTable)) + // field ownerColumns []string + { + size += int64(cap(cached.ownerColumns)) * int64(16) + for _, elem := range cached.ownerColumns { + size += int64(len(elem)) + } + } + // field lockLookupQuery string + size += int64(len(cached.lockLookupQuery)) + // field lockOwnerQuery string + size += int64(len(cached.lockOwnerQuery)) + // field insertLookupQuery string + size += int64(len(cached.insertLookupQuery)) + // field updateLookupQuery string + size += int64(len(cached.updateLookupQuery)) + return size +} +func (cached *lookupInternal) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(112) + } + // field Table string + size += int64(len(cached.Table)) + // field FromColumns []string + { + size += int64(cap(cached.FromColumns)) * int64(16) + for _, elem := range cached.FromColumns { + size += int64(len(elem)) + } + } + // field To string + size += int64(len(cached.To)) + // field sel string + size += int64(len(cached.sel)) + // field ver string + size += int64(len(cached.ver)) + // field del string + size += int64(len(cached.del)) + return size +} diff --git a/go/vt/vttablet/tabletserver/cached_size.go b/go/vt/vttablet/tabletserver/cached_size.go new file mode 100644 index 00000000000..7cd0a08b662 --- /dev/null +++ b/go/vt/vttablet/tabletserver/cached_size.go @@ -0,0 +1,51 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package tabletserver + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *TabletPlan) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(112) + } + // field Plan *vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder.Plan + size += cached.Plan.CachedSize(true) + // field Fields []*vitess.io/vitess/go/vt/proto/query.Field + { + size += int64(cap(cached.Fields)) * int64(8) + for _, elem := range cached.Fields { + size += elem.CachedSize(true) + } + } + // field Rules *vitess.io/vitess/go/vt/vttablet/tabletserver/rules.Rules + size += cached.Rules.CachedSize(true) + // field Authorized []*vitess.io/vitess/go/vt/tableacl.ACLResult + { + size += int64(cap(cached.Authorized)) * int64(8) + for _, elem := range cached.Authorized { + size += elem.CachedSize(true) + } + } + return size +} diff --git a/go/vt/vttablet/tabletserver/planbuilder/cached_size.go b/go/vt/vttablet/tabletserver/planbuilder/cached_size.go new file mode 100644 index 00000000000..37abf2ff5fb --- /dev/null +++ b/go/vt/vttablet/tabletserver/planbuilder/cached_size.go @@ -0,0 +1,62 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package planbuilder + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *Permission) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field TableName string + size += int64(len(cached.TableName)) + return size +} +func (cached *Plan) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(152) + } + // field Table *vitess.io/vitess/go/vt/vttablet/tabletserver/schema.Table + size += cached.Table.CachedSize(true) + // field Permissions []vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder.Permission + { + size += int64(cap(cached.Permissions)) * int64(24) + for _, elem := range cached.Permissions { + size += elem.CachedSize(false) + } + } + // field FieldQuery *vitess.io/vitess/go/vt/sqlparser.ParsedQuery + size += cached.FieldQuery.CachedSize(true) + // field FullQuery *vitess.io/vitess/go/vt/sqlparser.ParsedQuery + size += cached.FullQuery.CachedSize(true) + // field NextCount vitess.io/vitess/go/sqltypes.PlanValue + size += cached.NextCount.CachedSize(false) + // field WhereClause *vitess.io/vitess/go/vt/sqlparser.ParsedQuery + size += cached.WhereClause.CachedSize(true) + return size +} diff --git a/go/vt/vttablet/tabletserver/rules/cached_size.go b/go/vt/vttablet/tabletserver/rules/cached_size.go new file mode 100644 index 00000000000..61cd23586c2 --- /dev/null +++ b/go/vt/vttablet/tabletserver/rules/cached_size.go @@ -0,0 +1,124 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package rules + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *BindVarCond) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field name string + size += int64(len(cached.name)) + // field value vitess.io/vitess/go/vt/vttablet/tabletserver/rules.bvcValue + if cc, ok := cached.value.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *Rule) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(184) + } + // field Description string + size += int64(len(cached.Description)) + // field Name string + size += int64(len(cached.Name)) + // field requestIP vitess.io/vitess/go/vt/vttablet/tabletserver/rules.namedRegexp + size += cached.requestIP.CachedSize(false) + // field user vitess.io/vitess/go/vt/vttablet/tabletserver/rules.namedRegexp + size += cached.user.CachedSize(false) + // field query vitess.io/vitess/go/vt/vttablet/tabletserver/rules.namedRegexp + size += cached.query.CachedSize(false) + // field plans []vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder.PlanType + { + size += int64(cap(cached.plans)) * int64(8) + } + // field tableNames []string + { + size += int64(cap(cached.tableNames)) * int64(16) + for _, elem := range cached.tableNames { + size += int64(len(elem)) + } + } + // field bindVarConds []vitess.io/vitess/go/vt/vttablet/tabletserver/rules.BindVarCond + { + size += int64(cap(cached.bindVarConds)) * int64(48) + for _, elem := range cached.bindVarConds { + size += elem.CachedSize(false) + } + } + return size +} +func (cached *Rules) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field rules []*vitess.io/vitess/go/vt/vttablet/tabletserver/rules.Rule + { + size += int64(cap(cached.rules)) * int64(8) + for _, elem := range cached.rules { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *bvcre) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field re *regexp.Regexp + if cached.re != nil { + size += int64(153) + } + return size +} +func (cached *namedRegexp) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field name string + size += int64(len(cached.name)) + // field Regexp *regexp.Regexp + if cached.Regexp != nil { + size += int64(153) + } + return size +} diff --git a/go/vt/vttablet/tabletserver/schema/cached_size.go b/go/vt/vttablet/tabletserver/schema/cached_size.go new file mode 100644 index 00000000000..5f7941c071c --- /dev/null +++ b/go/vt/vttablet/tabletserver/schema/cached_size.go @@ -0,0 +1,77 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package schema + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *MessageInfo) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(80) + } + // field Fields []*vitess.io/vitess/go/vt/proto/query.Field + { + size += int64(cap(cached.Fields)) * int64(8) + for _, elem := range cached.Fields { + size += elem.CachedSize(true) + } + } + return size +} +func (cached *SequenceInfo) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + return size +} +func (cached *Table) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(88) + } + // field Name vitess.io/vitess/go/vt/sqlparser.TableIdent + size += cached.Name.CachedSize(false) + // field Fields []*vitess.io/vitess/go/vt/proto/query.Field + { + size += int64(cap(cached.Fields)) * int64(8) + for _, elem := range cached.Fields { + size += elem.CachedSize(true) + } + } + // field PKColumns []int + { + size += int64(cap(cached.PKColumns)) * int64(8) + } + // field SequenceInfo *vitess.io/vitess/go/vt/vttablet/tabletserver/schema.SequenceInfo + size += cached.SequenceInfo.CachedSize(true) + // field MessageInfo *vitess.io/vitess/go/vt/vttablet/tabletserver/schema.MessageInfo + size += cached.MessageInfo.CachedSize(true) + return size +} From d290f8b43c91cb451f54afdd3fa03783542eec1e Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Thu, 28 Jan 2021 15:10:07 +0100 Subject: [PATCH 3/6] sizegen: handle optional features on the codegen These two features are now handled exceptionally: - Generated files that don't attempt to perform interface casts no longer have a local interface definition for `cachedObject` - Generated functions that use `unsafe.Pointer` for hashmap size calculations are now properly flagged with a pragma to prevent a false positive in the compiler `ptrcheck` pass. - Abstracted the out filesystem to make things easier to test Signed-off-by: Vicent Marti Signed-off-by: Andres Taylor Signed-off-by: Vicent Marti --- go/sqltypes/cached_size.go | 4 - go/tools/sizegen/sizegen.go | 249 ++++++++++++------ go/tools/sizegen/sizegen_test.go | 61 +++++ go/vt/key/cached_size.go | 4 - go/vt/proto/query/cached_size.go | 4 - go/vt/proto/topodata/cached_size.go | 4 - go/vt/vtgate/engine/cached_size.go | 8 + go/vt/vtgate/vindexes/cached_size.go | 4 + go/vt/vttablet/tabletserver/cached_size.go | 4 - .../tabletserver/planbuilder/cached_size.go | 4 - .../tabletserver/schema/cached_size.go | 4 - 11 files changed, 237 insertions(+), 113 deletions(-) create mode 100644 go/tools/sizegen/sizegen_test.go diff --git a/go/sqltypes/cached_size.go b/go/sqltypes/cached_size.go index 0d47498fe8a..16d713212b2 100644 --- a/go/sqltypes/cached_size.go +++ b/go/sqltypes/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package sqltypes -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *PlanValue) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go index 3e2c3b39844..b8e5a450a5d 100644 --- a/go/tools/sizegen/sizegen.go +++ b/go/tools/sizegen/sizegen.go @@ -20,7 +20,9 @@ import ( "flag" "fmt" "go/types" + "io" "log" + "os" "path" "sort" "strings" @@ -29,7 +31,7 @@ import ( "golang.org/x/tools/go/packages" ) -const FileHeader = `Copyright 2021 The Vitess Authors. +const licenseFileHeader = `Copyright 2021 The Vitess Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -43,37 +45,50 @@ 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.` -type Sizegen struct { +type sizegen struct { DebugTypes bool mod *packages.Module sizes types.Sizes - codegen map[string]*File - known map[*types.Named]*TypeState + codegen map[string]*codeFile + known map[*types.Named]*typeState } -type Impl struct { - name string - code jen.Code +type generatedCode struct { + mod *packages.Module + files map[string]*codeFile } -type File struct { +type codeFlag uint32 + +const ( + codeWithInterface = 1 << 0 + codeWithUnsafe = 1 << 1 +) + +type codeImpl struct { + name string + flags codeFlag + code jen.Code +} + +type codeFile struct { pkg string - impls []Impl + impls []codeImpl } -type TypeState struct { +type typeState struct { generated bool local bool - pod bool + pod bool // struct with only primitives } -func NewSizegen(mod *packages.Module, sizes types.Sizes) *Sizegen { - return &Sizegen{ +func newSizegen(mod *packages.Module, sizes types.Sizes) *sizegen { + return &sizegen{ DebugTypes: true, mod: mod, sizes: sizes, - known: make(map[*types.Named]*TypeState), - codegen: make(map[string]*File), + known: make(map[*types.Named]*typeState), + codegen: make(map[string]*codeFile), } } @@ -99,11 +114,11 @@ func isPod(tt types.Type) bool { } } -func (sizegen *Sizegen) getKnownType(named *types.Named) *TypeState { +func (sizegen *sizegen) getKnownType(named *types.Named) *typeState { ts := sizegen.known[named] if ts == nil { local := strings.HasPrefix(named.Obj().Pkg().Path(), sizegen.mod.Path) - ts = &TypeState{ + ts = &typeState{ local: local, pod: isPod(named.Underlying()), } @@ -112,7 +127,7 @@ func (sizegen *Sizegen) getKnownType(named *types.Named) *TypeState { return ts } -func (sizegen *Sizegen) generateType(pkg *types.Package, file *File, named *types.Named) { +func (sizegen *sizegen) generateType(pkg *types.Package, file *codeFile, named *types.Named) { ts := sizegen.getKnownType(named) if ts.generated { return @@ -121,10 +136,11 @@ func (sizegen *Sizegen) generateType(pkg *types.Package, file *File, named *type switch tt := named.Underlying().(type) { case *types.Struct: - if impl := sizegen.sizeImplForStruct(named.Obj(), tt); impl != nil { - file.impls = append(file.impls, Impl{ - code: impl, - name: named.String(), + if impl, flag := sizegen.sizeImplForStruct(named.Obj(), tt); impl != nil { + file.impls = append(file.impls, codeImpl{ + code: impl, + name: named.String(), + flags: flag, }) } case *types.Interface: @@ -138,11 +154,11 @@ func (sizegen *Sizegen) generateType(pkg *types.Package, file *File, named *type } } -func (sizegen *Sizegen) GenerateKnownType(named *types.Named) { +func (sizegen *sizegen) generateKnownType(named *types.Named) { pkgInfo := named.Obj().Pkg() file := sizegen.codegen[pkgInfo.Path()] if file == nil { - file = &File{pkg: pkgInfo.Name()} + file = &codeFile{pkg: pkgInfo.Name()} sizegen.codegen[pkgInfo.Path()] = file } @@ -159,77 +175,131 @@ func findImplementations(scope *types.Scope, iff *types.Interface, impl func(typ } } -func (sizegen *Sizegen) GenerateForInterface(pkg *types.Package, iff *types.Interface) { +func (sizegen *sizegen) generateKnownInterface(pkg *types.Package, iff *types.Interface) { findImplementations(pkg.Scope(), iff, func(tt types.Type) { if named, ok := tt.(*types.Named); ok { - sizegen.GenerateKnownType(named) + sizegen.generateKnownType(named) } }) } -func (sizegen *Sizegen) Finalize() { - var complete bool +func (sizegen *sizegen) finalize() { + code := sizegen.generateRemainingKnownTypes() + writeGeneratedCode(code, &realFS{}) +} - for !complete { - complete = true - for tt, ts := range sizegen.known { - if ts.local && !ts.pod && !ts.generated { - sizegen.GenerateKnownType(tt) - complete = false - } - } +type fileWriter interface { + forFile(fullpath string) (io.WriteCloser, error) +} + +type realFS struct{} + +func (*realFS) forFile(fullpath string) (io.WriteCloser, error) { + file, err := os.Create(fullpath) + if err != nil { + return nil, err } - for pkg, file := range sizegen.codegen { + return file, nil +} + +var _ fileWriter = (*realFS)(nil) + +func writeGeneratedCode(code *generatedCode, wr fileWriter) error { + for pkg, file := range code.files { if len(file.impls) == 0 { continue } - if !strings.HasPrefix(pkg, sizegen.mod.Path) { + if !strings.HasPrefix(pkg, code.mod.Path) { log.Printf("failed to generate code for foreign package '%s'", pkg) log.Printf("DEBUG:\n%#v", file) continue } - out := jen.NewFile(file.pkg) - out.HeaderComment(FileHeader) - out.HeaderComment("Code generated by Sizegen. DO NOT EDIT.") - out.Add(jen.Type().Id("cachedObject").InterfaceFunc(func(i *jen.Group) { - i.Id("CachedSize").Params(jen.Id("alloc").Id("bool")).Int64() - })) - sort.Slice(file.impls, func(i, j int) bool { return strings.Compare(file.impls[i].name, file.impls[j].name) < 0 }) + out := jen.NewFile(file.pkg) + out.HeaderComment(licenseFileHeader) + out.HeaderComment("Code generated by Sizegen. DO NOT EDIT.") + + for _, impl := range file.impls { + if impl.flags&codeWithInterface != 0 { + out.Add(jen.Type().Id("cachedObject").InterfaceFunc(func(i *jen.Group) { + i.Id("CachedSize").Params(jen.Id("alloc").Id("bool")).Int64() + })) + break + } + } + for _, impl := range file.impls { + if impl.flags&codeWithUnsafe != 0 { + out.Commentf("//go:nocheckptr") + } out.Add(impl.code) } - fullPath := path.Join(sizegen.mod.Dir, strings.TrimPrefix(pkg, sizegen.mod.Path), "cached_size.go") - if err := out.Save(fullPath); err != nil { - log.Printf("failed to save '%s': %v", fullPath, err) - continue + fullPath := path.Join(code.mod.Dir, strings.TrimPrefix(pkg, code.mod.Path), "cached_size.go") + writer, err := wr.forFile(fullPath) + if err != nil { + return err } + + if err := out.Render(writer); err != nil { + writer.Close() + return fmt.Errorf("failed to save '%s': %v", fullPath, err) + } + if err = writer.Close(); err != nil { + return err + } + log.Printf("saved %s at '%s'", pkg, fullPath) } + return nil } -func (sizegen *Sizegen) sizeImplForStruct(name *types.TypeName, st *types.Struct) jen.Code { +func (sizegen *sizegen) generateRemainingKnownTypes() *generatedCode { + var complete bool + + for !complete { + complete = true + for tt, ts := range sizegen.known { + isComplex := !ts.pod + notYetGenerated := !ts.generated + if ts.local && isComplex && notYetGenerated { + sizegen.generateKnownType(tt) + complete = false + } + } + } + + return &generatedCode{ + mod: sizegen.mod, + files: sizegen.codegen, + } +} + +func (sizegen *sizegen) sizeImplForStruct(name *types.TypeName, st *types.Struct) (jen.Code, codeFlag) { if sizegen.sizes.Sizeof(st) == 0 { - return nil + return nil, 0 } var stmt []jen.Code + var funcFlags codeFlag for i := 0; i < st.NumFields(); i++ { field := st.Field(i) fieldType := field.Type() fieldName := jen.Id("cached").Dot(field.Name()) - if s := sizegen.sizeStmtForType(fieldName, fieldType, false); s != nil { + + fieldStmt, flag := sizegen.sizeStmtForType(fieldName, fieldType, false) + if fieldStmt != nil { if sizegen.DebugTypes { stmt = append(stmt, jen.Commentf("%s", field.String())) } - stmt = append(stmt, s) + stmt = append(stmt, fieldStmt) } + funcFlags |= flag } f := jen.Func() @@ -246,10 +316,10 @@ func (sizegen *Sizegen) sizeImplForStruct(name *types.TypeName, st *types.Struct } b.Add(jen.Return(jen.Id("size"))) }) - return f + return f, funcFlags } -func (sizegen *Sizegen) sizeStmtForMap(fieldName *jen.Statement, m *types.Map) []jen.Code { +func (sizegen *sizegen) sizeStmtForMap(fieldName *jen.Statement, m *types.Map) []jen.Code { const bucketCnt = 8 const sizeofHmap = int64(6 * 8) @@ -296,9 +366,9 @@ func (sizegen *Sizegen) sizeStmtForMap(fieldName *jen.Statement, m *types.Map) [ } } -func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Type, alloc bool) jen.Code { +func (sizegen *sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Type, alloc bool) (jen.Code, codeFlag) { if sizegen.sizes.Sizeof(field) == 0 { - return nil + return nil, 0 } switch node := field.(type) { @@ -308,12 +378,13 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty switch elemSize { case 0: - return nil + return nil, 0 case 1: - return jen.Id("size").Op("+=").Int64().Call(jen.Cap(fieldName)) + return jen.Id("size").Op("+=").Int64().Call(jen.Cap(fieldName)), 0 default: + stmt, flag := sizegen.sizeStmtForType(jen.Id("elem"), elemT, false) return jen.BlockFunc(func(b *jen.Group) { b.Add( jen.Id("size"). @@ -322,22 +393,22 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty Op("*"). Lit(sizegen.sizes.Sizeof(elemT))) - if stmt := sizegen.sizeStmtForType(jen.Id("elem"), elemT, false); stmt != nil { - b.Add(jen.For(jen.List(jen.Id("_"), jen.Id("elem")).Op(":=").Range().Add(fieldName))).Block(stmt) + if stmt != nil { + b.Add(jen.For(jen.List(jen.Id("_"), jen.Id("elem")).Op(":=").Range().Add(fieldName)).Block(stmt)) } - }) + }), flag } case *types.Map: - return jen.If(fieldName.Clone().Op("!=").Nil()).BlockFunc(func(b *jen.Group) { + keySize, keyFlag := sizegen.sizeStmtForType(jen.Id("k"), node.Key(), false) + valSize, valFlag := sizegen.sizeStmtForType(jen.Id("v"), node.Elem(), false) + + return jen.If(fieldName.Clone().Op("!=").Nil()).BlockFunc(func(block *jen.Group) { for _, stmt := range sizegen.sizeStmtForMap(fieldName, node) { - b.Add(stmt) + block.Add(stmt) } var forLoopVars []jen.Code - keySize := sizegen.sizeStmtForType(jen.Id("k"), node.Key(), false) - valSize := sizegen.sizeStmtForType(jen.Id("v"), node.Elem(), false) - switch { case keySize != nil && valSize != nil: forLoopVars = []jen.Code{jen.Id("k"), jen.Id("v")} @@ -349,7 +420,7 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty return } - b.Add(jen.For(jen.List(forLoopVars...).Op(":=").Range().Add(fieldName))).BlockFunc(func(b *jen.Group) { + block.Add(jen.For(jen.List(forLoopVars...).Op(":=").Range().Add(fieldName))).BlockFunc(func(b *jen.Group) { if keySize != nil { b.Add(keySize) } @@ -357,7 +428,7 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty b.Add(valSize) } }) - }) + }), codeWithUnsafe | keyFlag | valFlag case *types.Pointer: return sizegen.sizeStmtForType(fieldName, node.Elem(), true) @@ -371,15 +442,15 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty } return jen.If(fieldName.Clone().Op("!=").Nil()).Block( jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(node.Underlying())), - ) + ), 0 } - return nil + return nil, 0 } return sizegen.sizeStmtForType(fieldName, node.Underlying(), alloc) case *types.Interface: if node.Empty() { - return nil + return nil, 0 } return jen.If( jen.List( @@ -393,24 +464,23 @@ func (sizegen *Sizegen) sizeStmtForType(fieldName *jen.Statement, field types.Ty Id("cc"). Dot("CachedSize"). Call(jen.True()), - ) + ), codeWithInterface case *types.Struct: - return jen.Id("size").Op("+=").Add(fieldName.Clone().Dot("CachedSize").Call(jen.Lit(alloc))) + return jen.Id("size").Op("+=").Add(fieldName.Clone().Dot("CachedSize").Call(jen.Lit(alloc))), 0 case *types.Basic: if !alloc { if node.Info()&types.IsString != 0 { - return jen.Id("size").Op("+=").Int64().Call(jen.Len(fieldName)) + return jen.Id("size").Op("+=").Int64().Call(jen.Len(fieldName)), 0 } - return nil + return nil, 0 } - return jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(node)) + return jen.Id("size").Op("+=").Lit(sizegen.sizes.Sizeof(node)), 0 default: log.Printf("unhandled type: %T", node) + return nil, 0 } - - return nil } type typePaths []string @@ -440,7 +510,16 @@ func main() { log.Fatal(err) } - sizegen := NewSizegen(loaded[0].Module, loaded[0].TypesSizes) + sizegen, err := generateCode(loaded, generate) + if err != nil { + log.Fatal(err) + } + + sizegen.finalize() +} + +func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, error) { + sizegen := newSizegen(loaded[0].Module, loaded[0].TypesSizes) scopes := make(map[string]*types.Scope) for _, pkg := range loaded { @@ -450,7 +529,7 @@ func main() { for _, gen := range generate { pos := strings.LastIndexByte(gen, '.') if pos < 0 { - log.Fatalf("unexpected input type: %s", gen) + return nil, fmt.Errorf("unexpected input type: %s", gen) } pkgname := gen[:pos] @@ -458,16 +537,16 @@ func main() { scope := scopes[pkgname] if scope == nil { - log.Fatalf("no scope found for type '%s'", gen) + return nil, fmt.Errorf("no scope found for type '%s'", gen) } tt := scope.Lookup(typename) if tt == nil { - log.Fatalf("no type called '%s' found in '%s'", typename, pkgname) + return nil, fmt.Errorf("no type called '%s' found in '%s'", typename, pkgname) } - sizegen.GenerateKnownType(tt.Type().(*types.Named)) + sizegen.generateKnownType(tt.Type().(*types.Named)) } - sizegen.Finalize() + return sizegen, nil } diff --git a/go/tools/sizegen/sizegen_test.go b/go/tools/sizegen/sizegen_test.go new file mode 100644 index 00000000000..3cfcc803c7b --- /dev/null +++ b/go/tools/sizegen/sizegen_test.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/stretchr/testify/require" + + "golang.org/x/tools/go/packages" +) + +func createFile(dir, fileName, code string) error { + s := path.Join(dir, "a.go") + return ioutil.WriteFile(s, []byte(code), os.ModePerm) +} + +func TestName(t *testing.T) { + dir, err := ioutil.TempDir("", "src") + require.NoError(t, err) + command := exec.Command("go", "mod", "init", "example.com/m") + command.Dir = dir + command.Stdout = os.Stdout + command.Stderr = os.Stderr + err = command.Run() + require.NoError(t, err) + + code := ` +package code + +type A struct { +} +` + + err = createFile(dir, "a.go", code) + require.NoError(t, err) + + config := &packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule, + Logf: log.Printf, + Dir: dir, + } + join := path.Join(dir, "...") + initial, err := packages.Load(config, join) + require.NoError(t, err) + + pkg := initial[0] + require.Empty(t, pkg.Errors) + assert.NotNil(t, pkg.Module) + + generator, err := generateCode(initial, []string{"example.com/m.A"}) + require.NoError(t, err) + types := generator.generateRemainingKnownTypes() + fmt.Println(types) +} diff --git a/go/vt/key/cached_size.go b/go/vt/key/cached_size.go index 6307a708071..ef4e6e79896 100644 --- a/go/vt/key/cached_size.go +++ b/go/vt/key/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package key -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *DestinationExactKeyRange) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/proto/query/cached_size.go b/go/vt/proto/query/cached_size.go index 982298af411..d4a138a7f8f 100644 --- a/go/vt/proto/query/cached_size.go +++ b/go/vt/proto/query/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package query -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *BindVariable) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/proto/topodata/cached_size.go b/go/vt/proto/topodata/cached_size.go index 877d02e475b..a473c6f8a4b 100644 --- a/go/vt/proto/topodata/cached_size.go +++ b/go/vt/proto/topodata/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package topodata -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *KeyRange) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/vtgate/engine/cached_size.go b/go/vt/vtgate/engine/cached_size.go index 90b5798255e..136ce2b84f1 100644 --- a/go/vt/vtgate/engine/cached_size.go +++ b/go/vt/vtgate/engine/cached_size.go @@ -209,6 +209,8 @@ func (cached *Insert) CachedSize(alloc bool) int64 { size += int64(len(cached.Suffix)) return size } + +//go:nocheckptr func (cached *Join) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -662,6 +664,8 @@ func (cached *SysVarSetAware) CachedSize(alloc bool) int64 { } return size } + +//go:nocheckptr func (cached *Update) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -744,6 +748,8 @@ func (cached *VindexFunc) CachedSize(alloc bool) int64 { size += cached.Value.CachedSize(false) return size } + +//go:nocheckptr func (cached *VindexValues) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -769,6 +775,8 @@ func (cached *VindexValues) CachedSize(alloc bool) int64 { } return size } + +//go:nocheckptr func (cached *shardRoute) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/vtgate/vindexes/cached_size.go b/go/vt/vtgate/vindexes/cached_size.go index 4310740baea..70c7fd0ede6 100644 --- a/go/vt/vtgate/vindexes/cached_size.go +++ b/go/vt/vtgate/vindexes/cached_size.go @@ -258,6 +258,8 @@ func (cached *Numeric) CachedSize(alloc bool) int64 { size += int64(len(cached.name)) return size } + +//go:nocheckptr func (cached *NumericStaticMap) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) @@ -293,6 +295,8 @@ func (cached *RegionExperimental) CachedSize(alloc bool) int64 { size += int64(len(cached.name)) return size } + +//go:nocheckptr func (cached *RegionJSON) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/vttablet/tabletserver/cached_size.go b/go/vt/vttablet/tabletserver/cached_size.go index 7cd0a08b662..eadc6dee317 100644 --- a/go/vt/vttablet/tabletserver/cached_size.go +++ b/go/vt/vttablet/tabletserver/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package tabletserver -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *TabletPlan) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/vttablet/tabletserver/planbuilder/cached_size.go b/go/vt/vttablet/tabletserver/planbuilder/cached_size.go index 37abf2ff5fb..549f6a07962 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/cached_size.go +++ b/go/vt/vttablet/tabletserver/planbuilder/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package planbuilder -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *Permission) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/vttablet/tabletserver/schema/cached_size.go b/go/vt/vttablet/tabletserver/schema/cached_size.go index 5f7941c071c..54e55bef9a5 100644 --- a/go/vt/vttablet/tabletserver/schema/cached_size.go +++ b/go/vt/vttablet/tabletserver/schema/cached_size.go @@ -17,10 +17,6 @@ limitations under the License. package schema -type cachedObject interface { - CachedSize(alloc bool) int64 -} - func (cached *MessageInfo) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) From f362f446f397bc0e034c8778832752ab1cd88c6d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 28 Jan 2021 15:54:07 +0100 Subject: [PATCH 4/6] sizegen: implement a 'verify' flag Signed-off-by: Vicent Marti --- Makefile | 10 ++- go/tools/sizegen/sizegen.go | 123 +++++++++++++++---------------- go/tools/sizegen/sizegen_test.go | 21 ++++-- 3 files changed, 80 insertions(+), 74 deletions(-) diff --git a/Makefile b/Makefile index 00ec07a8cc2..f50ad626c6d 100644 --- a/Makefile +++ b/Makefile @@ -105,6 +105,12 @@ parser: visitor: go generate go/vt/sqlparser/rewriter.go +sizegen: + go run go/tools/sizegen/sizegen.go \ + -in ./go/vt/... \ + -gen vitess.io/vitess/go/vt/vtgate/engine.Plan \ + -gen vitess.io/vitess/go/vt/vttablet/tabletserver.TabletPlan + # To pass extra flags, run test.go manually. # For example: go run test.go -docker=false -- --extra-flag # For more info see: go run test.go -help @@ -306,7 +312,7 @@ web_bootstrap: ./tools/web_bootstrap.sh # Do a production build of the vtctld UI. -# This target needs to be manually run every time any file within web/vtctld2/app +# This target needs to be manually run every time any file within web/vtctld2/app # is modified to regenerate rice-box.go web_build: web_bootstrap ./tools/web_build.sh @@ -317,7 +323,7 @@ web_build: web_bootstrap web_start: web_bootstrap cd web/vtctld2 && npm run start -vtadmin_web_install: +vtadmin_web_install: cd web/vtadmin && npm install # Generate JavaScript/TypeScript bindings for vtadmin-web from the Vitess .proto files. diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go index b8e5a450a5d..90a5960f0f0 100644 --- a/go/tools/sizegen/sizegen.go +++ b/go/tools/sizegen/sizegen.go @@ -17,12 +17,12 @@ limitations under the License. package main import ( + "bytes" "flag" "fmt" "go/types" - "io" + "io/ioutil" "log" - "os" "path" "sort" "strings" @@ -53,11 +53,6 @@ type sizegen struct { known map[*types.Named]*typeState } -type generatedCode struct { - mod *packages.Module - files map[string]*codeFile -} - type codeFlag uint32 const ( @@ -183,34 +178,28 @@ func (sizegen *sizegen) generateKnownInterface(pkg *types.Package, iff *types.In }) } -func (sizegen *sizegen) finalize() { - code := sizegen.generateRemainingKnownTypes() - writeGeneratedCode(code, &realFS{}) -} - -type fileWriter interface { - forFile(fullpath string) (io.WriteCloser, error) -} - -type realFS struct{} +func (sizegen *sizegen) finalize() map[string]*jen.File { + var complete bool -func (*realFS) forFile(fullpath string) (io.WriteCloser, error) { - file, err := os.Create(fullpath) - if err != nil { - return nil, err + for !complete { + complete = true + for tt, ts := range sizegen.known { + isComplex := !ts.pod + notYetGenerated := !ts.generated + if ts.local && isComplex && notYetGenerated { + sizegen.generateKnownType(tt) + complete = false + } + } } - return file, nil -} - -var _ fileWriter = (*realFS)(nil) + outputFiles := make(map[string]*jen.File) -func writeGeneratedCode(code *generatedCode, wr fileWriter) error { - for pkg, file := range code.files { + for pkg, file := range sizegen.codegen { if len(file.impls) == 0 { continue } - if !strings.HasPrefix(pkg, code.mod.Path) { + if !strings.HasPrefix(pkg, sizegen.mod.Path) { log.Printf("failed to generate code for foreign package '%s'", pkg) log.Printf("DEBUG:\n%#v", file) continue @@ -240,44 +229,11 @@ func writeGeneratedCode(code *generatedCode, wr fileWriter) error { out.Add(impl.code) } - fullPath := path.Join(code.mod.Dir, strings.TrimPrefix(pkg, code.mod.Path), "cached_size.go") - writer, err := wr.forFile(fullPath) - if err != nil { - return err - } - - if err := out.Render(writer); err != nil { - writer.Close() - return fmt.Errorf("failed to save '%s': %v", fullPath, err) - } - if err = writer.Close(); err != nil { - return err - } - - log.Printf("saved %s at '%s'", pkg, fullPath) + fullPath := path.Join(sizegen.mod.Dir, strings.TrimPrefix(pkg, sizegen.mod.Path), "cached_size.go") + outputFiles[fullPath] = out } - return nil -} - -func (sizegen *sizegen) generateRemainingKnownTypes() *generatedCode { - var complete bool - for !complete { - complete = true - for tt, ts := range sizegen.known { - isComplex := !ts.pod - notYetGenerated := !ts.generated - if ts.local && isComplex && notYetGenerated { - sizegen.generateKnownType(tt) - complete = false - } - } - } - - return &generatedCode{ - mod: sizegen.mod, - files: sizegen.codegen, - } + return outputFiles } func (sizegen *sizegen) sizeImplForStruct(name *types.TypeName, st *types.Struct) (jen.Code, codeFlag) { @@ -497,8 +453,11 @@ func (t *typePaths) Set(path string) error { func main() { var patterns typePaths var generate typePaths + var verify bool + flag.Var(&patterns, "in", "Go packages to load the generator") flag.Var(&generate, "gen", "Typename of the Go struct to generate size info for") + flag.BoolVar(&verify, "verify", false, "ensure that the generated files are correct") flag.Parse() loaded, err := packages.Load(&packages.Config{ @@ -515,7 +474,41 @@ func main() { log.Fatal(err) } - sizegen.finalize() + result := sizegen.finalize() + + if verify { + verifyFilesOnDisk(result) + } else { + saveFilesToDisk(result) + } +} + +func saveFilesToDisk(result map[string]*jen.File) { + for fullPath, file := range result { + if err := file.Save(fullPath); err != nil { + log.Fatalf("filed to save file to '%s': %v", fullPath, err) + } + log.Printf("saved '%s'", fullPath) + } +} + +func verifyFilesOnDisk(result map[string]*jen.File) { + for fullPath, file := range result { + existing, err := ioutil.ReadFile(fullPath) + if err != nil { + log.Fatalf("missing file on disk: %s (%v)", fullPath, err) + } + + var buf bytes.Buffer + if err := file.Render(&buf); err != nil { + log.Fatalf("render error for '%s': %v", fullPath, err) + } + + if !bytes.Equal(existing, buf.Bytes()) { + log.Fatalf("'%s' has changed!", fullPath) + } + } + log.Printf("%d files OK", len(result)) } func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, error) { diff --git a/go/tools/sizegen/sizegen_test.go b/go/tools/sizegen/sizegen_test.go index 3cfcc803c7b..784a585f2f4 100644 --- a/go/tools/sizegen/sizegen_test.go +++ b/go/tools/sizegen/sizegen_test.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io/ioutil" "log" "os" @@ -10,14 +9,12 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "golang.org/x/tools/go/packages" ) func createFile(dir, fileName, code string) error { - s := path.Join(dir, "a.go") + s := path.Join(dir, fileName) return ioutil.WriteFile(s, []byte(code), os.ModePerm) } @@ -35,7 +32,15 @@ func TestName(t *testing.T) { package code type A struct { + str string + field uint64 +} + +type B struct { + field1 uint64 + field2 *A } + ` err = createFile(dir, "a.go", code) @@ -54,8 +59,10 @@ type A struct { require.Empty(t, pkg.Errors) assert.NotNil(t, pkg.Module) - generator, err := generateCode(initial, []string{"example.com/m.A"}) + generator, err := generateCode(initial, []string{"example.com/m.A", "example.com/m.B"}) require.NoError(t, err) - types := generator.generateRemainingKnownTypes() - fmt.Println(types) + + for _, file := range generator.finalize() { + t.Logf("%#v", file) + } } From 39a67915b8fedffd4a7ffde075741c71b253f3f3 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 28 Jan 2021 18:16:08 +0100 Subject: [PATCH 5/6] sizegen: implement unit tests Signed-off-by: Vicent Marti --- go/tools/sizegen/integration/cached_size.go | 225 ++++++++++++++++++ .../sizegen/integration/integration_test.go | 85 +++++++ go/tools/sizegen/integration/types.go | 76 ++++++ go/tools/sizegen/sizegen.go | 16 +- go/tools/sizegen/sizegen_test.go | 16 ++ 5 files changed, 413 insertions(+), 5 deletions(-) create mode 100644 go/tools/sizegen/integration/cached_size.go create mode 100644 go/tools/sizegen/integration/integration_test.go create mode 100644 go/tools/sizegen/integration/types.go diff --git a/go/tools/sizegen/integration/cached_size.go b/go/tools/sizegen/integration/cached_size.go new file mode 100644 index 00000000000..7ceba285138 --- /dev/null +++ b/go/tools/sizegen/integration/cached_size.go @@ -0,0 +1,225 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package integration + +import ( + "math" + "reflect" + "unsafe" +) + +type cachedObject interface { + CachedSize(alloc bool) int64 +} + +func (cached *A) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + return size +} +func (cached *Bimpl) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + return size +} +func (cached *C) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(16) + } + // field field1 vitess.io/vitess/go/tools/sizegen/integration.B + if cc, ok := cached.field1.(cachedObject); ok { + size += cc.CachedSize(true) + } + return size +} +func (cached *D) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field field1 *vitess.io/vitess/go/tools/sizegen/integration.Bimpl + if cached.field1 != nil { + size += int64(8) + } + return size +} + +//go:nocheckptr +func (cached *Map1) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field field1 map[uint8]uint8 + if cached.field1 != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.field1) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 32) + if len(cached.field1) > 0 || numBuckets > 1 { + size += int64(numBuckets * 32) + } + } + return size +} + +//go:nocheckptr +func (cached *Map2) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field field1 map[uint64]vitess.io/vitess/go/tools/sizegen/integration.A + if cached.field1 != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.field1) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.field1) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + } + return size +} + +//go:nocheckptr +func (cached *Map3) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(8) + } + // field field1 map[uint64]vitess.io/vitess/go/tools/sizegen/integration.B + if cached.field1 != nil { + size += int64(48) + hmap := reflect.ValueOf(cached.field1) + numBuckets := int(math.Pow(2, float64((*(*uint8)(unsafe.Pointer(hmap.Pointer() + uintptr(9))))))) + numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10)))) + size += int64(numOldBuckets * 208) + if len(cached.field1) > 0 || numBuckets > 1 { + size += int64(numBuckets * 208) + } + for _, v := range cached.field1 { + if cc, ok := v.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + return size +} +func (cached *Padded) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + return size +} +func (cached *Slice1) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field field1 []vitess.io/vitess/go/tools/sizegen/integration.A + { + size += int64(cap(cached.field1)) * int64(16) + } + return size +} +func (cached *Slice2) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field field1 []vitess.io/vitess/go/tools/sizegen/integration.B + { + size += int64(cap(cached.field1)) * int64(16) + for _, elem := range cached.field1 { + if cc, ok := elem.(cachedObject); ok { + size += cc.CachedSize(true) + } + } + } + return size +} +func (cached *Slice3) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field field1 []*vitess.io/vitess/go/tools/sizegen/integration.Bimpl + { + size += int64(cap(cached.field1)) * int64(8) + for _, elem := range cached.field1 { + if elem != nil { + size += int64(8) + } + } + } + return size +} +func (cached *String1) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(24) + } + // field field1 string + size += int64(len(cached.field1)) + return size +} diff --git a/go/tools/sizegen/integration/integration_test.go b/go/tools/sizegen/integration/integration_test.go new file mode 100644 index 00000000000..d2c22a2cbcd --- /dev/null +++ b/go/tools/sizegen/integration/integration_test.go @@ -0,0 +1,85 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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 integration + +import ( + "fmt" + "testing" +) + +func TestTypeSizes(t *testing.T) { + const PtrSize = 8 + const SliceHeaderSize = 3 * PtrSize + const FatPointerSize = 2 * PtrSize + const BucketHeaderSize = 8 + const BucketSize = 8 + const HashMapHeaderSize = 48 + + cases := []struct { + obj cachedObject + size int64 + }{ + {&A{}, 16}, + {&C{}, 16}, + {&C{field1: &Bimpl{}}, 24}, + {&D{}, 8}, + {&D{field1: &Bimpl{}}, 16}, + {&Padded{}, 24}, + + {&Slice1{}, 24}, + {&Slice1{field1: []A{}}, SliceHeaderSize}, + {&Slice1{field1: []A{{}}}, SliceHeaderSize + 16}, + {&Slice1{field1: []A{{}, {}, {}, {}}}, SliceHeaderSize + 16*4}, + + {&Slice2{}, SliceHeaderSize}, + {&Slice2{field1: []B{}}, SliceHeaderSize}, + {&Slice2{field1: []B{&Bimpl{}}}, SliceHeaderSize + FatPointerSize*1 + 8*1}, + {&Slice2{field1: []B{&Bimpl{}, &Bimpl{}, &Bimpl{}, &Bimpl{}}}, SliceHeaderSize + FatPointerSize*4 + 8*4}, + + {&Slice3{}, SliceHeaderSize}, + {&Slice3{field1: []*Bimpl{}}, SliceHeaderSize}, + {&Slice3{field1: []*Bimpl{nil}}, SliceHeaderSize + PtrSize*1 + 0}, + {&Slice3{field1: []*Bimpl{nil, nil, nil, nil}}, SliceHeaderSize + PtrSize*4 + 0}, + {&Slice3{field1: []*Bimpl{{}}}, SliceHeaderSize + PtrSize*1 + 8*1}, + {&Slice3{field1: []*Bimpl{{}, {}, {}, {}}}, SliceHeaderSize + PtrSize*4 + 8*4}, + + {&Map1{field1: nil}, PtrSize}, + {&Map1{field1: map[uint8]uint8{}}, PtrSize + HashMapHeaderSize}, + {&Map1{field1: map[uint8]uint8{0: 0}}, PtrSize + HashMapHeaderSize + BucketHeaderSize + 1*BucketSize + 1*BucketSize + PtrSize}, + + {&Map2{field1: nil}, PtrSize}, + {&Map2{field1: map[uint64]A{}}, PtrSize + HashMapHeaderSize}, + {&Map2{field1: map[uint64]A{0: {}}}, PtrSize + HashMapHeaderSize + BucketHeaderSize + 8*BucketSize + 16*BucketSize + PtrSize}, + + {&Map3{field1: nil}, PtrSize}, + {&Map3{field1: map[uint64]B{}}, PtrSize + HashMapHeaderSize}, + {&Map3{field1: map[uint64]B{0: &Bimpl{}}}, PtrSize + HashMapHeaderSize + BucketHeaderSize + 8*BucketSize + FatPointerSize*BucketSize + PtrSize + 8}, + {&Map3{field1: map[uint64]B{0: nil}}, PtrSize + HashMapHeaderSize + BucketHeaderSize + 8*BucketSize + FatPointerSize*BucketSize + PtrSize}, + + {&String1{}, PtrSize*2 + 8}, + {&String1{field1: "1234"}, PtrSize*2 + 8 + 4}, + } + + for _, tt := range cases { + t.Run(fmt.Sprintf("sizeof(%T)", tt.obj), func(t *testing.T) { + size := tt.obj.CachedSize(true) + if size != tt.size { + t.Errorf("expected %T to be %d bytes, got %d", tt.obj, tt.size, size) + } + }) + } +} diff --git a/go/tools/sizegen/integration/types.go b/go/tools/sizegen/integration/types.go new file mode 100644 index 00000000000..c05b08cfd07 --- /dev/null +++ b/go/tools/sizegen/integration/types.go @@ -0,0 +1,76 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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. +*/ + +//nolint +package integration + +type A struct { + field1 uint64 + field2 uint64 +} + +type B interface { + iface() +} + +type Bimpl struct { + field1 uint64 +} + +func (b *Bimpl) iface() {} + +type C struct { + field1 B +} + +type D struct { + field1 *Bimpl +} + +type Padded struct { + field1 uint64 + field2 uint8 + field3 uint64 +} + +type Slice1 struct { + field1 []A +} + +type Slice2 struct { + field1 []B +} + +type Slice3 struct { + field1 []*Bimpl +} + +type Map1 struct { + field1 map[uint8]uint8 +} + +type Map2 struct { + field1 map[uint64]A +} + +type Map3 struct { + field1 map[uint64]B +} + +type String1 struct { + field1 string + field2 uint64 +} diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go index 90a5960f0f0..4de3862f96d 100644 --- a/go/tools/sizegen/sizegen.go +++ b/go/tools/sizegen/sizegen.go @@ -533,12 +533,18 @@ func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, err return nil, fmt.Errorf("no scope found for type '%s'", gen) } - tt := scope.Lookup(typename) - if tt == nil { - return nil, fmt.Errorf("no type called '%s' found in '%s'", typename, pkgname) - } + if typename == "*" { + for _, name := range scope.Names() { + sizegen.generateKnownType(scope.Lookup(name).Type().(*types.Named)) + } + } else { + tt := scope.Lookup(typename) + if tt == nil { + return nil, fmt.Errorf("no type called '%s' found in '%s'", typename, pkgname) + } - sizegen.generateKnownType(tt.Type().(*types.Named)) + sizegen.generateKnownType(tt.Type().(*types.Named)) + } } return sizegen, nil diff --git a/go/tools/sizegen/sizegen_test.go b/go/tools/sizegen/sizegen_test.go index 784a585f2f4..4eb523aa383 100644 --- a/go/tools/sizegen/sizegen_test.go +++ b/go/tools/sizegen/sizegen_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed 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 main import ( From 048dc8154f6262d68d792db79f2d29921825b19f Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Fri, 29 Jan 2021 13:23:46 +0100 Subject: [PATCH 6/6] sizegen: more interesting integration tests Signed-off-by: Vicent Marti --- go/tools/sizegen/sizegen.go | 64 ++++++++++++++++--------------- go/tools/sizegen/sizegen_test.go | 65 +++++--------------------------- 2 files changed, 44 insertions(+), 85 deletions(-) diff --git a/go/tools/sizegen/sizegen.go b/go/tools/sizegen/sizegen.go index 4de3862f96d..6281cd1485e 100644 --- a/go/tools/sizegen/sizegen.go +++ b/go/tools/sizegen/sizegen.go @@ -460,58 +460,62 @@ func main() { flag.BoolVar(&verify, "verify", false, "ensure that the generated files are correct") flag.Parse() - loaded, err := packages.Load(&packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule, - Logf: log.Printf, - }, patterns...) - + result, err := GenerateSizeHelpers(patterns, generate) if err != nil { log.Fatal(err) } - sizegen, err := generateCode(loaded, generate) - if err != nil { - log.Fatal(err) - } - - result := sizegen.finalize() - if verify { - verifyFilesOnDisk(result) + for _, err := range VerifyFilesOnDisk(result) { + log.Fatal(err) + } + log.Printf("%d files OK", len(result)) } else { - saveFilesToDisk(result) - } -} - -func saveFilesToDisk(result map[string]*jen.File) { - for fullPath, file := range result { - if err := file.Save(fullPath); err != nil { - log.Fatalf("filed to save file to '%s': %v", fullPath, err) + for fullPath, file := range result { + if err := file.Save(fullPath); err != nil { + log.Fatalf("filed to save file to '%s': %v", fullPath, err) + } + log.Printf("saved '%s'", fullPath) } - log.Printf("saved '%s'", fullPath) } } -func verifyFilesOnDisk(result map[string]*jen.File) { +// VerifyFilesOnDisk compares the generated results from the codegen against the files that +// currently exist on disk and returns any mismatches +func VerifyFilesOnDisk(result map[string]*jen.File) (errors []error) { for fullPath, file := range result { existing, err := ioutil.ReadFile(fullPath) if err != nil { - log.Fatalf("missing file on disk: %s (%v)", fullPath, err) + errors = append(errors, fmt.Errorf("missing file on disk: %s (%w)", fullPath, err)) + continue } var buf bytes.Buffer if err := file.Render(&buf); err != nil { - log.Fatalf("render error for '%s': %v", fullPath, err) + errors = append(errors, fmt.Errorf("render error for '%s': %w", fullPath, err)) + continue } if !bytes.Equal(existing, buf.Bytes()) { - log.Fatalf("'%s' has changed!", fullPath) + errors = append(errors, fmt.Errorf("'%s' has changed", fullPath)) + continue } } - log.Printf("%d files OK", len(result)) + return errors } -func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, error) { +// GenerateSizeHelpers generates the auxiliary code that implements CachedSize helper methods +// for all the types listed in typePatterns +func GenerateSizeHelpers(packagePatterns []string, typePatterns []string) (map[string]*jen.File, error) { + loaded, err := packages.Load(&packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule, + Logf: log.Printf, + }, packagePatterns...) + + if err != nil { + return nil, err + } + sizegen := newSizegen(loaded[0].Module, loaded[0].TypesSizes) scopes := make(map[string]*types.Scope) @@ -519,7 +523,7 @@ func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, err scopes[pkg.PkgPath] = pkg.Types.Scope() } - for _, gen := range generate { + for _, gen := range typePatterns { pos := strings.LastIndexByte(gen, '.') if pos < 0 { return nil, fmt.Errorf("unexpected input type: %s", gen) @@ -547,5 +551,5 @@ func generateCode(loaded []*packages.Package, generate typePaths) (*sizegen, err } } - return sizegen, nil + return sizegen.finalize(), nil } diff --git a/go/tools/sizegen/sizegen_test.go b/go/tools/sizegen/sizegen_test.go index 4eb523aa383..7b549d727c4 100644 --- a/go/tools/sizegen/sizegen_test.go +++ b/go/tools/sizegen/sizegen_test.go @@ -17,68 +17,23 @@ limitations under the License. package main import ( - "io/ioutil" - "log" - "os" - "os/exec" - "path" + "fmt" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "golang.org/x/tools/go/packages" ) -func createFile(dir, fileName, code string) error { - s := path.Join(dir, fileName) - return ioutil.WriteFile(s, []byte(code), os.ModePerm) -} - -func TestName(t *testing.T) { - dir, err := ioutil.TempDir("", "src") - require.NoError(t, err) - command := exec.Command("go", "mod", "init", "example.com/m") - command.Dir = dir - command.Stdout = os.Stdout - command.Stderr = os.Stderr - err = command.Run() - require.NoError(t, err) - - code := ` -package code - -type A struct { - str string - field uint64 -} - -type B struct { - field1 uint64 - field2 *A -} - -` - - err = createFile(dir, "a.go", code) - require.NoError(t, err) - - config := &packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule, - Logf: log.Printf, - Dir: dir, - } - join := path.Join(dir, "...") - initial, err := packages.Load(config, join) +func TestFullGeneration(t *testing.T) { + result, err := GenerateSizeHelpers([]string{"./integration/..."}, []string{"vitess.io/vitess/go/tools/sizegen/integration.*"}) require.NoError(t, err) - pkg := initial[0] - require.Empty(t, pkg.Errors) - assert.NotNil(t, pkg.Module) - - generator, err := generateCode(initial, []string{"example.com/m.A", "example.com/m.B"}) - require.NoError(t, err) + verifyErrors := VerifyFilesOnDisk(result) + require.Empty(t, verifyErrors) - for _, file := range generator.finalize() { - t.Logf("%#v", file) + for _, file := range result { + contents := fmt.Sprintf("%#v", file) + require.Contains(t, contents, "http://www.apache.org/licenses/LICENSE-2.0") + require.Contains(t, contents, "type cachedObject interface") + require.Contains(t, contents, "//go:nocheckptr") } }