Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "google.golang.org/protobuf/internal/encoding/wire" |
| 9 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 10 | ) |
| 11 | |
| 12 | type extensionFieldInfo struct { |
| 13 | wiretag uint64 |
| 14 | tagsize int |
| 15 | funcs ifaceCoderFuncs |
| 16 | } |
| 17 | |
| 18 | func (mi *MessageType) extensionFieldInfo(desc *piface.ExtensionDescV1) *extensionFieldInfo { |
| 19 | // As of this time (Go 1.12, linux/amd64), an RWMutex benchmarks as faster |
| 20 | // than a sync.Map. |
| 21 | mi.extensionFieldInfosMu.RLock() |
| 22 | e, ok := mi.extensionFieldInfos[desc] |
| 23 | mi.extensionFieldInfosMu.RUnlock() |
| 24 | if ok { |
| 25 | return e |
| 26 | } |
| 27 | |
| 28 | etype := extensionTypeFromDesc(desc) |
| 29 | wiretag := wire.EncodeTag(etype.Number(), wireTypes[etype.Kind()]) |
| 30 | e = &extensionFieldInfo{ |
| 31 | wiretag: wiretag, |
| 32 | tagsize: wire.SizeVarint(wiretag), |
| 33 | funcs: encoderFuncsForValue(etype, etype.GoType()), |
| 34 | } |
| 35 | |
| 36 | mi.extensionFieldInfosMu.Lock() |
| 37 | if mi.extensionFieldInfos == nil { |
| 38 | mi.extensionFieldInfos = make(map[*piface.ExtensionDescV1]*extensionFieldInfo) |
| 39 | } |
| 40 | mi.extensionFieldInfos[desc] = e |
| 41 | mi.extensionFieldInfosMu.Unlock() |
| 42 | return e |
| 43 | } |