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

Max-cover: minor optimizations #8311

Merged
merged 3 commits into from Jan 22, 2021
Merged

Max-cover: minor optimizations #8311

merged 3 commits into from Jan 22, 2021

Conversation

farazdagi
Copy link
Contributor

What type of PR is this?

Other / Optimization

What does this PR do? Why is it needed?

  • This PR improves performance of the max-cover aggregation w/o any major changes (in follow-up PR the new optimized Bitlist64 based implementation of max-cover will be pushed).
  • Optimizations include:
    • Getting rid of redundant bit length validation, as we validate once, when aggregation starts, see:
      if err := unaggregated.validate(); err != nil {
      if errors.Is(err, aggregation.ErrBitsDifferentLen) {
      return unaggregated, nil
      }
      return nil, err
      }
    • Replacing manual list merging with simple call to append()
    • Using a filtering w/o allocation (see how it works in Slice Tricks

Which issues(s) does this PR fix?

Part of #7871, extracted from #7938

Other notes for review

Original benchmark:
image

After applying this PR's patch:
image

@farazdagi farazdagi self-assigned this Jan 21, 2021
return atts, nil
}
return aggregated.merge(unaggregated), err
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NewMaxCover() doesn't return error now, since it is used in a loop over the same set of attestations, and it is more optimal to validate that set of attestations once, before loop starts (which we indeed do, see line 25 above).

}
}

func NewMaxCover(atts []*ethpb.Attestation) *aggregation.MaxCoverProblem {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just removing validation in a new method.

i++
}
return merged
return append(al, al1...)
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 am not sure what I was thinking about when I implemented manual merging of two slices, when append() has us covered (if future the whole attList.merge() method might be removed, and we will just be inlining append(), but for now wanted to keep change constrained).

@@ -171,7 +144,7 @@ func (al attList) filterContained() attList {
sort.Slice(al, func(i, j int) bool {
return al[i].AggregationBits.Count() > al[j].AggregationBits.Count()
})
filtered := make([]*ethpb.Attestation, 0, len(al))
filtered := al[:0]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just relying on fact that al[:0] will be sharing the same underlying array, so no need to allocate anything - we are ok wit in-place amendment (see https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating)

@farazdagi farazdagi marked this pull request as ready for review January 22, 2021 11:32
@farazdagi farazdagi requested a review from a team as a code owner January 22, 2021 11:32
@farazdagi farazdagi added the Ready For Review A pull request ready for code review label Jan 22, 2021
@nisdas nisdas merged commit 75fc3b0 into develop Jan 22, 2021
@delete-merged-branch delete-merged-branch bot deleted the maxcover-minor-optimizations branch January 22, 2021 12:10
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.

None yet

2 participants