Skip to content

Commit

Permalink
types/dynamicpb: switch atomicExtFiles to atomic.Uint64 type
Browse files Browse the repository at this point in the history
(Go Protobuf currently requires Go 1.21.)

This guarantees correct alignment on stack-allocated values, too.

Change-Id: If713532c20326a7ee3f1dd5c77f49c1d52f6eb9a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/641017
Reviewed-by: Chressie Himpel <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
stapelberg committed Jan 7, 2025
1 parent ad89419 commit 9acc8f2
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions types/dynamicpb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ type extField struct {
type Types struct {
// atomicExtFiles is used with sync/atomic and hence must be the first word
// of the struct to guarantee 64-bit alignment.
//
// TODO(stapelberg): once we only support Go 1.19 and newer, switch this
// field to be of type atomic.Uint64 to guarantee alignment on
// stack-allocated values, too.
atomicExtFiles uint64
atomicExtFiles atomic.Uint64
extMu sync.Mutex

files *protoregistry.Files
Expand Down Expand Up @@ -90,7 +86,7 @@ func (t *Types) FindExtensionByName(name protoreflect.FullName) (protoreflect.Ex
func (t *Types) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
// Construct the extension number map lazily, since not every user will need it.
// Update the map if new files are added to the registry.
if atomic.LoadUint64(&t.atomicExtFiles) != uint64(t.files.NumFiles()) {
if t.atomicExtFiles.Load() != uint64(t.files.NumFiles()) {
t.updateExtensions()
}
xd := t.extensionsByMessage[extField{message, field}]
Expand Down Expand Up @@ -133,10 +129,10 @@ func (t *Types) FindMessageByURL(url string) (protoreflect.MessageType, error) {
func (t *Types) updateExtensions() {
t.extMu.Lock()
defer t.extMu.Unlock()
if atomic.LoadUint64(&t.atomicExtFiles) == uint64(t.files.NumFiles()) {
if t.atomicExtFiles.Load() == uint64(t.files.NumFiles()) {
return
}
defer atomic.StoreUint64(&t.atomicExtFiles, uint64(t.files.NumFiles()))
defer t.atomicExtFiles.Store(uint64(t.files.NumFiles()))
t.files.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
t.registerExtensions(fd.Extensions())
t.registerExtensionsInMessages(fd.Messages())
Expand Down

0 comments on commit 9acc8f2

Please sign in to comment.