Skip to content

Commit

Permalink
topdown: move context.Context cancellation check (#7210)
Browse files Browse the repository at this point in the history
The topdown Cancel machinery is there because it's cheap to check. ctx.Err() is
not.

This change moves the "Is the context the cause for cancellation?" check into
the branch were evaluation has been aborted through the topdown.Cancel call.

When evaluation has already been cancelled, an expensive check no longer
matters much -- when it's still ongoing, it'll affect the overall performance.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus authored Dec 6, 2024
1 parent 51a48eb commit b4e2909
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 7 additions & 8 deletions topdown/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,14 @@ func (e *eval) evalExpr(iter evalIterator) error {
return &earlyExitError{prev: err, e: e}
}

if e.ctx != nil && e.ctx.Err() != nil {
return &Error{
Code: CancelErr,
Message: e.ctx.Err().Error(),
err: e.ctx.Err(),
}
}

if e.cancel != nil && e.cancel.Cancelled() {
if e.ctx != nil && e.ctx.Err() != nil {
return &Error{
Code: CancelErr,
Message: e.ctx.Err().Error(),
err: e.ctx.Err(),
}
}
return &Error{
Code: CancelErr,
Message: "caller cancelled query execution",
Expand Down
6 changes: 5 additions & 1 deletion topdown/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,12 +1625,16 @@ func TestContextErrorHandling(t *testing.T) {
txn := storage.NewTransactionOrDie(ctx, store)
defer store.Abort(ctx, txn)

c := NewCancel()
query := NewQuery(ast.MustParseBody("")).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn)
WithTransaction(txn).
WithCancel(c)

testCtx := tc.before()
c.Cancel()

qrs, err := query.Run(testCtx)

if err == nil {
Expand Down

0 comments on commit b4e2909

Please sign in to comment.