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

Commit 06edc32

Browse files
authored
Merge pull request #86 from libp2p/fix/alloc-less
reduce allocations when adding addrs
2 parents 693780b + 491519a commit 06edc32

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

pstoremem/addr_book.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
154154
log.Warningf("was passed nil multiaddr for %s", p)
155155
continue
156156
}
157-
addrstr := string(addr.Bytes())
158-
a, found := amap[addrstr]
157+
asBytes := addr.Bytes()
158+
a, found := amap[string(asBytes)] // won't allocate.
159159
if !found || exp.After(a.Expires) {
160-
amap[addrstr] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
160+
amap[string(asBytes)] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
161161

162162
mab.subManager.BroadcastAddr(p, addr)
163163
}
@@ -188,14 +188,14 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
188188
log.Warningf("was passed nil multiaddr for %s", p)
189189
continue
190190
}
191-
// re-set all of them for new ttl.
192-
addrstr := string(addr.Bytes())
193191

192+
// re-set all of them for new ttl.
193+
aBytes := addr.Bytes()
194194
if ttl > 0 {
195-
amap[addrstr] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
195+
amap[string(aBytes)] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
196196
mab.subManager.BroadcastAddr(p, addr)
197197
} else {
198-
delete(amap, addrstr)
198+
delete(amap, string(aBytes))
199199
}
200200
}
201201
}

0 commit comments

Comments
 (0)