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

perf: make this._json calculation lazy #81

Merged
merged 1 commit into from
Aug 13, 2018
Merged

Conversation

achingbrain
Copy link
Member

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 because 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.

@codecov
Copy link

codecov bot commented Aug 2, 2018

Codecov Report

Merging #81 into master will increase coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #81      +/-   ##
==========================================
+ Coverage   90.87%   91.04%   +0.16%     
==========================================
  Files          13       13              
  Lines         274      268       -6     
==========================================
- Hits          249      244       -5     
+ Misses         25       24       -1
Impacted Files Coverage Δ
src/dag-node/addLink.js 94.44% <100%> (-0.3%) ⬇️
src/dag-node/index.js 83.87% <100%> (+2.62%) ⬆️
src/dag-link/index.js 85% <100%> (-2.5%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 97bbf30...d9e0e55. Read the comment docs.

@@ -78,6 +77,7 @@
"ipfs-block-service": "~0.14.0",
"ipfs-repo": "~0.22.1",
"lodash": "^4.17.10",
"multihashes": "~0.4.13",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think multihashes is no needed after this very good change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still being used in a test which is why it's moved from dependencies to devDependencies.

Copy link
Member

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jolly good.

@olizilla
Copy link
Member

olizilla commented Aug 2, 2018

@achingbrain @vmx @diasdavid as per #80 (comment) are there any objections to providing a cid getter on DagLink and DagNode?

Ideally we'd remove the mutlihash property and release as a major version, having both props and a deprecation warning seems more mess than helpful, though I could be wrong.

I'm already normalising all IPLD nodes to have a cid property in IPLD explorer https://github.com/ipfs-shipyard/ipld-explorer/blob/14fee7a65773b32c78c82ff58ce99c70c9d28a81/src/lib/dag.js#L27-L51

@achingbrain
Copy link
Member Author

are there any objections to providing a cid getter on DagLink and DagNode?

Fine by me. It'd be good to get people using CIDs instead of multihashes as they aren't the same thing in this brave new CIDv1 world.

Would you have it return the object or the buffer?

@olizilla
Copy link
Member

olizilla commented Aug 2, 2018

I had imagined it'd return the buffer to be a more minimal change. In the common case of cidv0 it'd mean the usage site just has to rename the property.

I do have to upgrade the buffers to CID objects all over the place right now, so it'd help to use CID objects over buffers in the object representations, but we would have to review all the IPLD node types.

I think we should do the smaller change now... renaming multihash to cid but have it still return a Buffer. Then as a more awesome endeavour, we should figure out if all links in all nodes should be represented as a CID object rather than a Buffer.

@achingbrain
Copy link
Member Author

achingbrain commented Aug 2, 2018 via email

@olizilla
Copy link
Member

olizilla commented Aug 2, 2018

Compelling argument @achingbrain. I don't have a feel for the direction the IPLD interfaces are supposed to be moving in, I had assumed that using Buffers all over the place was deliberate and I just hadn't found the repo that explained it yet, but I do agree it doesn't make for an intuitive API for people that want to traverse IPLD nodes.

Vaguely related to this is the discussion around what is the standard pattern for the IPLD transform that we apply when representing entities from other systems as objects ipld/js-ipld-ethereum#25

@vmx
Copy link
Member

vmx commented Aug 6, 2018

@achingbrain @olizilla It's funny that the CID object vs. Buffer comes up as @mikeal and I were discussing the same thing during the DWeb Summit. Combining that discussion with yours above I'd say the outcome would be that we have a new cid field which contains the CID object.

Please everyone mentioned in this comment give a thumb up if you agree, or post a comment if you don't. It'll then be a separate change, independent of this one.

Update: Reference to the CID discussions: ipld/ipld#44

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.
@achingbrain achingbrain force-pushed the make-json-serialization-lazy branch from 6c8ed4d to d9e0e55 Compare August 7, 2018 12:11
@achingbrain
Copy link
Member Author

This is the sort of thing I am seeing while importing npm to IPFS without this PR:

image

The other really long flat bar is the garbage collector.

Flame graph from appmetrics-dash.

@achingbrain
Copy link
Member Author

Can it be merged & released?

@vmx vmx merged commit d138c95 into master Aug 13, 2018
@vmx vmx deleted the make-json-serialization-lazy branch August 13, 2018 15:06
@vmx
Copy link
Member

vmx commented Aug 13, 2018

@achingbrain Sorry this somehow slipped my attention. I've also done a release.

achingbrain added a commit that referenced this pull request Aug 13, 2018
Relaxes the constraints from #81 a little as it's
caused quite a lot of tests to break.

We still have an immutable copy in the object but any objects
returned will be mutable.
vmx pushed a commit that referenced this pull request Aug 13, 2018
Relaxes the constraints from #81 a little as it's
caused quite a lot of tests to break.

We still have an immutable copy in the object but any objects
returned will be mutable.
alanshaw added a commit to ipfs/js-ipfs that referenced this pull request Aug 15, 2018
...which was removed in ipld/js-ipld-dag-pb#81

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
alanshaw added a commit to ipfs/js-ipfs that referenced this pull request Aug 15, 2018
This PR contains fixes for the test failures that are happening in master for `object.patch.rmLink`.

I **think** this change ipld/js-ipld-dag-pb#80 made `DAGLink` validate the hash better and caused the failures to start.

It also contains fixes for the `pinSet` module which was using the "private" `_multihash` property which was removed in ipld/js-ipld-dag-pb#81 and released 2 days ago - don't use private APIs kids.

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
achingbrain added a commit that referenced this pull request Oct 9, 2018
vmx pushed a commit that referenced this pull request Oct 12, 2018
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 this pull request may close these issues.

3 participants