Skip to content

Commit

Permalink
Merge #53237
Browse files Browse the repository at this point in the history
53237: sql/rowexec: recycle coveringSpans in batchedInvertedExprEvaluator r=nvanbenschoten a=nvanbenschoten

This commit updates batchedInvertedExprEvaluator to recycle the []invertedSpan
that it constructs in its init method, such that the same slice can be reused
for each batch of rows in the `invertedJoiner`.

Before this change, this heap allocations accounted for 5.51% of all heap
allocations (by size, `alloc_space`) in a geospatial query of interest:

```
----------------------------------------------------------+-------------
                                             804MB   100% |   github.com/cockroachdb/cockroach/pkg/sql/rowexec.(*invertedJoiner).readInput /go/src/github.com/cockroachdb/cockroach/pkg/sql/rowexec/inverted_joiner.go:418
     804MB  5.51% 36.69%      804MB  5.51%                | github.com/cockroachdb/cockroach/pkg/sql/rowexec.(*batchedInvertedExprEvaluator).init /go/src/github.com/cockroachdb/cockroach/pkg/sql/rowexec/inverted_expr_evaluator.go:407
----------------------------------------------------------+-------------
```

When applied on top of #53181 and #53215, this results in a **0.4%** speedup on
the following geospatial query:

```sql
SELECT Count(census.blkid), Count(subways.name)
FROM nyc_census_blocks AS census
JOIN nyc_subway_stations AS subways
ON ST_Intersects(subways.geom, census.geom);
```

```
name                                old ms    new ms    delta
Test/postgis_geometry_tutorial/nyc  134 ±12%  133 ±12%  -0.40%  (p=0.006 n=976+979)
```

Co-authored-by: Nathan VanBenschoten <[email protected]>
  • Loading branch information
craig[bot] and nvanbenschoten committed Aug 22, 2020
2 parents 7ac3e98 + 2601363 commit 79ffcf6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/sql/rowexec/inverted_expr_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ type batchedInvertedExprEvaluator struct {
fragmentedSpans []invertedSpanRoutingInfo

// Temporary state used during initialization.
routingSpans []invertedSpanRoutingInfo
routingSpans []invertedSpanRoutingInfo
coveringSpans []invertedSpan
}

// Helper used in building fragmentedSpans using pendingSpans. pendingSpans
Expand Down Expand Up @@ -348,7 +349,8 @@ func (b *batchedInvertedExprEvaluator) pendingLenWithSameEnd(
}

// init fragments the spans for later routing of rows and returns spans
// representing a union of all the spans (for executing the scan).
// representing a union of all the spans (for executing the scan). The
// returned slice is only valid until the next call to reset.
func (b *batchedInvertedExprEvaluator) init() []invertedSpan {
if cap(b.exprEvals) < len(b.exprs) {
b.exprEvals = make([]*invertedExprEvaluator, len(b.exprs))
Expand Down Expand Up @@ -389,7 +391,6 @@ func (b *batchedInvertedExprEvaluator) init() []invertedSpan {
})

// The union of the spans, which is returned from this function.
var coveringSpans []invertedSpan
currentCoveringSpan := b.routingSpans[0].span
// Create a slice of pendingSpans to be fragmented by windowing over the
// full collection of routingSpans. All spans in a given window have the
Expand All @@ -404,7 +405,7 @@ func (b *batchedInvertedExprEvaluator) init() []invertedSpan {
if bytes.Compare(pendingSpans[0].span.Start, span.span.Start) < 0 {
pendingSpans = b.fragmentPendingSpans(pendingSpans, span.span.Start)
if bytes.Compare(currentCoveringSpan.End, span.span.Start) < 0 {
coveringSpans = append(coveringSpans, currentCoveringSpan)
b.coveringSpans = append(b.coveringSpans, currentCoveringSpan)
currentCoveringSpan = span.span
} else if bytes.Compare(currentCoveringSpan.End, span.span.End) < 0 {
currentCoveringSpan.End = span.span.End
Expand All @@ -417,8 +418,8 @@ func (b *batchedInvertedExprEvaluator) init() []invertedSpan {
pendingSpans = pendingSpans[:len(pendingSpans)+1]
}
b.fragmentPendingSpans(pendingSpans, nil)
coveringSpans = append(coveringSpans, currentCoveringSpan)
return coveringSpans
b.coveringSpans = append(b.coveringSpans, currentCoveringSpan)
return b.coveringSpans
}

// TODO(sumeer): if this will be called in non-decreasing order of enc,
Expand Down Expand Up @@ -451,4 +452,5 @@ func (b *batchedInvertedExprEvaluator) reset() {
b.exprEvals = b.exprEvals[:0]
b.fragmentedSpans = b.fragmentedSpans[:0]
b.routingSpans = b.routingSpans[:0]
b.coveringSpans = b.coveringSpans[:0]
}

0 comments on commit 79ffcf6

Please sign in to comment.