Skip to content

Commit

Permalink
coldataext: remove an allocation in production builds for arrays
Browse files Browse the repository at this point in the history
This commit makes it so that we only perform assertions in the datum vec
in the test builds. In particular, this allows us to remove an
allocation for each array object being set (which happens in
`tree.DArray.ResolvedType` where we construct an array type on demand).
I was just looking at a heap profile where this allocation accounted for
7% of all allocations.

Release note: None
  • Loading branch information
yuzefovich committed Jan 17, 2023
1 parent 2ee814f commit 990ef9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/col/coldataext/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/sql/sem/eval",
"//pkg/sql/sem/tree",
"//pkg/sql/types",
"//pkg/util/buildutil",
"@com_github_cockroachdb_errors//:errors",
],
)
Expand Down
17 changes: 13 additions & 4 deletions pkg/col/coldataext/datum_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/errors"
)

Expand Down Expand Up @@ -90,7 +91,9 @@ func (dv *datumVec) Get(i int) coldata.Datum {
// Set implements coldata.DatumVec interface.
func (dv *datumVec) Set(i int, v coldata.Datum) {
datum := convertToDatum(v)
dv.assertValidDatum(datum)
if buildutil.CrdbTestBuild {
dv.assertValidDatum(datum)
}
dv.data[i] = datum
}

Expand All @@ -106,21 +109,27 @@ func (dv *datumVec) Window(start, end int) coldata.DatumVec {
// CopySlice implements coldata.DatumVec interface.
func (dv *datumVec) CopySlice(src coldata.DatumVec, destIdx, srcStartIdx, srcEndIdx int) {
castSrc := src.(*datumVec)
dv.assertSameTypeFamily(castSrc.t)
if buildutil.CrdbTestBuild {
dv.assertSameTypeFamily(castSrc.t)
}
copy(dv.data[destIdx:], castSrc.data[srcStartIdx:srcEndIdx])
}

// AppendSlice implements coldata.DatumVec interface.
func (dv *datumVec) AppendSlice(src coldata.DatumVec, destIdx, srcStartIdx, srcEndIdx int) {
castSrc := src.(*datumVec)
dv.assertSameTypeFamily(castSrc.t)
if buildutil.CrdbTestBuild {
dv.assertSameTypeFamily(castSrc.t)
}
dv.data = append(dv.data[:destIdx], castSrc.data[srcStartIdx:srcEndIdx]...)
}

// AppendVal implements coldata.DatumVec interface.
func (dv *datumVec) AppendVal(v coldata.Datum) {
datum := convertToDatum(v)
dv.assertValidDatum(datum)
if buildutil.CrdbTestBuild {
dv.assertValidDatum(datum)
}
dv.data = append(dv.data, datum)
}

Expand Down

0 comments on commit 990ef9c

Please sign in to comment.