Skip to content
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

Merged
merged 1 commit into from
Dec 29, 2023

Conversation

StephenButtolph
Copy link
Contributor

Why this should be merged

  • serializedFieldIndices is initialized during struct creation, so the nil 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

  • Removes the dead code
  • Optimistically grabs a read lock prior to falling back to the write lock.

How this was tested

  • CI

@StephenButtolph StephenButtolph added the cleanup Code quality improvement label Dec 29, 2023
@StephenButtolph StephenButtolph added this to the v1.10.18 milestone Dec 29, 2023
@StephenButtolph StephenButtolph self-assigned this Dec 29, 2023
Comment on lines -71 to -73
if s.serializedFieldIndices == nil {
s.serializedFieldIndices = make(map[reflect.Type][]FieldDesc)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💀

Comment on lines +114 to +120
func (s *structFielder) getCachedSerializedFields(t reflect.Type) ([]FieldDesc, bool) {
s.lock.RLock()
defer s.lock.RUnlock()

cachedFields, ok := s.serializedFieldIndices[t]
return cachedFields, ok
}
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@StephenButtolph StephenButtolph added this pull request to the merge queue Dec 29, 2023
Merged via the queue into dev with commit 029867f Dec 29, 2023
17 checks passed
@StephenButtolph StephenButtolph deleted the struct-fielder-nits branch December 29, 2023 16:11
pull bot pushed a commit to AKAJOKER2/avalanchego that referenced this pull request Jan 1, 2024
…a-labs#2560)

Co-authored-by: Alberto Benegiamo <alberto.benegiamo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants