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

Return ReadOnlyValidator slice from state and use it when possible #13782

Closed
wants to merge 3 commits into from

Conversation

rkapka
Copy link
Contributor

@rkapka rkapka commented Mar 21, 2024

What type of PR is this?

Other

What does this PR do? Why is it needed?

Whenever we need validators from the state, we call Validators() which returns a copy of the state's validators. But we don't need mutable objects when all we do is perform read operations. Returning a slice of ReadOnlyValidator wrappers is many times faster and doesn't require making copies.

What this PR entails:

  • ReadOnlyValidators() state method that returns a slice of read only wrappers over the state's validators
  • Change uses of Validators() to the new method whenever possible
  • Move read only validator struct and interface to consensus-types module to avoid circular dependencies in some cases

@rkapka rkapka requested a review from a team as a code owner March 21, 2024 04:44
@rkapka rkapka requested review from kasey, nisdas and james-prysm March 21, 2024 04:44
Comment on lines +182 to +190
vals := mv.Value(mv.State())
rovals := make([]validator.ReadOnlyValidator, len(vals))
var err error
for i, v := range vals {
rovals[i], err = validator.NewValidator(v)
if err != nil {
return nil, err
}
}
Copy link
Contributor Author

@rkapka rkapka Mar 21, 2024

Choose a reason for hiding this comment

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

It takes e few ms to convert 1 million validators, so this extra code is not a big issue

@rkapka rkapka force-pushed the state-readonlyvals branch 3 times, most recently from e091954 to 0d1e0cc Compare March 21, 2024 05:10
@rkapka rkapka force-pushed the state-readonlyvals branch from 0d1e0cc to da97349 Compare March 21, 2024 05:42
@rkapka rkapka changed the title Use ReadOnlyValidator when possible Return ReadOnlyValidator slice from state and use it when possible Mar 21, 2024
@nisdas
Copy link
Member

nisdas commented Mar 26, 2024

Can you quanitfy the advantages of ReadOnlyValidators here over Validators ? This is a big change and in some places I don't think it is helpful. Some things that are helpful are like to like heap/cpu profiles with this method(epoch transitions) and also some benchmarks on how much mem is actually allocated and CPU time it takes for the different methods. In the readOnlyValidator version, you still make a large slice of structs(instead of pointers). Avoiding copying each validator is helpful, but I would like more data on this in a production workload.

@rkapka rkapka closed this Apr 15, 2024
@rkapka rkapka deleted the state-readonlyvals branch May 16, 2024 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants