-
Notifications
You must be signed in to change notification settings - Fork 105
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
refactor: optimize subnet calculations using big.Int for reduced memory usage #1969
Conversation
network/commons/common.go
Outdated
// SetCommitteeSubnet returns the subnet for the given committee, it doesn't allocate memory but uses the passed in big.Int | ||
func SetCommitteeSubnet(bigInst *big.Int, cid spectypes.CommitteeID) uint64 { | ||
bigInst.SetBytes(cid[:]) | ||
return bigInst.Mod(bigInst, bigIntSubnetsCount).Uint64() | ||
} |
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 think it would make more sense if it just set the value and not returned it
the caller can just call Uint64() and it would emphasize the big.Int is being modified (even without reading comment)
that's less convenient, but it's an optimized alternative func so it's OK imo
operator/validator/controller.go
Outdated
func (c *controller) selfSubnets() records.Subnets { | ||
// selfSubnets calculates the operator's subnets by adding up the fixed subnets and the active committees | ||
// it recvs big int to avoid unnecessary conversions, if intCid is nil it will allocate a new big int | ||
func (c *controller) selfSubnets(intCid *big.Int) records.Subnets { |
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.
externally the big.Int is just a buffer and has no usage outside this func, so to avoid confusion i'd call it just "buffer" and clarify it's for re-using memory (we can keep explanation about cid
but don't have to IMO)
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.
taking inspiration from https://pkg.go.dev/bytes#Buffer
which has a similar pattern to what you did where if you dont pass buf
it will allocate for you
Codecov ReportAttention: Patch coverage is
Additional details and impacted files☔ View full report in Codecov by Sentry. |
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.
LGTM 🔥
…adata sync (#1805) * rename setupEventHandling to syncContractEvents * refactor beaconprotocol.UpdateValidatorsMetadata to MetadataFetcher * don't pass logger to operatorNode.Start() * don't pass logger to reportOperators * don't pass logger to p2pNetwork Setup and Start * fix TestSetupValidatorsExporter * reduce errors text in fetchAndUpdateValidatorsMetadata * extract a package for metadata updating; start it before p2p setup * minor cleanup * pass context * get rid of update metadata loop in validator controller * remove unused code from validator controller * fix tests for StartValidators * initialize metadata updater before validator controller * various fixes in metadata updater * sharesStorage -> shareStorage * remove redundant comment * avoid blocking on channel send * return shares instead of nil on timeout * fix TODO's; add tests * fix linter * review comments and some code improvements * review comments [2] * minor improvements * move metadata updater inside validator * review comments [3] * add comments * add another comment * network/p2p: extract logger changes to another PR * network/p2p: revert leftovers * resolve a busy loop * remove logic with indices diff * wrap context in reportIndicesChange * review comments * ValidatorSyncer -> Syncer * fix comment * rename receiver * get rid of fetcher * fix TestUpdateValidatorMetadata * NewValidatorSyncer -> NewSyncer * minor renames * add a comment in HandleMetadataUpdates * revert removal of active index comparison * add self subnets logic missed on merge conflicts * fix leftovers * apply changes from #1969 * filter shares by own subnets * use fixed subnets * improve the last batch sleep comment * minor rename * comments * comment * comment * logs --------- Co-authored-by: zippoxer <[email protected]>
…adata sync (#1805) * rename setupEventHandling to syncContractEvents * refactor beaconprotocol.UpdateValidatorsMetadata to MetadataFetcher * don't pass logger to operatorNode.Start() * don't pass logger to reportOperators * don't pass logger to p2pNetwork Setup and Start * fix TestSetupValidatorsExporter * reduce errors text in fetchAndUpdateValidatorsMetadata * extract a package for metadata updating; start it before p2p setup * minor cleanup * pass context * get rid of update metadata loop in validator controller * remove unused code from validator controller * fix tests for StartValidators * initialize metadata updater before validator controller * various fixes in metadata updater * sharesStorage -> shareStorage * remove redundant comment * avoid blocking on channel send * return shares instead of nil on timeout * fix TODO's; add tests * fix linter * review comments and some code improvements * review comments [2] * minor improvements * move metadata updater inside validator * review comments [3] * add comments * add another comment * network/p2p: extract logger changes to another PR * network/p2p: revert leftovers * resolve a busy loop * remove logic with indices diff * wrap context in reportIndicesChange * review comments * ValidatorSyncer -> Syncer * fix comment * rename receiver * get rid of fetcher * fix TestUpdateValidatorMetadata * NewValidatorSyncer -> NewSyncer * minor renames * add a comment in HandleMetadataUpdates * revert removal of active index comparison * add self subnets logic missed on merge conflicts * fix leftovers * apply changes from #1969 * filter shares by own subnets * use fixed subnets * improve the last batch sleep comment * minor rename * comments * comment * comment * logs --------- Co-authored-by: zippoxer <[email protected]>
Description
I observed high memory usage and a lot of allocations stemming from the
CommitteeSubnet
code used to calculate the subnets for which validators we should pull metadata for (Changed in #1787)Changes
SubnetsCount
big.Int
representation to reduce allocations.SetCommitteeSubnet
which receives abig.Int
instance and uses it instead of allocating new ints.