Skip to content

Commit 03f9a84

Browse files
committed
tree: avoid creating new DString in AdjustValueToType when possible
Previously, in `AdjustValueToType` we would always create a new `DString` object if the original datum wasn't modified in any way. This seems unnecessary, so we now track whether a modification might have occurred and allocate the new object only in that case, otherwise returning the original datum. We choose to not do the same for collated strings since there it wouldn't have been an equivalent change (we currently initialize a new CollationEnvironment). Noticed this inefficiency when looking at CPU profile of a node running an IMPORT. ``` name old time/op new time/op delta ImportFixture/tpcc/warehouses=1-24 586ms ± 9% 585ms ± 4% ~ (p=0.853 n=10+10) name old speed new speed delta ImportFixture/tpcc/warehouses=1-24 148MB/s ±10% 148MB/s ± 5% ~ (p=0.912 n=10+10) name old alloc/op new alloc/op delta ImportFixture/tpcc/warehouses=1-24 1.13GB ± 1% 1.10GB ± 1% -2.76% (p=0.000 n=10+10) name old allocs/op new allocs/op delta ImportFixture/tpcc/warehouses=1-24 6.08M ± 0% 4.12M ± 0% -32.25% (p=0.000 n=10+10) ``` Release note: None
1 parent be2cc31 commit 03f9a84

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pkg/sql/sem/tree/datum.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6393,11 +6393,15 @@ func AdjustValueToType(typ *types.T, inVal Datum) (outVal Datum, err error) {
63936393
switch typ.Family() {
63946394
case types.StringFamily, types.CollatedStringFamily:
63956395
var sv string
6396+
var isString, isCollatedString bool
63966397
if v, ok := AsDString(inVal); ok {
63976398
sv = string(v)
6399+
isString = true
63986400
} else if v, ok := inVal.(*DCollatedString); ok {
63996401
sv = v.Contents
6402+
isCollatedString = true
64006403
}
6404+
origLen := len(sv)
64016405
switch typ.Oid() {
64026406
case oid.T_char:
64036407
// "char" is supposed to truncate long values.
@@ -6432,9 +6436,14 @@ func AdjustValueToType(typ *types.T, inVal Datum) (outVal Datum, err error) {
64326436
}
64336437

64346438
if typ.Oid() == oid.T_bpchar || typ.Oid() == oid.T_char || typ.Oid() == oid.T_varchar {
6435-
if _, ok := AsDString(inVal); ok {
6439+
if isString {
6440+
if len(sv) == origLen {
6441+
// The string wasn't modified, so we can just return the
6442+
// original datum.
6443+
return inVal, nil
6444+
}
64366445
return NewDString(sv), nil
6437-
} else if _, ok := inVal.(*DCollatedString); ok {
6446+
} else if isCollatedString {
64386447
return NewDCollatedString(sv, typ.Locale(), &CollationEnvironment{})
64396448
}
64406449
}

0 commit comments

Comments
 (0)