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

Commit

Permalink
logical-tree: Make it mutate to improve performance
Browse files Browse the repository at this point in the history
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
  • Loading branch information
iarna committed Oct 1, 2015
1 parent 0879682 commit cf42217
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
var clone = require('lodash.clonedeep')
var union = require('lodash.union')
var without = require('lodash.without')
var validate = require('aproba')
Expand All @@ -8,17 +7,15 @@ var isExtraneous = require('./is-extraneous.js')
var validateAllPeerDeps = require('./deps.js').validateAllPeerDeps
var getPackageId = require('./get-package-id.js')

var logicalTree = module.exports = function (tree) {
var mutateIntoLogicalTree = module.exports = function (tree) {
validate('O', arguments)

var newTree = clone(tree)

validateAllPeerDeps(newTree, function (tree, pkgname, version) {
validateAllPeerDeps(tree, function (tree, pkgname, version) {
if (!tree.missingPeers) tree.missingPeers = {}
tree.missingPeers[pkgname] = version
})

var flat = flattenTree(newTree)
var flat = flattenTree(tree)

function getNode (flatname) {
return flatname.substr(0, 5) === '#DEV:' ?
Expand Down Expand Up @@ -47,14 +44,15 @@ var logicalTree = module.exports = function (tree) {
parentNode.children = union(parentNode.children, [node])
})
if (node.package._requiredBy.some(function (nodename) { return nodename[0] === '#' })) {
newTree.children = union(newTree.children, [node])
tree.children = union(tree.children, [node])
}
})
return newTree
return tree
}

module.exports.asReadInstalled = function (tree) {
return translateTree(logicalTree(tree))
mutateIntoLogicalTree(tree)
return translateTree(tree)
}

function translateTree (tree) {
Expand Down
5 changes: 3 additions & 2 deletions lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var color = require('ansicolors')
var npa = require('npm-package-arg')
var iferr = require('iferr')
var npm = require('./npm.js')
var logicalTree = require('./install/logical-tree.js')
var mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
var recalculateMetadata = require('./install/deps.js').recalculateMetadata
var getPackageId = require('./install/get-package-id.js')

Expand Down Expand Up @@ -61,7 +61,8 @@ var lsFromTree = ls.fromTree = function (dir, physicalTree, args, silent, cb) {
})
}

var data = logicalTree.asReadInstalled(physicalTree)
var data = mutateIntoLogicalTree.asReadInstalled(physicalTree)

pruneNestedExtraneous(data)
filterByEnv(data)
var bfs = filterFound(bfsify(data), args)
Expand Down
6 changes: 3 additions & 3 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var styles = require('ansistyles')
var table = require('text-table')
var semver = require('semver')
var npa = require('npm-package-arg')
var logicalTree = require('./install/logical-tree.js')
var mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
var cache = require('./cache.js')
var npm = require('./npm.js')
var long = npm.config.get('long')
Expand Down Expand Up @@ -73,8 +73,8 @@ function outdated (args, silent, cb) {
// default depth for `outdated` is 0 (cf. `ls`)
if (npm.config.get('depth') === Infinity) npm.config.set('depth', 0)

readPackageTree(dir, andRecalculateMetadata(function (er, physicalTree) {
var tree = logicalTree(physicalTree)
readPackageTree(dir, andRecalculateMetadata(function (er, tree) {
mutateIntoLogicalTree(tree)
outdated_(args, '', tree, {}, 0, function (er, list) {
list = uniq(list || []).sort(function (aa, bb) {
return aa[0].path.localeCompare(bb[0].path) ||
Expand Down

0 comments on commit cf42217

Please sign in to comment.