Skip to content

Commit

Permalink
consts and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mdisibio committed Oct 8, 2024
1 parent 372e6cb commit faaee88
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
45 changes: 23 additions & 22 deletions pkg/traceql/ast_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (o *BinaryOperation) execute(span Span) (Static, error) {
}

if recording {
o.b.Finish(0)
o.b.Finish(leftBranch)
}

// Look for cases where we don't even need to evalulate the RHS
Expand Down Expand Up @@ -366,7 +366,7 @@ func (o *BinaryOperation) execute(span Span) (Static, error) {
}

if recording {
o.b.Finish(1)
o.b.Finish(rightBranch)
}

// Ensure the resolved types are still valid
Expand Down Expand Up @@ -449,39 +449,40 @@ func (o *BinaryOperation) execute(span Span) (Static, error) {
rhsB, _ := rhs.Bool()

if recording {
if done := o.b.Sampled(); done {
if o.b.OptimalBranch() == 1 {
// RHS is the optimal starting branch,
// so swap the elements now.
o.LHS, o.RHS = o.RHS, o.LHS
}
}
}

switch o.Op {
case OpAnd:
if recording {
switch o.Op {
case OpAnd:
if !lhsB {
// Record cost of wasted rhs execution
o.b.Penalize(1)
o.b.Penalize(rightBranch)
}
if !rhsB {
// Record cost of wasted lhs execution
o.b.Penalize(0)
o.b.Penalize(leftBranch)
}
}
return NewStaticBool(lhsB && rhsB), nil
case OpOr:
if recording {
case OpOr:
if rhsB {
// Record cost of wasted lhs execution
o.b.Penalize(0)
o.b.Penalize(rightBranch)
}
if lhsB {
// Record cost of wasated rhs execution
o.b.Penalize(1)
o.b.Penalize(leftBranch)
}
}

if done := o.b.Sampled(); done {
if o.b.OptimalBranch() == rightBranch {
// RHS is the optimal starting branch,
// so swap the elements now.
o.LHS, o.RHS = o.RHS, o.LHS
}
}
}

switch o.Op {
case OpAnd:
return NewStaticBool(lhsB && rhsB), nil
case OpOr:
return NewStaticBool(lhsB || rhsB), nil
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/traceql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (b *bucketSet) addAndTest(i int) bool {
return false
}

const (
leftBranch = 0
rightBranch = 1
)

type branchOptimizer struct {
start time.Time
last []time.Duration
Expand Down

0 comments on commit faaee88

Please sign in to comment.