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

Commit d138c95

Browse files
achingbrainvmx
authored andcommitted
perf: make this._json calculation lazy
I've been doing some profiling of js-ipfs and where there are large amounts of DAG operations being performed (importing all of npm to ipfs, for example) we spend about 1.8-2.5% of our time in the creation of this._json objects, even though they are not always used, so this PR makes them be created when first requested. It also passes the `this._json` object through `Object.freeze` becase otherwise we expose a mutable object to the world - it seems odd that we've taken pains to ensure that the properties of a DAGNode/DAGLink are immutable, then expose objects whose properties are not immutable. Finally it makes more use of the `cids` module to parse CIDs from multihashes as the `cids` module respects CIDv1 versions/codecs if present whereas the `multihashes` module does not and is only really suitable for use with CIDv0.
1 parent 97bbf30 commit d138c95

File tree

6 files changed

+34
-40
lines changed

6 files changed

+34
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"cids": "~0.5.3",
6262
"class-is": "^1.1.0",
6363
"is-ipfs": "~0.4.2",
64-
"multihashes": "~0.4.13",
6564
"multihashing-async": "~0.5.1",
6665
"protons": "^1.0.1",
6766
"pull-stream": "^3.6.8",
@@ -78,6 +77,7 @@
7877
"ipfs-block-service": "~0.14.0",
7978
"ipfs-repo": "~0.22.1",
8079
"lodash": "^4.17.10",
80+
"multihashes": "~0.4.13",
8181
"ncp": "^2.0.0",
8282
"rimraf": "^2.6.2"
8383
}

src/dag-link/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,23 @@ class DAGLink {
1414

1515
this._name = name || ''
1616
this._size = size
17-
18-
if (typeof multihash === 'string') {
19-
const cid = new CID(multihash)
20-
this._multihash = cid.buffer
21-
} else if (Buffer.isBuffer(multihash)) {
22-
this._multihash = multihash
23-
}
17+
this._cid = new CID(multihash)
2418
}
2519

2620
toString () {
27-
const cid = new CID(this.multihash)
28-
return `DAGLink <${cid.toBaseEncodedString()} - name: "${this.name}", size: ${this.size}>`
21+
return `DAGLink <${this._cid.toBaseEncodedString()} - name: "${this.name}", size: ${this.size}>`
2922
}
3023

3124
toJSON () {
32-
const cid = new CID(this.multihash)
33-
return {
34-
name: this.name,
35-
size: this.size,
36-
multihash: cid.toBaseEncodedString()
25+
if (!this._json) {
26+
this._json = Object.freeze({
27+
name: this.name,
28+
size: this.size,
29+
multihash: this._cid.toBaseEncodedString()
30+
})
3731
}
32+
33+
return this._json
3834
}
3935

4036
get name () {
@@ -54,7 +50,7 @@ class DAGLink {
5450
}
5551

5652
get multihash () {
57-
return this._multihash
53+
return this._cid.buffer
5854
}
5955

6056
set multihash (multihash) {

src/dag-node/addLink.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ function addLink (node, link, callback) {
2121
link = toDAGLink(link)
2222
} else {
2323
// It's a Object with name, multihash/link and size
24-
link.multihash = link.multihash || link.hash
2524
try {
26-
link = new DAGLink(link.name, link.size, link.multihash)
25+
link = new DAGLink(link.name, link.size, link.multihash || link.hash)
2726
} catch (err) {
2827
return callback(err)
2928
}

src/dag-node/index.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
'use strict'
22

3-
const mh = require('multihashes')
43
const assert = require('assert')
54
const withIs = require('class-is')
5+
const CID = require('cids')
66

77
class DAGNode {
88
constructor (data, links, serialized, multihash) {
99
assert(serialized, 'DAGNode needs its serialized format')
1010
assert(multihash, 'DAGNode needs its multihash')
1111

12-
if (typeof multihash === 'string') {
13-
multihash = mh.fromB58String(multihash)
14-
}
15-
12+
this._cid = new CID(multihash)
1613
this._data = data || Buffer.alloc(0)
1714
this._links = links || []
1815
this._serialized = serialized
19-
this._multihash = multihash
20-
21-
this._size = this.links.reduce((sum, l) => sum + l.size, this.serialized.length)
22-
23-
this._json = {
24-
data: this.data,
25-
links: this.links.map((l) => l.toJSON()),
26-
multihash: mh.toB58String(this.multihash),
27-
size: this.size
28-
}
2916
}
3017

3118
toJSON () {
19+
if (!this._json) {
20+
this._json = Object.freeze({
21+
data: this.data,
22+
links: this.links.map((l) => l.toJSON()),
23+
multihash: this._cid.toBaseEncodedString(),
24+
size: this.size
25+
})
26+
}
27+
3228
return this._json
3329
}
3430

3531
toString () {
36-
const mhStr = mh.toB58String(this.multihash)
37-
return `DAGNode <${mhStr} - data: "${this.data.toString()}", links: ${this.links.length}, size: ${this.size}>`
32+
return `DAGNode <${this._cid.toBaseEncodedString()} - data: "${this.data.toString()}", links: ${this.links.length}, size: ${this.size}>`
3833
}
3934

4035
get data () {
@@ -62,6 +57,10 @@ class DAGNode {
6257
}
6358

6459
get size () {
60+
if (this._size === undefined) {
61+
this._size = this.links.reduce((sum, l) => sum + l.size, this.serialized.length)
62+
}
63+
6564
return this._size
6665
}
6766

@@ -70,7 +69,7 @@ class DAGNode {
7069
}
7170

7271
get multihash () {
73-
return this._multihash
72+
return this._cid.buffer
7473
}
7574

7675
set multihash (multihash) {

test/dag-link-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const chai = require('chai')
55
const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
8-
const mh = require('multihashes')
8+
const CID = require('cids')
99
const DAGLink = require('../src').DAGLink
1010

1111
module.exports = (repo) => {
@@ -26,7 +26,7 @@ module.exports = (repo) => {
2626
it('create with multihash as a multihash Buffer', () => {
2727
const link = new DAGLink('hello', 3, Buffer.from('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43', 'hex'))
2828

29-
expect(mh.toB58String(link.multihash))
29+
expect(new CID(link.multihash).toBaseEncodedString())
3030
.to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
3131
})
3232

test/dag-node-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ module.exports = (repo) => {
362362
})
363363
},
364364
(cb) => {
365-
const link = toDAGLink(node2).toJSON()
365+
const link = Object.assign({}, toDAGLink(node2).toJSON())
366366
link.name = 'banana'
367367

368368
DAGNode.addLink(node1a, link, (err, node) => {
@@ -402,7 +402,7 @@ module.exports = (repo) => {
402402
})
403403
},
404404
(cb) => {
405-
const link = toDAGLink(node2).toJSON()
405+
const link = Object.assign({}, toDAGLink(node2).toJSON())
406406
link.name = 'banana'
407407

408408
DAGNode.addLink(node1a, link, (err, node) => {

0 commit comments

Comments
 (0)