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

refactor: optimize subnet calculations using big.Int for reduced memory usage #1969

Merged
merged 5 commits into from
Dec 29, 2024

Conversation

y0sher
Copy link
Contributor

@y0sher y0sher commented Dec 29, 2024

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

  • Initialized a global SubnetsCount big.Int representation to reduce allocations.
  • Created SetCommitteeSubnet which receives a big.Int instance and uses it instead of allocating new ints.
  • Used the new function in all the metadata collection loop calls to CommitteeSubnet.

@y0sher y0sher changed the base branch from main to stage December 29, 2024 11:24
Comment on lines 71 to 75
// 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()
}
Copy link
Contributor

@moshe-blox moshe-blox Dec 29, 2024

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

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

@moshe-blox moshe-blox Dec 29, 2024

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)

Copy link
Contributor

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

Copy link

codecov bot commented Dec 29, 2024

Codecov Report

Attention: Patch coverage is 65.21739% with 8 lines in your changes missing coverage. Please review.

Project coverage is 46.5%. Comparing base (4db9639) to head (0074688).
Report is 6 commits behind head on stage.

Files with missing lines Patch % Lines
operator/validator/controller.go 52.9% 7 Missing and 1 partial ⚠️
Additional details and impacted files

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@moshe-blox moshe-blox left a comment

Choose a reason for hiding this comment

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

LGTM 🔥

@y0sher y0sher merged commit fc9bd6c into stage Dec 29, 2024
7 checks passed
@y0sher y0sher deleted the opt/cmtsubnet-no-allocation branch December 29, 2024 16:41
nkryuchkov added a commit that referenced this pull request Jan 12, 2025
moshe-blox added a commit that referenced this pull request Jan 15, 2025
…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]>
oleg-ssvlabs pushed a commit that referenced this pull request Jan 20, 2025
…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]>
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.

3 participants