Skip to content

Commit 48670ae

Browse files
authored
desc: fix caching when wrapping protoreflect.Descriptor instances (#606)
1 parent aee3749 commit 48670ae

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

desc/cache.go

-9
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,3 @@ func (c mapCache) get(d protoreflect.Descriptor) Descriptor {
4646
func (c mapCache) put(key protoreflect.Descriptor, val Descriptor) {
4747
c[key] = val
4848
}
49-
50-
type noopCache struct{}
51-
52-
func (noopCache) get(protoreflect.Descriptor) Descriptor {
53-
return nil
54-
}
55-
56-
func (noopCache) put(protoreflect.Descriptor, Descriptor) {
57-
}

desc/convert.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ func convertFile(d protoreflect.FileDescriptor, fd *descriptorpb.FileDescriptorP
9595
ret.deps = make([]*FileDescriptor, len(fd.GetDependency()))
9696
for i := 0; i < d.Imports().Len(); i++ {
9797
f := d.Imports().Get(i).FileDescriptor
98-
if c := cache.get(f); c != nil {
99-
ret.deps[i] = c.(*FileDescriptor)
100-
} else if c, err := wrapFile(f, cache); err != nil {
98+
if c, err := wrapFile(f, cache); err != nil {
10199
return nil, err
102100
} else {
103101
ret.deps[i] = c

desc/load.go

-7
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ func LoadFileDescriptor(file string) (*FileDescriptor, error) {
5353

5454
var fd *FileDescriptor
5555
loadedDescriptors.withLock(func(cache descriptorCache) {
56-
// double-check cache, in case it was concurrently added while
57-
// we were waiting for the lock
58-
f := cache.get(d)
59-
if f != nil {
60-
fd = f.(*FileDescriptor)
61-
return
62-
}
6356
fd, err = wrapFile(d, cache)
6457
})
6558
return fd, err

desc/wrap.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type DescriptorWrapper interface {
2121
// WrapDescriptor wraps the given descriptor, returning a desc.Descriptor
2222
// value that represents the same element.
2323
func WrapDescriptor(d protoreflect.Descriptor) (Descriptor, error) {
24-
return wrapDescriptor(d, noopCache{})
24+
return wrapDescriptor(d, mapCache{})
2525
}
2626

2727
func wrapDescriptor(d protoreflect.Descriptor, cache descriptorCache) (Descriptor, error) {
@@ -65,18 +65,21 @@ func WrapFiles(d []protoreflect.FileDescriptor) ([]*FileDescriptor, error) {
6565
// WrapFile wraps the given file descriptor, returning a *desc.FileDescriptor
6666
// value that represents the same file.
6767
func WrapFile(d protoreflect.FileDescriptor) (*FileDescriptor, error) {
68-
return wrapFile(d, noopCache{})
68+
return wrapFile(d, mapCache{})
6969
}
7070

7171
func wrapFile(d protoreflect.FileDescriptor, cache descriptorCache) (*FileDescriptor, error) {
72+
if res := cache.get(d); res != nil {
73+
return res.(*FileDescriptor), nil
74+
}
7275
fdp := protoutil.ProtoFromFileDescriptor(d)
7376
return convertFile(d, fdp, cache)
7477
}
7578

7679
// WrapMessage wraps the given message descriptor, returning a *desc.MessageDescriptor
7780
// value that represents the same message.
7881
func WrapMessage(d protoreflect.MessageDescriptor) (*MessageDescriptor, error) {
79-
return wrapMessage(d, noopCache{})
82+
return wrapMessage(d, mapCache{})
8083
}
8184

8285
func wrapMessage(d protoreflect.MessageDescriptor, cache descriptorCache) (*MessageDescriptor, error) {
@@ -97,7 +100,7 @@ func wrapMessage(d protoreflect.MessageDescriptor, cache descriptorCache) (*Mess
97100
// WrapField wraps the given field descriptor, returning a *desc.FieldDescriptor
98101
// value that represents the same field.
99102
func WrapField(d protoreflect.FieldDescriptor) (*FieldDescriptor, error) {
100-
return wrapField(d, noopCache{})
103+
return wrapField(d, mapCache{})
101104
}
102105

103106
func wrapField(d protoreflect.FieldDescriptor, cache descriptorCache) (*FieldDescriptor, error) {
@@ -121,7 +124,7 @@ func wrapField(d protoreflect.FieldDescriptor, cache descriptorCache) (*FieldDes
121124
// WrapOneOf wraps the given oneof descriptor, returning a *desc.OneOfDescriptor
122125
// value that represents the same oneof.
123126
func WrapOneOf(d protoreflect.OneofDescriptor) (*OneOfDescriptor, error) {
124-
return wrapOneOf(d, noopCache{})
127+
return wrapOneOf(d, mapCache{})
125128
}
126129

127130
func wrapOneOf(d protoreflect.OneofDescriptor, cache descriptorCache) (*OneOfDescriptor, error) {
@@ -138,7 +141,7 @@ func wrapOneOf(d protoreflect.OneofDescriptor, cache descriptorCache) (*OneOfDes
138141
// WrapEnum wraps the given enum descriptor, returning a *desc.EnumDescriptor
139142
// value that represents the same enum.
140143
func WrapEnum(d protoreflect.EnumDescriptor) (*EnumDescriptor, error) {
141-
return wrapEnum(d, noopCache{})
144+
return wrapEnum(d, mapCache{})
142145
}
143146

144147
func wrapEnum(d protoreflect.EnumDescriptor, cache descriptorCache) (*EnumDescriptor, error) {
@@ -159,7 +162,7 @@ func wrapEnum(d protoreflect.EnumDescriptor, cache descriptorCache) (*EnumDescri
159162
// WrapEnumValue wraps the given enum value descriptor, returning a *desc.EnumValueDescriptor
160163
// value that represents the same enum value.
161164
func WrapEnumValue(d protoreflect.EnumValueDescriptor) (*EnumValueDescriptor, error) {
162-
return wrapEnumValue(d, noopCache{})
165+
return wrapEnumValue(d, mapCache{})
163166
}
164167

165168
func wrapEnumValue(d protoreflect.EnumValueDescriptor, cache descriptorCache) (*EnumValueDescriptor, error) {
@@ -176,7 +179,7 @@ func wrapEnumValue(d protoreflect.EnumValueDescriptor, cache descriptorCache) (*
176179
// WrapService wraps the given service descriptor, returning a *desc.ServiceDescriptor
177180
// value that represents the same service.
178181
func WrapService(d protoreflect.ServiceDescriptor) (*ServiceDescriptor, error) {
179-
return wrapService(d, noopCache{})
182+
return wrapService(d, mapCache{})
180183
}
181184

182185
func wrapService(d protoreflect.ServiceDescriptor, cache descriptorCache) (*ServiceDescriptor, error) {
@@ -193,7 +196,7 @@ func wrapService(d protoreflect.ServiceDescriptor, cache descriptorCache) (*Serv
193196
// WrapMethod wraps the given method descriptor, returning a *desc.MethodDescriptor
194197
// value that represents the same method.
195198
func WrapMethod(d protoreflect.MethodDescriptor) (*MethodDescriptor, error) {
196-
return wrapMethod(d, noopCache{})
199+
return wrapMethod(d, mapCache{})
197200
}
198201

199202
func wrapMethod(d protoreflect.MethodDescriptor, cache descriptorCache) (*MethodDescriptor, error) {

0 commit comments

Comments
 (0)