Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-20.2: opt: reduce allocations in Implicator and JoinOrderBuilder #54214

Merged
merged 2 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/sql/opt/partialidx/implicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func (im *Implicator) Init(f *norm.Factory, md *opt.Metadata, evalCtx *tree.Eval
im.f = f
im.md = md
im.evalCtx = evalCtx
im.constraintCache = make(map[opt.ScalarExpr]constraintCacheItem)
}

// FiltersImplyPredicate attempts to prove that a partial index predicate is
Expand Down Expand Up @@ -647,9 +646,18 @@ func (im *Implicator) twoVarComparisonImpliesTwoVarComparison(
return false, true
}

// initConstraintCache initializes the constraintCache field if it has not yet
// been initialized.
func (im *Implicator) initConstraintCache() {
if im.constraintCache == nil {
im.constraintCache = make(map[opt.ScalarExpr]constraintCacheItem)
}
}

// cacheConstraint caches a constraint set and a tight boolean for the given
// scalar expression.
func (im *Implicator) cacheConstraint(e opt.ScalarExpr, c *constraint.Set, tight bool) {
im.initConstraintCache()
if _, ok := im.constraintCache[e]; !ok {
im.constraintCache[e] = constraintCacheItem{
c: c,
Expand All @@ -662,6 +670,7 @@ func (im *Implicator) cacheConstraint(e opt.ScalarExpr, c *constraint.Set, tight
// cache contains an entry for the given scalar expression. It returns
// ok = false if the scalar expression does not exist in the cache.
func (im *Implicator) fetchConstraint(e opt.ScalarExpr) (_ *constraint.Set, tight bool, ok bool) {
im.initConstraintCache()
if res, ok := im.constraintCache[e]; ok {
return res.c, res.tight, true
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/opt/xform/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type Optimizer struct {
disabledRules RuleSet

// JoinOrderBuilder adds new join orderings to the memo.
jb *JoinOrderBuilder
jb JoinOrderBuilder
}

// Init initializes the Optimizer with a new, blank memo structure inside. This
Expand All @@ -112,7 +112,7 @@ func (o *Optimizer) Init(evalCtx *tree.EvalContext, catalog cat.Catalog) {
o.matchedRule = nil
o.appliedRule = nil
o.disabledRules = util.FastIntSet{}
o.jb = &JoinOrderBuilder{}
o.jb = JoinOrderBuilder{}
if evalCtx.TestingKnobs.DisableOptimizerRuleProbability > 0 {
o.disableRules(evalCtx.TestingKnobs.DisableOptimizerRuleProbability)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (o *Optimizer) SetCoster(coster Coster) {
// JoinOrderBuilder returns the JoinOrderBuilder instance that the optimizer is
// currently using to reorder join trees.
func (o *Optimizer) JoinOrderBuilder() *JoinOrderBuilder {
return o.jb
return &o.jb
}

// DisableOptimizations disables all transformation rules, including normalize
Expand Down