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

Commit cf42217

Browse files
committed
logical-tree: Make it mutate to improve performance
The clone we were doing to save ourselves from mutation proved to be excessively slow. While lodash is being updated to not have the same scaling issues, it was determined that we weren't gaining anything from not mutating here, so there was no reason to pay even a lesser price. PR-URL: #9803 Fixes: #8826
1 parent 0879682 commit cf42217

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

lib/install/logical-tree.js lib/install/mutate-into-logical-tree.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict'
2-
var clone = require('lodash.clonedeep')
32
var union = require('lodash.union')
43
var without = require('lodash.without')
54
var validate = require('aproba')
@@ -8,17 +7,15 @@ var isExtraneous = require('./is-extraneous.js')
87
var validateAllPeerDeps = require('./deps.js').validateAllPeerDeps
98
var getPackageId = require('./get-package-id.js')
109

11-
var logicalTree = module.exports = function (tree) {
10+
var mutateIntoLogicalTree = module.exports = function (tree) {
1211
validate('O', arguments)
1312

14-
var newTree = clone(tree)
15-
16-
validateAllPeerDeps(newTree, function (tree, pkgname, version) {
13+
validateAllPeerDeps(tree, function (tree, pkgname, version) {
1714
if (!tree.missingPeers) tree.missingPeers = {}
1815
tree.missingPeers[pkgname] = version
1916
})
2017

21-
var flat = flattenTree(newTree)
18+
var flat = flattenTree(tree)
2219

2320
function getNode (flatname) {
2421
return flatname.substr(0, 5) === '#DEV:' ?
@@ -47,14 +44,15 @@ var logicalTree = module.exports = function (tree) {
4744
parentNode.children = union(parentNode.children, [node])
4845
})
4946
if (node.package._requiredBy.some(function (nodename) { return nodename[0] === '#' })) {
50-
newTree.children = union(newTree.children, [node])
47+
tree.children = union(tree.children, [node])
5148
}
5249
})
53-
return newTree
50+
return tree
5451
}
5552

5653
module.exports.asReadInstalled = function (tree) {
57-
return translateTree(logicalTree(tree))
54+
mutateIntoLogicalTree(tree)
55+
return translateTree(tree)
5856
}
5957

6058
function translateTree (tree) {

lib/ls.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var color = require('ansicolors')
1616
var npa = require('npm-package-arg')
1717
var iferr = require('iferr')
1818
var npm = require('./npm.js')
19-
var logicalTree = require('./install/logical-tree.js')
19+
var mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
2020
var recalculateMetadata = require('./install/deps.js').recalculateMetadata
2121
var getPackageId = require('./install/get-package-id.js')
2222

@@ -61,7 +61,8 @@ var lsFromTree = ls.fromTree = function (dir, physicalTree, args, silent, cb) {
6161
})
6262
}
6363

64-
var data = logicalTree.asReadInstalled(physicalTree)
64+
var data = mutateIntoLogicalTree.asReadInstalled(physicalTree)
65+
6566
pruneNestedExtraneous(data)
6667
filterByEnv(data)
6768
var bfs = filterFound(bfsify(data), args)

lib/outdated.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var styles = require('ansistyles')
3232
var table = require('text-table')
3333
var semver = require('semver')
3434
var npa = require('npm-package-arg')
35-
var logicalTree = require('./install/logical-tree.js')
35+
var mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
3636
var cache = require('./cache.js')
3737
var npm = require('./npm.js')
3838
var long = npm.config.get('long')
@@ -73,8 +73,8 @@ function outdated (args, silent, cb) {
7373
// default depth for `outdated` is 0 (cf. `ls`)
7474
if (npm.config.get('depth') === Infinity) npm.config.set('depth', 0)
7575

76-
readPackageTree(dir, andRecalculateMetadata(function (er, physicalTree) {
77-
var tree = logicalTree(physicalTree)
76+
readPackageTree(dir, andRecalculateMetadata(function (er, tree) {
77+
mutateIntoLogicalTree(tree)
7878
outdated_(args, '', tree, {}, 0, function (er, list) {
7979
list = uniq(list || []).sort(function (aa, bb) {
8080
return aa[0].path.localeCompare(bb[0].path) ||

0 commit comments

Comments
 (0)