Skip to content

Commit

Permalink
encoding/gob: lazily initialize registerBasics
Browse files Browse the repository at this point in the history
Defer call to registerBasics until the first invocation of NewDecoder() or NewEncoder()

This saves ~13.5kB of allocation in init()

Updates golang#26775
  • Loading branch information
zegl committed Aug 2, 2018
1 parent a2ef8b9 commit f2e8552
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/encoding/gob/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Decoder struct {
// If r does not also implement io.ByteReader, it will be wrapped in a
// bufio.Reader.
func NewDecoder(r io.Reader) *Decoder {
registerBasics()

dec := new(Decoder)
// We use the ability to read bytes as a plausible surrogate for buffering.
if _, ok := r.(io.ByteReader); !ok {
Expand Down
2 changes: 2 additions & 0 deletions src/encoding/gob/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var spaceForLength = make([]byte, maxLength)

// NewEncoder returns a new encoder that will transmit on the io.Writer.
func NewEncoder(w io.Writer) *Encoder {
registerBasics()

enc := new(Encoder)
enc.w = []io.Writer{w}
enc.sent = make(map[reflect.Type]typeId)
Expand Down
75 changes: 39 additions & 36 deletions src/encoding/gob/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ func init() {
panic(fmt.Sprintln("nextId too large:", nextId))
}
nextId = firstUserId
registerBasics()
wireTypeUserInfo = userType(reflect.TypeOf((*wireType)(nil)))
}

Expand Down Expand Up @@ -874,40 +873,44 @@ func Register(value interface{}) {
RegisterName(name, value)
}

var registerBasicsOnce sync.Once

func registerBasics() {
Register(int(0))
Register(int8(0))
Register(int16(0))
Register(int32(0))
Register(int64(0))
Register(uint(0))
Register(uint8(0))
Register(uint16(0))
Register(uint32(0))
Register(uint64(0))
Register(float32(0))
Register(float64(0))
Register(complex64(0i))
Register(complex128(0i))
Register(uintptr(0))
Register(false)
Register("")
Register([]byte(nil))
Register([]int(nil))
Register([]int8(nil))
Register([]int16(nil))
Register([]int32(nil))
Register([]int64(nil))
Register([]uint(nil))
Register([]uint8(nil))
Register([]uint16(nil))
Register([]uint32(nil))
Register([]uint64(nil))
Register([]float32(nil))
Register([]float64(nil))
Register([]complex64(nil))
Register([]complex128(nil))
Register([]uintptr(nil))
Register([]bool(nil))
Register([]string(nil))
registerBasicsOnce.Do(func() {
Register(int(0))
Register(int8(0))
Register(int16(0))
Register(int32(0))
Register(int64(0))
Register(uint(0))
Register(uint8(0))
Register(uint16(0))
Register(uint32(0))
Register(uint64(0))
Register(float32(0))
Register(float64(0))
Register(complex64(0i))
Register(complex128(0i))
Register(uintptr(0))
Register(false)
Register("")
Register([]byte(nil))
Register([]int(nil))
Register([]int8(nil))
Register([]int16(nil))
Register([]int32(nil))
Register([]int64(nil))
Register([]uint(nil))
Register([]uint8(nil))
Register([]uint16(nil))
Register([]uint32(nil))
Register([]uint64(nil))
Register([]float32(nil))
Register([]float64(nil))
Register([]complex64(nil))
Register([]complex128(nil))
Register([]uintptr(nil))
Register([]bool(nil))
Register([]string(nil))
})
}

0 comments on commit f2e8552

Please sign in to comment.