diff options
| author | Zeke Lu <lvzecai@gmail.com> | 2022-10-04 07:10:09 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2022-10-04 23:15:54 +0000 |
| commit | e7d203f494281a229a7d4ef769f14975e9b12e4e (patch) | |
| tree | b89e9726ceb1bbc50ebae8f4832ec2bc1957dacb /src | |
| parent | 58158e990f272774e615c9abd8662bf0198c29aa (diff) | |
| download | go-e7d203f494281a229a7d4ef769f14975e9b12e4e.tar.xz | |
reflect: avoid unnecessary copy of funcTypes
Imagine that initFuncTypes is called with n=3, funcTypes will be
[nil, nil, nil, **reflect.rtype] afterward, then it's called with n=2.
The current implementation will copy funcTypes because funcTypes[2] is
nil. This is unnecessary. It should make a new slice and copy funcTypes
into it only when n >= len(funcTypes).
Updates #56011.
Change-Id: Ia093d2f550d6924a4c58bcd21325093e32b40baa
GitHub-Last-Rev: a599eae7c2f6a388dfe1ff39cf61fd645885a64d
GitHub-Pull-Request: golang/go#56024
Reviewed-on: https://go-review.googlesource.com/c/go/+/438395
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/reflect/type.go | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/reflect/type.go b/src/reflect/type.go index 339c982087..b06b7ffd9e 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -2005,13 +2005,15 @@ var funcTypesMutex sync.Mutex func initFuncTypes(n int) Type { funcTypesMutex.Lock() defer funcTypesMutex.Unlock() - if n < len(funcTypes) && funcTypes[n] != nil { + if n >= len(funcTypes) { + newFuncTypes := make([]Type, n+1) + copy(newFuncTypes, funcTypes) + funcTypes = newFuncTypes + } + if funcTypes[n] != nil { return funcTypes[n] } - newFuncTypes := make([]Type, n+1) - copy(newFuncTypes, funcTypes) - funcTypes = newFuncTypes funcTypes[n] = StructOf([]StructField{ { Name: "FuncType", |
