From b52f6c98233f35ce9a9007cd9933fd2fb7a408b2 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Thu, 11 Nov 2021 11:16:41 -0500 Subject: [PATCH] Remove some allocs for slices in interfaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` name old time/op new time/op delta UnmarshalDataset/config-2 24.9ms ± 1% 24.9ms ± 0% ~ (p=0.413 n=5+4) UnmarshalDataset/canada-2 66.1ms ± 0% 61.7ms ± 1% -6.63% (p=0.008 n=5+5) UnmarshalDataset/citm_catalog-2 25.3ms ± 5% 24.7ms ± 1% -2.09% (p=0.032 n=5+5) UnmarshalDataset/twitter-2 10.9ms ± 2% 10.9ms ± 2% ~ (p=1.000 n=5+5) UnmarshalDataset/code-2 108ms ± 0% 108ms ± 0% ~ (p=0.095 n=5+5) UnmarshalDataset/example-2 177µs ± 2% 176µs ± 0% ~ (p=0.841 n=5+5) Unmarshal/SimpleDocument/struct-2 579ns ± 0% 586ns ± 1% +1.30% (p=0.008 n=5+5) Unmarshal/SimpleDocument/map-2 875ns ± 1% 876ns ± 0% ~ (p=0.548 n=5+5) Unmarshal/ReferenceFile/struct-2 49.7µs ± 1% 49.5µs ± 0% ~ (p=0.095 n=5+5) Unmarshal/ReferenceFile/map-2 80.4µs ± 0% 79.6µs ± 0% -0.99% (p=0.008 n=5+5) Unmarshal/HugoFrontMatter-2 13.9µs ± 0% 13.7µs ± 0% -1.70% (p=0.008 n=5+5) name old speed new speed delta UnmarshalDataset/config-2 42.1MB/s ± 1% 42.2MB/s ± 0% ~ (p=0.381 n=5+4) UnmarshalDataset/canada-2 33.3MB/s ± 0% 35.7MB/s ± 1% +7.11% (p=0.008 n=5+5) UnmarshalDataset/citm_catalog-2 22.1MB/s ± 5% 22.6MB/s ± 1% +2.08% (p=0.032 n=5+5) UnmarshalDataset/twitter-2 40.7MB/s ± 2% 40.6MB/s ± 2% ~ (p=1.000 n=5+5) UnmarshalDataset/code-2 24.8MB/s ± 0% 24.9MB/s ± 0% ~ (p=0.103 n=5+5) UnmarshalDataset/example-2 45.8MB/s ± 2% 46.0MB/s ± 0% ~ (p=0.841 n=5+5) Unmarshal/SimpleDocument/struct-2 19.0MB/s ± 0% 18.8MB/s ± 1% -1.26% (p=0.008 n=5+5) Unmarshal/SimpleDocument/map-2 12.6MB/s ± 1% 12.6MB/s ± 0% ~ (p=0.508 n=5+5) Unmarshal/ReferenceFile/struct-2 105MB/s ± 1% 106MB/s ± 0% ~ (p=0.095 n=5+5) Unmarshal/ReferenceFile/map-2 65.2MB/s ± 0% 65.8MB/s ± 0% +1.00% (p=0.008 n=5+5) Unmarshal/HugoFrontMatter-2 39.3MB/s ± 0% 40.0MB/s ± 0% +1.73% (p=0.008 n=5+5) name old alloc/op new alloc/op delta UnmarshalDataset/config-2 5.85MB ± 0% 5.85MB ± 0% -0.00% (p=0.008 n=5+5) UnmarshalDataset/canada-2 76.6MB ± 0% 75.2MB ± 0% -1.76% (p=0.016 n=4+5) UnmarshalDataset/citm_catalog-2 35.3MB ± 0% 35.0MB ± 0% -0.71% (p=0.008 n=5+5) UnmarshalDataset/twitter-2 13.5MB ± 0% 13.5MB ± 0% -0.19% (p=0.016 n=4+5) UnmarshalDataset/code-2 22.3MB ± 0% 22.0MB ± 0% -1.31% (p=0.008 n=5+5) UnmarshalDataset/example-2 204kB ± 0% 203kB ± 0% -0.34% (p=0.008 n=5+5) Unmarshal/SimpleDocument/struct-2 709B ± 0% 709B ± 0% ~ (all equal) Unmarshal/SimpleDocument/map-2 1.08kB ± 0% 1.08kB ± 0% ~ (all equal) Unmarshal/ReferenceFile/struct-2 19.8kB ± 0% 19.7kB ± 0% -0.24% (p=0.008 n=5+5) Unmarshal/ReferenceFile/map-2 37.3kB ± 0% 37.0kB ± 0% -0.64% (p=0.029 n=4+4) Unmarshal/HugoFrontMatter-2 7.26kB ± 0% 7.22kB ± 0% -0.66% (p=0.008 n=5+5) name old allocs/op new allocs/op delta UnmarshalDataset/config-2 230k ± 0% 230k ± 0% -0.00% (p=0.000 n=5+4) UnmarshalDataset/canada-2 447k ± 0% 391k ± 0% -12.53% (p=0.008 n=5+5) UnmarshalDataset/citm_catalog-2 169k ± 0% 158k ± 0% -6.20% (p=0.029 n=4+4) UnmarshalDataset/twitter-2 55.8k ± 0% 54.7k ± 0% -1.88% (p=0.029 n=4+4) UnmarshalDataset/code-2 1.06M ± 0% 1.05M ± 0% -1.14% (p=0.008 n=5+5) UnmarshalDataset/example-2 1.31k ± 0% 1.28k ± 0% -2.21% (p=0.008 n=5+5) Unmarshal/SimpleDocument/struct-2 8.00 ± 0% 8.00 ± 0% ~ (all equal) Unmarshal/SimpleDocument/map-2 13.0 ± 0% 13.0 ± 0% ~ (all equal) Unmarshal/ReferenceFile/struct-2 125 ± 0% 123 ± 0% -1.60% (p=0.008 n=5+5) Unmarshal/ReferenceFile/map-2 600 ± 0% 590 ± 0% -1.67% (p=0.008 n=5+5) Unmarshal/HugoFrontMatter-2 132 ± 0% 130 ± 0% -1.52% (p=0.008 n=5+5) ``` --- unmarshaler.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index 8251db2..92237c4 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -625,7 +625,7 @@ type unmarshalArrayFn func(d *decoder, array *ast.Node, v reflect.Value) error var globalUnmarshalArrayFnCache atomic.Value // map[danger.TypeID]unmarshalArrayFn -func unmarshalArrayFnFor(vt reflect.Type) unmarshalArrayFn { +func unmarshalArrayFnForSlice(vt reflect.Type) unmarshalArrayFn { tid := danger.MakeTypeID(vt) cache, _ := globalUnmarshalArrayFnCache.Load().(map[danger.TypeID]unmarshalArrayFn) @@ -693,7 +693,7 @@ func (d *decoder) unmarshalArray(array *ast.Node, v reflect.Value) error { switch v.Kind() { case reflect.Slice: vt = v.Type() - fn := unmarshalArrayFnFor(vt) + fn := unmarshalArrayFnForSlice(vt) return fn(d, array, v) case reflect.Array: vt = v.Type() @@ -701,8 +701,8 @@ func (d *decoder) unmarshalArray(array *ast.Node, v reflect.Value) error { case reflect.Interface: elem := v.Elem() if !elem.IsValid() { - elem = reflect.New(sliceInterfaceType).Elem() - elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16)) + s := make([]interface{}, 0, 16) + elem = reflect.ValueOf(&s).Elem() } else if elem.Kind() == reflect.Slice { if elem.Type() != sliceInterfaceType { elem = reflect.New(sliceInterfaceType).Elem()