Skip to content

Commit

Permalink
encoding/gob: avoid pointers to fieldType
Browse files Browse the repository at this point in the history
fieldType is a struct with only a string and an integer,
so its size will barely be three times that of a pointer.

The indirection doesn't save us any memory or append/grow cost,
but it does cause a significant amount of allocations at init time.

	goos: linux
	goarch: amd64
	pkg: encoding/gob
	cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics
					│     old     │                new                 │
					│   sec/op    │   sec/op     vs base               │
	EndToEndPipe-16   730.9n ± 5%   741.6n ± 5%       ~ (p=0.529 n=10)
	EncodingGob       173.7µ ± 0%   171.1µ ± 0%  -1.46% (p=0.000 n=10)
	geomean           11.27µ        11.26µ       -0.01%

					│     old      │                  new                   │
					│     B/op     │     B/op      vs base                  │
	EndToEndPipe-16   1.766Ki ± 0%   1.766Ki ± 0%        ~ (p=1.000 n=10) ¹
	EncodingGob       38.27Ki ± 0%   34.30Ki ± 0%  -10.38% (p=0.000 n=10)
	geomean           8.221Ki        7.782Ki        -5.33%
	¹ all samples are equal

					│    old     │                 new                 │
					│ allocs/op  │ allocs/op   vs base                 │
	EndToEndPipe-16   2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
	EncodingGob       642.0 ± 0%   615.0 ± 0%  -4.21% (p=0.000 n=10)
	geomean           35.83        35.07       -2.13%
	¹ all samples are equal

Change-Id: I852a799834d2e9b7b915da74e871a4052d13892e
Reviewed-on: https://go-review.googlesource.com/c/go/+/479400
Reviewed-by: Ian Lance Taylor <[email protected]>
Run-TryBot: Daniel Martí <[email protected]>
Reviewed-by: Rob Pike <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
mvdan committed Mar 29, 2023
1 parent 72013c4 commit f80e270
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/encoding/gob/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ type fieldType struct {

type structType struct {
CommonType
Field []*fieldType
Field []fieldType
}

func (s *structType) safeString(seen map[typeId]bool) string {
Expand Down Expand Up @@ -549,7 +549,7 @@ func newTypeObject(name string, ut *userTypeInfo, rt reflect.Type) (gobType, err
if gt.id() == 0 {
setTypeId(gt)
}
st.Field = append(st.Field, &fieldType{f.Name, gt.id()})
st.Field = append(st.Field, fieldType{f.Name, gt.id()})
}
return st, nil

Expand Down

0 comments on commit f80e270

Please sign in to comment.