Skip to content

Commit

Permalink
Reduce init time allocation when declaring types used for reflect (#821)
Browse files Browse the repository at this point in the history
In declaration of types used for reflect, use
reflect.TypeOf((*T)).Elem() instead of reflect.TypeOf(T{}) to avoid
init-time allocations.

See related stdlib issue: golang/go#55973
  • Loading branch information
dolmen committed Oct 7, 2022
1 parent 9428417 commit d268873
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions types.go
Expand Up @@ -6,9 +6,9 @@ import (
"time"
)

var timeType = reflect.TypeOf(time.Time{})
var textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
var mapStringInterfaceType = reflect.TypeOf(map[string]interface{}{})
var sliceInterfaceType = reflect.TypeOf([]interface{}{})
var timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
var textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
var textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
var mapStringInterfaceType = reflect.TypeOf(map[string]interface{}(nil))
var sliceInterfaceType = reflect.TypeOf([]interface{}(nil))
var stringType = reflect.TypeOf("")

0 comments on commit d268873

Please sign in to comment.