From 535e64adbcc1c0907c18a25c7f149c31cb69e708 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 13 May 2023 14:23:29 +0200 Subject: [PATCH] reflect: optimize Zero() a little bit It could be expensive to call Size() three times, and it is unnecessary. Instead, do it only once. This results in a very small reduction of binary size if Zero() is used. --- src/reflect/value.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/reflect/value.go b/src/reflect/value.go index bd8c1476..96cbcb80 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -1486,7 +1486,8 @@ func init() { } func Zero(typ Type) Value { - if typ.Size() <= unsafe.Sizeof(uintptr(0)) { + size := typ.Size() + if size <= unsafe.Sizeof(uintptr(0)) { return Value{ typecode: typ.(*rawType), value: nil, @@ -1494,7 +1495,7 @@ func Zero(typ Type) Value { } } - if typ.Size() <= zerobufferLen { + if size <= zerobufferLen { return Value{ typecode: typ.(*rawType), value: unsafe.Pointer(zerobuffer), @@ -1504,7 +1505,7 @@ func Zero(typ Type) Value { return Value{ typecode: typ.(*rawType), - value: alloc(typ.Size(), nil), + value: alloc(size, nil), flags: valueFlagExported | valueFlagRO, } }