Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

GetProtocols memory implementation incorrect slice preallocation #197

Closed
mrd0ll4r opened this issue Apr 21, 2022 · 1 comment · Fixed by #198
Closed

GetProtocols memory implementation incorrect slice preallocation #197

mrd0ll4r opened this issue Apr 21, 2022 · 1 comment · Fixed by #198

Comments

@mrd0ll4r
Copy link
Contributor

A small performance bug:

The memory implementation holds segments of protocols map[peer.ID]map[string]struct{}.
In GetProtocols(p peer.ID) a slice is preallocated to len((segment).protocols), which is the number of peers in that segment.
It was probably intended to be be len((segment).protocols[p]) instead.

Source: https://github.com/libp2p/go-libp2p-peerstore/blob/master/pstoremem/protobook.go#L134

Fix:

s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()

out := make([]string, 0, len(s.protocols[p]))
for k := range s.protocols[p] {
	out = append(out, k)
}
@marten-seemann
Copy link
Contributor

Good catch! Would you mind submitting a PR?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants