Skip to content

Fix/protocol diff complexity #3329

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pranavkonde
Copy link

Description

Fixes #3328

Optimizes the protocol diff implementation in identify/id.go from O(n²) to O(n) by using a map-based approach instead of nested loops.

Changes

// Before: O(n²)
for _, x := range b {
    for _, y := range a {
        if x == y {
            found = true
// After: O(n)
seen := make(map[protocol.ID]struct{}, len(a))
for _, x := range a {
    seen[x] = struct{}{}
}

Copy link
Member

@sukunrt sukunrt left a comment

Choose a reason for hiding this comment

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

Thanks @pranavkonde. Can you make this PR over master and not your other PR?

@@ -753,31 +754,28 @@ func (ids *idService) getSignedRecord(snapshot *identifySnapshot) []byte {

// diff takes two slices of strings (a and b) and computes which elements were added and removed in b
func diff(a, b []protocol.ID) (added, removed []protocol.ID) {
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 do sort -> unique -> diff?
That'll make it not alloc for the map.

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.

Performance Optimization: Improve Protocol Diff Algorithm Complexity
2 participants