Skip to content

Commit

Permalink
Improves performance of file.expand, file.expandMapping, and `tas…
Browse files Browse the repository at this point in the history
…k.normalizeMultiTaskFiles` by caching glob fs results across multiple calls to `glob.sync`
  • Loading branch information
cspotcode committed Nov 8, 2016
1 parent a09f84c commit 8755cdd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ file.expand = function() {
// If the first argument is an options object, save those options to pass
// into the file.glob.sync method.
var options = grunt.util.kindOf(args[0]) === 'object' ? args.shift() : {};
// Cache fs calls across multiple file.glob.sync operations
var globOptions = grunt.util._.extend(file._createGlobIoCache(), options);
// Use the first argument if it's an Array, otherwise convert the arguments
// object to an array and use that.
var patterns = Array.isArray(args[0]) ? args[0] : args;
Expand All @@ -101,7 +103,7 @@ file.expand = function() {
// Return all matching filepaths.
var matches = processPatterns(patterns, function(pattern) {
// Find all matching files for this pattern.
return file.glob.sync(pattern, options);
return file.glob.sync(pattern, globOptions);
});
// Filter result set?
if (options.filter) {
Expand Down Expand Up @@ -452,3 +454,14 @@ file.isPathInCwd = function() {
return false;
}
};

// Creates all glob options that store cached fs results for the `glob` library.
// Can be removed when this glob PR is released: https://github.com/isaacs/node-glob/pull/306
file._createGlobIoCache = function() {
return {
cache: Object.create(null),
statCache: Object.create(null),
symlinks: Object.create(null),
realpathCache: Object.create(null)
};
};
3 changes: 2 additions & 1 deletion lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
}

// Process all normalized file objects.
var globIoCache = grunt.file._createGlobIoCache();
files = grunt.util._(files).chain().forEach(function(obj) {
if (!('src' in obj) || !obj.src) { return; }
// Normalize .src properties to flattened array.
Expand All @@ -127,7 +128,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
}
}).map(function(obj) {
// Build options object, removing unwanted properties.
var expandOptions = grunt.util._.extend({}, obj);
var expandOptions = grunt.util._.extend({}, globIoCache, obj);
delete expandOptions.src;
delete expandOptions.dest;

Expand Down

0 comments on commit 8755cdd

Please sign in to comment.