Skip to content

Commit

Permalink
sql: use FastIntMap for QueryState.RangesPerNode
Browse files Browse the repository at this point in the history
This commit switches `QueryState.RangesPerNode` from a `map[roachpb.NodeID]int`
to a `util.FastIntMap`. This avoids a pair of heap allocation as long as node
IDs say below 32 and the number of ranges for each node stays below 14.

```
name                   old time/op    new time/op    delta
KV/Scan/SQL/rows=1-10    94.5µs ± 4%    94.1µs ± 6%    ~     (p=0.796 n=10+10)

name                   old alloc/op   new alloc/op   delta
KV/Scan/SQL/rows=1-10    20.1kB ± 0%    19.9kB ± 0%  -0.84%  (p=0.000 n=10+9)

name                   old allocs/op  new allocs/op  delta
KV/Scan/SQL/rows=1-10       245 ± 0%       243 ± 0%  -0.82%  (p=0.000 n=10+9)
```
  • Loading branch information
nvanbenschoten committed Dec 31, 2021
1 parent cc22c45 commit fad1ef7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/sql/physicalplan/replicaoracle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//pkg/rpc",
"//pkg/settings/cluster",
"//pkg/sql/sqlerrors",
"//pkg/util",
"@com_github_cockroachdb_errors//:errors",
],
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/physicalplan/replicaoracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/errors"
)

Expand Down Expand Up @@ -106,14 +107,13 @@ var oracleFactories = map[Policy]OracleFactory{}
// QueryState encapsulates the history of assignments of ranges to nodes
// done by an oracle on behalf of one particular query.
type QueryState struct {
RangesPerNode map[roachpb.NodeID]int
RangesPerNode util.FastIntMap
AssignedRanges map[roachpb.RangeID]roachpb.ReplicaDescriptor
}

// MakeQueryState creates an initialized QueryState.
func MakeQueryState() QueryState {
return QueryState{
RangesPerNode: make(map[roachpb.NodeID]int),
AssignedRanges: make(map[roachpb.RangeID]roachpb.ReplicaDescriptor),
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (o *binPackingOracle) ChoosePreferredReplica(
minLoad := int(math.MaxInt32)
var leastLoadedIdx int
for i, repl := range replicas {
assignedRanges := queryState.RangesPerNode[repl.NodeID]
assignedRanges := queryState.RangesPerNode.GetDefault(int(repl.NodeID))
if assignedRanges != 0 && assignedRanges < o.maxPreferredRangesPerLeaseHolder {
return repl.ReplicaDescriptor, nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/physicalplan/span_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func (it *spanResolverIterator) ReplicaInfo(
if err != nil {
return roachpb.ReplicaDescriptor{}, err
}
it.queryState.RangesPerNode[repl.NodeID]++
prev := it.queryState.RangesPerNode.GetDefault(int(repl.NodeID))
it.queryState.RangesPerNode.Set(int(repl.NodeID), prev+1)
it.queryState.AssignedRanges[rngID] = repl
return repl, nil
}

0 comments on commit fad1ef7

Please sign in to comment.