# UNDF: UNDF-2026-000001013 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -698,22 +698,18 @@ func (ids *idService) getRecordBytes() []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) { - // This is O(n^2), but it's fine because the slices are small. - for _, x := range b { - var found bool - if slices.Contains(a, x) { - found = true - } - if !found { + aSet := make(map[protocol.ID]struct{}, len(a)) + for _, x := range a { + aSet[x] = struct{}{} + } + bSet := make(map[protocol.ID]struct{}, len(b)) + for _, x := range b { + bSet[x] = struct{}{} + if _, ok := aSet[x]; !ok { added = append(added, x) } } for _, x := range a { - var found bool - if slices.Contains(b, x) { - found = true - } - if !found { + if _, ok := bSet[x]; !ok { removed = append(removed, x) } }