mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
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.
This commit is contained in:
committed by
Ron Evans
parent
af936f3261
commit
535e64adbc
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user