From 7febcf64f3c0bdb9948aea22296c9ed041126324 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 25 Jan 2021 14:44:07 +0100 Subject: [PATCH] small optimisation that makes big difference Before optimisation: BenchmarkPlanner/large_cases.txt-v4-16 70786 167749 ns/op 74315 B/op 1618 allocs/op BenchmarkPlanner/large_cases.txt-v4-16 70998 168734 ns/op 74303 B/op 1618 allocs/op After: BenchmarkPlanner/large_cases.txt-v4-16 76375 156411 ns/op 67752 B/op 1376 allocs/op BenchmarkPlanner/large_cases.txt-v4-16 76207 157553 ns/op 67816 B/op 1376 allocs/op Signed-off-by: Andres Taylor --- go/vt/vtgate/planbuilder/querygraph_test.go | 1 - go/vt/vtgate/planbuilder/route_planning_test.go | 2 +- go/vt/vtgate/semantics/semantic_state.go | 12 +++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/go/vt/vtgate/planbuilder/querygraph_test.go b/go/vt/vtgate/planbuilder/querygraph_test.go index 71c8c06989f..43717a4a465 100644 --- a/go/vt/vtgate/planbuilder/querygraph_test.go +++ b/go/vt/vtgate/planbuilder/querygraph_test.go @@ -123,7 +123,6 @@ func TestQueryGraph(t *testing.T) { require.NoError(t, err) qgraph, err := createQGFromSelect(tree.(*sqlparser.Select), semTable) require.NoError(t, err) - fmt.Println(qgraph.testString()) assert.Equal(t, tc.output, qgraph.testString()) utils.MustMatch(t, tc.output, qgraph.testString(), "incorrect query graph") }) diff --git a/go/vt/vtgate/planbuilder/route_planning_test.go b/go/vt/vtgate/planbuilder/route_planning_test.go index 535aecd0f2a..4209225bd66 100644 --- a/go/vt/vtgate/planbuilder/route_planning_test.go +++ b/go/vt/vtgate/planbuilder/route_planning_test.go @@ -99,7 +99,7 @@ func TestMergeJoins(t *testing.T) { }} for i, tc := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - result := tryMerge(tc.l, tc.r, tc.predicates, &semantics.SemTable{}) + result := tryMerge(tc.l, tc.r, tc.predicates, semantics.NewSemTable()) assert.Equal(t, tc.expected, result) }) } diff --git a/go/vt/vtgate/semantics/semantic_state.go b/go/vt/vtgate/semantics/semantic_state.go index 6e770a5c188..f30bb023641 100644 --- a/go/vt/vtgate/semantics/semantic_state.go +++ b/go/vt/vtgate/semantics/semantic_state.go @@ -42,6 +42,11 @@ type ( } ) +// NewSemTable creates a new empty SemTable +func NewSemTable() *SemTable { + return &SemTable{exprDependencies: map[sqlparser.Expr]TableSet{}} +} + // TableSetFor returns the bitmask for this particular tableshoe func (st *SemTable) TableSetFor(t table) TableSet { for idx, t2 := range st.Tables { @@ -54,7 +59,10 @@ func (st *SemTable) TableSetFor(t table) TableSet { // Dependencies return the table dependencies of the expression. func (st *SemTable) Dependencies(expr sqlparser.Expr) TableSet { - var deps TableSet + deps, found := st.exprDependencies[expr] + if found { + return deps + } _ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { colName, ok := node.(*sqlparser.ColName) @@ -65,6 +73,8 @@ func (st *SemTable) Dependencies(expr sqlparser.Expr) TableSet { return true, nil }, expr) + st.exprDependencies[expr] = deps + return deps }