Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit aefb261

Browse files
author
Alan Shaw
authored
perf: lazy load IPLD formats (#1704)
This PR uses the new `loadFormat` option for IPLD to lazily require IPLD formats in order to reduce the startup time for the node. If you're feeling like you've seen this before then, for reference: The PR ipld/js-ipld#164 undid the work done in ipld/js-ipld#145 and ipld/js-ipld#178 re-enabled the ability to do so. This PR makes use of this new ability to lazy load the formats. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 06262b5 commit aefb261

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@
108108
"ipfs-repo": "~0.25.0",
109109
"ipfs-unixfs": "~0.1.16",
110110
"ipfs-unixfs-engine": "~0.33.0",
111-
"ipld": "~0.19.1",
111+
"ipld": "~0.19.3",
112112
"ipld-bitcoin": "~0.1.8",
113113
"ipld-dag-pb": "~0.14.11",
114114
"ipld-ethereum": "^2.0.1",
115115
"ipld-git": "~0.2.2",
116-
"ipld-raw": "^2.0.1",
117116
"ipld-zcash": "~0.1.6",
118117
"ipns": "~0.3.0",
119118
"is-ipfs": "~0.4.7",

src/core/index.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ const debug = require('debug')
1515
const extend = require('deep-extend')
1616
const EventEmitter = require('events')
1717

18-
// All known IPLD formats
19-
const ipldBitcoin = require('ipld-bitcoin')
20-
const ipldDagCbor = require('ipld-dag-cbor')
21-
const ipldDagPb = require('ipld-dag-pb')
22-
const ipldEthAccountSnapshot = require('ipld-ethereum').ethAccountSnapshot
23-
const ipldEthBlock = require('ipld-ethereum').ethBlock
24-
const ipldEthBlockList = require('ipld-ethereum').ethBlockList
25-
const ipldEthStateTrie = require('ipld-ethereum').ethStateTrie
26-
const ipldEthStorageTrie = require('ipld-ethereum').ethStorageTrie
27-
const ipldEthTrie = require('ipld-ethereum').ethTxTrie
28-
const ipldEthTx = require('ipld-ethereum').ethTx
29-
const ipldGit = require('ipld-git')
30-
const ipldRaw = require('ipld-raw')
31-
const ipldZcash = require('ipld-zcash')
32-
3318
const config = require('./config')
3419
const boot = require('./boot')
3520
const components = require('./components')
@@ -39,6 +24,40 @@ const defaultRepo = require('./runtime/repo-nodejs')
3924
const preload = require('./preload')
4025
const mfsPreload = require('./mfs-preload')
4126

27+
// All known (non-default) IPLD formats
28+
const IpldFormats = {
29+
get 'bitcoin-block' () {
30+
return require('ipld-bitcoin')
31+
},
32+
get 'eth-account-snapshot' () {
33+
return require('ipld-ethereum').ethAccountSnapshot
34+
},
35+
get 'eth-block' () {
36+
return require('ipld-ethereum').ethBlock
37+
},
38+
get 'eth-block-list' () {
39+
return require('ipld-ethereum').ethBlockList
40+
},
41+
get 'eth-state-trie' () {
42+
return require('ipld-ethereum').ethStateTrie
43+
},
44+
get 'eth-storage-trie' () {
45+
return require('ipld-ethereum').ethStorageTrie
46+
},
47+
get 'eth-tx' () {
48+
return require('ipld-ethereum').ethTx
49+
},
50+
get 'eth-tx-trie' () {
51+
return require('ipld-ethereum').ethTxTrie
52+
},
53+
get 'git-raw' () {
54+
return require('ipld-git')
55+
},
56+
get 'zcash-block' () {
57+
return require('ipld-zcash')
58+
}
59+
}
60+
4261
class IPFS extends EventEmitter {
4362
constructor (options) {
4463
super()
@@ -99,11 +118,11 @@ class IPFS extends EventEmitter {
99118
this._blockService = new BlockService(this._repo)
100119
this._ipld = new Ipld({
101120
blockService: this._blockService,
102-
formats: [
103-
ipldBitcoin, ipldDagCbor, ipldDagPb, ipldEthAccountSnapshot,
104-
ipldEthBlock, ipldEthBlockList, ipldEthStateTrie, ipldEthStorageTrie,
105-
ipldEthTrie, ipldEthTx, ipldGit, ipldRaw, ipldZcash
106-
]
121+
loadFormat: (codec, callback) => {
122+
this.log('Loading IPLD format', codec)
123+
if (IpldFormats[codec]) return callback(null, IpldFormats[codec])
124+
callback(new Error(`Missing IPLD format "${codec}"`))
125+
}
107126
})
108127
this._preload = preload(this)
109128
this._mfsPreload = mfsPreload(this)

0 commit comments

Comments
 (0)