-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove dead code and unnecessary lock from reflect codec #2560
Conversation
if s.serializedFieldIndices == nil { | ||
s.serializedFieldIndices = make(map[reflect.Type][]FieldDesc) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💀
func (s *structFielder) getCachedSerializedFields(t reflect.Type) ([]FieldDesc, bool) { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
cachedFields, ok := s.serializedFieldIndices[t] | ||
return cachedFields, ok | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I waffled back and forth on if this should be it's own function... but ended up making it it's own function so that we could defer the RUnlock
@@ -65,15 +65,13 @@ type structFielder struct { | |||
} | |||
|
|||
func (s *structFielder) GetSerializedFields(t reflect.Type) ([]FieldDesc, error) { | |||
if serializedFields, ok := s.getCachedSerializedFields(t); ok { // use pre-computed result | |||
return serializedFields, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We expect this to always happen once the node has marshalled / unmarshalled all of the struct types that are registered by the node.
…a-labs#2560) Co-authored-by: Alberto Benegiamo <[email protected]>
Why this should be merged
serializedFieldIndices
is initialized during struct creation, so thenil
check is dead code.serializedFieldIndices
is expected to only be modified around node startup. This means that we expect the vast majority of the time for the fields to be cached. This avoids grabbing a write lock when looking up the cached fields.How this works
How this was tested