Skip to content

Commit

Permalink
Small fix to decrease memory allocation (#16791)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay authored Sep 17, 2024
1 parent b5eb928 commit f97ea77
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (d *Delete) GetOrdering(*plancontext.PlanningContext) []OrderBy {
return nil
}

func (d *Delete) TablesUsed() []string {
return SingleQualifiedIdentifier(d.Target.VTable.Keyspace, d.Target.VTable.Name)
func (d *Delete) TablesUsed(in []string) []string {
return append(in, QualifiedString(d.Target.VTable.Keyspace, d.Target.VTable.Name.String()))
}

func (d *Delete) ShortDescription() string {
Expand Down
14 changes: 8 additions & 6 deletions go/vt/vtgate/planbuilder/operators/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package operators

import (
"fmt"
"slices"
"sort"

"vitess.io/vitess/go/vt/sqlparser"
Expand Down Expand Up @@ -82,20 +83,21 @@ func TableID(op Operator) (result semantics.TableSet) {

// TableUser is used to signal that this operator directly interacts with one or more tables
type TableUser interface {
TablesUsed() []string
TablesUsed([]string) []string
}

func TablesUsed(op Operator) []string {
addString, collect := collectSortedUniqueStrings()
var in []string
_ = Visit(op, func(this Operator) error {
if tbl, ok := this.(TableUser); ok {
for _, u := range tbl.TablesUsed() {
addString(u)
}
in = tbl.TablesUsed(in)
}
return nil
})
return collect()

slices.Sort(in)
compacted := slices.Compact(in)
return compacted
}

func CostOf(op Operator) (cost int) {
Expand Down
10 changes: 7 additions & 3 deletions go/vt/vtgate/planbuilder/operators/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ func (i *Insert) Clone([]Operator) Operator {
}
}

func (i *Insert) TablesUsed() []string {
return SingleQualifiedIdentifier(i.VTable.Keyspace, i.VTable.Name)
func (i *Insert) TablesUsed(in []string) []string {
return append(in, i.tableTarget())
}

func (i *Insert) tableTarget() string {
return QualifiedString(i.VTable.Keyspace, i.VTable.Name.String())
}

func (i *Insert) Statement() sqlparser.Statement {
Expand Down Expand Up @@ -423,7 +427,7 @@ func insertSelectPlan(
// When the table you are streaming data from and table you are inserting from are same.
// Then due to locking of the index range on the table we might not be able to insert into the table.
// Therefore, instead of streaming, this flag will ensure the records are first read and then inserted.
insertTbl := insOp.TablesUsed()[0]
insertTbl := insOp.tableTarget()
selTables := TablesUsed(selOp)
for _, tbl := range selTables {
if insertTbl == tbl {
Expand Down
9 changes: 3 additions & 6 deletions go/vt/vtgate/planbuilder/operators/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,11 @@ func (r *Route) GetOrdering(ctx *plancontext.PlanningContext) []OrderBy {

// TablesUsed returns tables used by MergedWith routes, which are not included
// in Inputs() and thus not a part of the operator tree
func (r *Route) TablesUsed() []string {
addString, collect := collectSortedUniqueStrings()
func (r *Route) TablesUsed(in []string) []string {
for _, mw := range r.MergedWith {
for _, u := range TablesUsed(mw) {
addString(u)
}
in = append(in, TablesUsed(mw)...)
}
return collect()
return in
}

func isSpecialOrderBy(o OrderBy) bool {
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/planbuilder/operators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func (to *Table) AddCol(col *sqlparser.ColName) {
to.Columns = append(to.Columns, col)
}

func (to *Table) TablesUsed() []string {
func (to *Table) TablesUsed(in []string) []string {
if sqlparser.SystemSchema(to.QTable.Table.Qualifier.String()) {
return nil
return in
}
return SingleQualifiedIdentifier(to.VTable.Keyspace, to.VTable.Name)
return append(in, QualifiedString(to.VTable.Keyspace, to.VTable.Name.String()))
}

func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.Expr) int {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (u *Update) GetOrdering(*plancontext.PlanningContext) []OrderBy {
return nil
}

func (u *Update) TablesUsed() []string {
return SingleQualifiedIdentifier(u.Target.VTable.Keyspace, u.Target.VTable.Name)
func (u *Update) TablesUsed(in []string) []string {
return append(in, QualifiedString(u.Target.VTable.Keyspace, u.Target.VTable.Name.String()))
}

func (u *Update) ShortDescription() string {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/vindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func (v *Vindex) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.E

// TablesUsed implements the Operator interface.
// It is not keyspace-qualified.
func (v *Vindex) TablesUsed() []string {
return []string{v.Table.Table.Name.String()}
func (v *Vindex) TablesUsed(in []string) []string {
return append(in, v.Table.Table.Name.String())
}

func (v *Vindex) ShortDescription() string {
Expand Down

0 comments on commit f97ea77

Please sign in to comment.