Skip to content

Commit 7383821

Browse files
authored
fix: remove peer cache (#2786)
In cases where the peer store has grown very large (e.g. when a well connected node has been online for a long time), the peer store can get so large that this cache causes abnormal memory use. The cache was added without any profiling, remove the cache for now, we can add it back if it actually solves a problem
1 parent 717731e commit 7383821

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

packages/peer-store/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"dependencies": {
6262
"@libp2p/crypto": "^5.0.5",
6363
"@libp2p/interface": "^2.1.3",
64-
"@libp2p/peer-collections": "^6.0.9",
6564
"@libp2p/peer-id": "^5.0.6",
6665
"@libp2p/peer-record": "^8.0.9",
6766
"@multiformats/multiaddr": "^12.2.3",

packages/peer-store/src/store.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { InvalidParametersError } from '@libp2p/interface'
2-
import { PeerMap } from '@libp2p/peer-collections'
32
import { peerIdFromCID } from '@libp2p/peer-id'
43
import mortice, { type Mortice } from 'mortice'
54
import { base32 } from 'multiformats/bases/base32'
@@ -20,37 +19,27 @@ export interface PeerUpdate extends PeerUpdateExternal {
2019
updated: boolean
2120
}
2221

23-
function decodePeer (key: Key, value: Uint8Array, cache: PeerMap<Peer>): Peer {
22+
function decodePeer (key: Key, value: Uint8Array): Peer {
2423
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
2524
const base32Str = key.toString().split('/')[2]
2625
const buf = CID.parse(base32Str, base32)
2726
const peerId = peerIdFromCID(buf)
2827

29-
const cached = cache.get(peerId)
30-
31-
if (cached != null) {
32-
return cached
33-
}
34-
35-
const peer = bytesToPeer(peerId, value)
36-
37-
cache.set(peerId, peer)
38-
39-
return peer
28+
return bytesToPeer(peerId, value)
4029
}
4130

42-
function mapQuery (query: PeerQuery, cache: PeerMap<Peer>): Query {
31+
function mapQuery (query: PeerQuery): Query {
4332
if (query == null) {
4433
return {}
4534
}
4635

4736
return {
4837
prefix: NAMESPACE_COMMON,
4938
filters: (query.filters ?? []).map(fn => ({ key, value }) => {
50-
return fn(decodePeer(key, value, cache))
39+
return fn(decodePeer(key, value))
5140
}),
5241
orders: (query.orders ?? []).map(fn => (a, b) => {
53-
return fn(decodePeer(a.key, a.value, cache), decodePeer(b.key, b.value, cache))
42+
return fn(decodePeer(a.key, a.value), decodePeer(b.key, b.value))
5443
})
5544
}
5645
}
@@ -131,10 +120,8 @@ export class PersistentStore {
131120
}
132121

133122
async * all (query?: PeerQuery): AsyncGenerator<Peer, void, unknown> {
134-
const peerCache = new PeerMap<Peer>()
135-
136-
for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}, peerCache))) {
137-
const peer = decodePeer(key, value, peerCache)
123+
for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}))) {
124+
const peer = decodePeer(key, value)
138125

139126
if (peer.id.equals(this.peerId)) {
140127
// Skip self peer if present

0 commit comments

Comments
 (0)