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

Use New Cache Key For Unaggregated Attestations #14590

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

nisdas
Copy link
Member

@nisdas nisdas commented Oct 30, 2024

What type of PR is this?

Optimization

What does this PR do? Why is it needed?

This optimizes the cache key computation for specific attestation and reduces the amount of memory allocated. In certain networks, this is a high traffic path which leads to unnecessary allocation of memory.

Benchmarks of New Implementation:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
BenchmarkCacheKeyImplementations
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation-8                  7620234               169.0 ns/op           280 B/op          4 allocs/op
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation-8                 35468606                31.01 ns/op           24 B/op          1 allocs/op

Which issues(s) does this PR fix?

N.A

Other notes for review

Acknowledgements

  • I have read CONTRIBUTING.md.
  • I have made an appropriate entry to CHANGELOG.md.
  • I have added a description to this PR with sufficient context for reviewers to understand this PR.

@nisdas nisdas added the Ready For Review A pull request ready for code review label Oct 30, 2024
@nisdas nisdas requested a review from a team as a code owner October 30, 2024 05:48
@@ -328,3 +326,15 @@ func (s *Service) hasBlockAndState(ctx context.Context, blockRoot [32]byte) bool
hasState := hasStateSummary || s.cfg.beaconDB.HasState(ctx, blockRoot)
return hasState && s.cfg.chain.HasBlock(ctx, blockRoot)
}

func seenAttCacheKey(slot primitives.Slot, committeeID primitives.CommitteeIndex, aggregationBits []byte) string {
totalLen := 8 + 8 + len(aggregationBits)
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be helpful to add an explanation of what these numbers represent for anyone unfamiliar with the code. Otherwise they just seem like magic numbers

@@ -328,3 +326,15 @@ func (s *Service) hasBlockAndState(ctx context.Context, blockRoot [32]byte) bool
hasState := hasStateSummary || s.cfg.beaconDB.HasState(ctx, blockRoot)
return hasState && s.cfg.chain.HasBlock(ctx, blockRoot)
}

func seenAttCacheKey(slot primitives.Slot, committeeID primitives.CommitteeIndex, aggregationBits []byte) string {
Copy link
Member

Choose a reason for hiding this comment

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

A benchmark test would be nice so that we can see the before/after improvements.

Copy link
Member Author

Choose a reason for hiding this comment

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

Benchmark between old and new implementations:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
BenchmarkCacheKeyImplementations
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation-8                  7620234               169.0 ns/op           280 B/op          4 allocs/op
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation-8                 35468606                31.01 ns/op           24 B/op          1 allocs/op


// Avoid copying to reduce allocation when casting. It is guaranteed to be immutable as the
// slice is only created in this method and isn't accessed/returned anywhere else.
return unsafe.String(&key[0], len(key)) // #nosec G103
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the same bytesutil.UnsafeCastToString from #14591 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do after that PR is merged in and remove this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants