Skip to content

Commit f596bd3

Browse files
authored
marshal: use passed in byte slice if available. (apache#1167)
We don't aloways need to allocate a new slice to unmarshal into when the passed in one is available. ``` name old time/op new time/op delta UnmarshalVarchar-4 280ns ± 3% 75ns ± 4% -73.23% (p=0.000 n=18+19) name old alloc/op new alloc/op delta UnmarshalVarchar-4 1.06kB ± 0% 0.03kB ± 0% -96.97% (p=0.000 n=20+20) name old allocs/op new allocs/op delta UnmarshalVarchar-4 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=20+20) ```
1 parent 246df0a commit f596bd3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

marshal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,13 @@ func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
232232
return nil
233233
case *[]byte:
234234
if data != nil {
235-
*v = copyBytes(data)
235+
*v = append((*v)[:0], data...)
236236
} else {
237237
*v = nil
238238
}
239239
return nil
240240
}
241+
241242
rv := reflect.ValueOf(value)
242243
if rv.Kind() != reflect.Ptr {
243244
return unmarshalErrorf("can not unmarshal into non-pointer %T", value)

marshal_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,3 +1401,16 @@ func TestMarshalDate(t *testing.T) {
14011401
}
14021402
}
14031403
}
1404+
1405+
func BenchmarkUnmarshalVarchar(b *testing.B) {
1406+
b.ReportAllocs()
1407+
src := make([]byte, 1024)
1408+
dst := make([]byte, len(src))
1409+
1410+
b.ResetTimer()
1411+
for i := 0; i < b.N; i++ {
1412+
if err := unmarshalVarchar(NativeType{}, src, &dst); err != nil {
1413+
b.Fatal(err)
1414+
}
1415+
}
1416+
}

0 commit comments

Comments
 (0)