github.com/xiaoshude/protoreflect@v1.16.1-0.20220310024924-8c94d7247598/desc/descriptor_unsafe.go (about)

     1  //+build !appengine,!gopherjs,!purego
     2  // NB: other environments where unsafe is unappropriate should use "purego" build tag
     3  // https://github.com/golang/go/issues/23172
     4  
     5  package desc
     6  
     7  import (
     8  	"sync/atomic"
     9  	"unsafe"
    10  )
    11  
    12  type jsonNameMap map[string]*FieldDescriptor // loaded/stored atomically via atomic+unsafe
    13  type memoizedDefault *interface{}            // loaded/stored atomically via atomic+unsafe
    14  
    15  // FindFieldByJSONName finds the field with the given JSON field name. If no such
    16  // field exists then nil is returned. Only regular fields are returned, not
    17  // extensions.
    18  func (md *MessageDescriptor) FindFieldByJSONName(jsonName string) *FieldDescriptor {
    19  	// NB: We don't want to eagerly index JSON names because many programs won't use it.
    20  	// So we want to do it lazily, but also make sure the result is thread-safe. So we
    21  	// atomically load/store the map as if it were a normal pointer. We don't use other
    22  	// mechanisms -- like sync.Mutex, sync.RWMutex, sync.Once, or atomic.Value -- to
    23  	// do this lazily because those types cannot be copied, and we'd rather not induce
    24  	// 'go vet' errors in programs that use descriptors and try to copy them.
    25  	// If multiple goroutines try to access the index at the same time, before it is
    26  	// built, they will all end up computing the index redundantly. Future reads of
    27  	// the index will use whatever was the "last one stored" by those racing goroutines.
    28  	// Since building the index is deterministic, this is fine: all indices computed
    29  	// will be the same.
    30  	addrOfJsonNames := (*unsafe.Pointer)(unsafe.Pointer(&md.jsonNames))
    31  	jsonNames := atomic.LoadPointer(addrOfJsonNames)
    32  	var index map[string]*FieldDescriptor
    33  	if jsonNames == nil {
    34  		// slow path: compute the index
    35  		index = map[string]*FieldDescriptor{}
    36  		for _, f := range md.fields {
    37  			jn := f.GetJSONName()
    38  			index[jn] = f
    39  		}
    40  		atomic.StorePointer(addrOfJsonNames, *(*unsafe.Pointer)(unsafe.Pointer(&index)))
    41  	} else {
    42  		*(*unsafe.Pointer)(unsafe.Pointer(&index)) = jsonNames
    43  	}
    44  	return index[jsonName]
    45  }
    46  
    47  func (fd *FieldDescriptor) getDefaultValue() interface{} {
    48  	addrOfDef := (*unsafe.Pointer)(unsafe.Pointer(&fd.def))
    49  	def := atomic.LoadPointer(addrOfDef)
    50  	if def != nil {
    51  		return *(*interface{})(def)
    52  	}
    53  	// slow path: compute the default, potentially involves decoding value
    54  	d := fd.determineDefault()
    55  	atomic.StorePointer(addrOfDef, (unsafe.Pointer(&d)))
    56  	return d
    57  }