Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: avoid string to byte conversion copies on insert path #101693

Merged
merged 12 commits into from
May 31, 2023
Merged
31 changes: 31 additions & 0 deletions pkg/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,36 @@ func runBenchmarkInsert(b *testing.B, db *sqlutils.SQLRunner, count int) {

}

// runBenchmarkInsertLarge benchmarks inserting count large rows into a table
// where large means rows with a 1k string and 1k BYTES object.
func runBenchmarkInsertLarge(b *testing.B, db *sqlutils.SQLRunner, count int) {
defer func() {
db.Exec(b, `DROP TABLE IF EXISTS bench.insert`)
}()

db.Exec(b, `CREATE TABLE bench.insert (k INT PRIMARY KEY, s STRING, b BYTES)`)
bigstr := strings.Repeat("x", 1<<10)
bigbytes := bytes.Repeat([]byte("x"), 1<<10)

b.ResetTimer()
var buf bytes.Buffer
val := 0
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(`INSERT INTO bench.insert VALUES `)
for j := 0; j < count; j++ {
if j > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "(%d, '%s', '%s')", val, bigstr, bigbytes)
val++
}
db.Exec(b, buf.String())
}
b.StopTimer()

}

// runBenchmarkInsertFK benchmarks inserting count rows into a table with a
// present foreign key into another table.
func runBenchmarkInsertFK(b *testing.B, db *sqlutils.SQLRunner, count int) {
Expand Down Expand Up @@ -419,6 +449,7 @@ func BenchmarkSQL(b *testing.B) {
for _, runFn := range []func(*testing.B, *sqlutils.SQLRunner, int){
runBenchmarkDelete,
runBenchmarkInsert,
runBenchmarkInsertLarge,
runBenchmarkInsertDistinct,
runBenchmarkInsertFK,
runBenchmarkInsertSecondaryIndex,
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/colenc/inverted.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/errors"
)

Expand All @@ -32,7 +33,7 @@ func invertedColToDatum(vec coldata.Vec, row int) tree.Datum {
return tree.NewDJSON(vec.JSON().Get(row))
case types.StringFamily:
b := vec.Bytes().Get(row)
s := unsafeConvertBytesToString(b)
s := encoding.UnsafeConvertBytesToString(b)
return tree.NewDString(s)
}
// This handles arrays, geo etc.
Expand Down
10 changes: 2 additions & 8 deletions pkg/sql/colenc/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package colenc

import (
"unsafe"

"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand Down Expand Up @@ -144,9 +142,9 @@ func encodeKeys[T []byte | roachpb.Key](
}
s := ss.Get(r + start)
if dir == encoding.Ascending {
kys[r] = encoding.EncodeStringAscending(b, unsafeConvertBytesToString(s))
kys[r] = encoding.EncodeStringAscending(b, encoding.UnsafeConvertBytesToString(s))
} else {
kys[r] = encoding.EncodeStringDescending(b, unsafeConvertBytesToString(s))
kys[r] = encoding.EncodeStringDescending(b, encoding.UnsafeConvertBytesToString(s))
}
}
case types.TimestampFamily, types.TimestampTZFamily:
Expand Down Expand Up @@ -249,7 +247,3 @@ func (b *BatchEncoder) encodeIndexKey(
}
return nil
}

func unsafeConvertBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
3 changes: 2 additions & 1 deletion pkg/sql/colenc/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/errors"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func MarshalLegacy(colType *types.T, vec coldata.Vec, row int) (roachpb.Value, e
switch vec.Type().Family() {
case types.StringFamily, types.BytesFamily, types.UuidFamily, types.EnumFamily:
b := vec.Bytes().Get(row)
r.SetString(unsafeConvertBytesToString(b))
r.SetString(encoding.UnsafeConvertBytesToString(b))
return r, nil
}
case types.TimestampFamily, types.TimestampTZFamily:
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colexec/colexecproj/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_library(
"//pkg/sql/sqltelemetry", # keep
"//pkg/sql/types", # keep
"//pkg/util/duration", # keep
"//pkg/util/encoding", # keep
"//pkg/util/json", # keep
"@com_github_cockroachdb_apd_v3//:apd", # keep
"@com_github_cockroachdb_errors//:errors", # keep
Expand Down
41 changes: 21 additions & 20 deletions pkg/sql/colexec/colexecproj/proj_non_const_ops.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/sql/colexec/colexecproj/proj_non_const_ops_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/errors"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colexec/colexecprojconst/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_library(
"//pkg/sql/sqltelemetry", # keep
"//pkg/sql/types",
"//pkg/util/duration", # keep
"//pkg/util/encoding", # keep
"//pkg/util/json", # keep
"@com_github_cockroachdb_apd_v3//:apd", # keep
"@com_github_cockroachdb_errors//:errors",
Expand Down
Loading