Skip to content

Commit

Permalink
Remove LRU caching
Browse files Browse the repository at this point in the history
Creating the minimatch object is usually slower than the cost to look it
up in an LRU.  No point.

This also means that we no longer need sigmund.
  • Loading branch information
isaacs committed Dec 1, 2014
1 parent 66681f4 commit 0f06f18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 219 deletions.
214 changes: 12 additions & 202 deletions minimatch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module.exports = minimatch
minimatch.Minimatch = Minimatch

var LRU = require("lru-cache")
, cache = minimatch.cache = new LRU({max: 100})
, GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
, sigmund = require("sigmund")
var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
, expand = require("brace-expansion")

// any single thing other than /
// don't need to escape / when using new RegExp()
Expand Down Expand Up @@ -99,7 +97,7 @@ function minimatch (p, pattern, options) {

function Minimatch (pattern, options) {
if (!(this instanceof Minimatch)) {
return new Minimatch(pattern, options, cache)
return new Minimatch(pattern, options)
}

if (typeof pattern !== "string") {
Expand All @@ -109,14 +107,6 @@ function Minimatch (pattern, options) {
if (!options) options = {}
pattern = pattern.trim()

// lru storage.
// these things aren't particularly big, but walking down the string
// and turning it into a regexp can get pretty costly.
var cacheKey = pattern + "\n" + sigmund(options)
var cached = minimatch.cache.get(cacheKey)
if (cached) return cached
minimatch.cache.set(cacheKey, this)

this.options = options
this.set = []
this.pattern = pattern
Expand Down Expand Up @@ -218,19 +208,19 @@ function parseNegate () {
// a{2..}b -> a{2..}b
// a{b}c -> a{b}c
minimatch.braceExpand = function (pattern, options) {
return new Minimatch(pattern, options).braceExpand()
return braceExpand(pattern, options)
}

Minimatch.prototype.braceExpand = braceExpand

function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

function braceExpand (pattern, options) {
options = options || this.options
if (!options) {
if (this instanceof Minimatch)
options = this.options
else
options = {}
}

pattern = typeof pattern === "undefined"
? this.pattern : pattern

Expand All @@ -244,187 +234,7 @@ function braceExpand (pattern, options) {
return [pattern]
}

var escaping = false

// examples and comments refer to this crazy pattern:
// a{b,c{d,e},{f,g}h}x{y,z}
// expected:
// abxy
// abxz
// acdxy
// acdxz
// acexy
// acexz
// afhxy
// afhxz
// aghxy
// aghxz

// everything before the first \{ is just a prefix.
// So, we pluck that off, and work with the rest,
// and then prepend it to everything we find.
if (pattern.charAt(0) !== "{") {
this.debug(pattern)
var prefix = null
for (var i = 0, l = pattern.length; i < l; i ++) {
var c = pattern.charAt(i)
this.debug(i, c)
if (c === "\\") {
escaping = !escaping
} else if (c === "{" && !escaping) {
prefix = pattern.substr(0, i)
break
}
}

// actually no sets, all { were escaped.
if (prefix === null) {
this.debug("no sets")
return [pattern]
}

var tail = braceExpand.call(this, pattern.substr(i), options)
return tail.map(function (t) {
return prefix + t
})
}

// now we have something like:
// {b,c{d,e},{f,g}h}x{y,z}
// walk through the set, expanding each part, until
// the set ends. then, we'll expand the suffix.
// If the set only has a single member, then'll put the {} back

// first, handle numeric sets, since they're easier
var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
if (numset) {
this.debug("numset", numset[1], numset[2])
var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
, start = +numset[1]
, needPadding = numset[1][0] === '0'
, startWidth = numset[1].length
, padded
, end = +numset[2]
, inc = start > end ? -1 : 1
, set = []

for (var i = start; i != (end + inc); i += inc) {
padded = needPadding ? pad(i, startWidth) : i + ''
// append all the suffixes
for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
set.push(padded + suf[ii])
}
}
return set
}

// ok, walk through the set
// We hope, somewhat optimistically, that there
// will be a } at the end.
// If the closing brace isn't found, then the pattern is
// interpreted as braceExpand("\\" + pattern) so that
// the leading \{ will be interpreted literally.
var i = 1 // skip the \{
, depth = 1
, set = []
, member = ""
, sawEnd = false
, escaping = false

function addMember () {
set.push(member)
member = ""
}

this.debug("Entering for")
FOR: for (i = 1, l = pattern.length; i < l; i ++) {
var c = pattern.charAt(i)
this.debug("", i, c)

if (escaping) {
escaping = false
member += "\\" + c
} else {
switch (c) {
case "\\":
escaping = true
continue

case "{":
depth ++
member += "{"
continue

case "}":
depth --
// if this closes the actual set, then we're done
if (depth === 0) {
addMember()
// pluck off the close-brace
i ++
break FOR
} else {
member += c
continue
}

case ",":
if (depth === 1) {
addMember()
} else {
member += c
}
continue

default:
member += c
continue
} // switch
} // else
} // for

// now we've either finished the set, and the suffix is
// pattern.substr(i), or we have *not* closed the set,
// and need to escape the leading brace
if (depth !== 0) {
this.debug("didn't close", pattern)
return braceExpand.call(this, "\\" + pattern, options)
}

// x{y,z} -> ["xy", "xz"]
this.debug("set", set)
this.debug("suffix", pattern.substr(i))
var suf = braceExpand.call(this, pattern.substr(i), options)
// ["b", "c{d,e}","{f,g}h"] ->
// [["b"], ["cd", "ce"], ["fh", "gh"]]
var addBraces = set.length === 1
this.debug("set pre-expanded", set)
set = set.map(function (p) {
return braceExpand.call(this, p, options)
}, this)
this.debug("set expanded", set)


// [["b"], ["cd", "ce"], ["fh", "gh"]] ->
// ["b", "cd", "ce", "fh", "gh"]
set = set.reduce(function (l, r) {
return l.concat(r)
})

if (addBraces) {
set = set.map(function (s) {
return "{" + s + "}"
})
}

// now attach the suffixes.
var ret = []
for (var i = 0, l = set.length; i < l; i ++) {
for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
ret.push(set[i] + suf[ii])
}
}
return ret
return expand(pattern)
}

// parse a component of the expanded set.
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
"node": "*"
},
"dependencies": {
"brace-expansion": "^1.0.0",
"lru-cache": "2",
"sigmund": "~1.0.0"
"brace-expansion": "^1.0.0"
},
"devDependencies": {
"browserify": "^6.3.3",
Expand Down
14 changes: 0 additions & 14 deletions test/caching.js

This file was deleted.

0 comments on commit 0f06f18

Please sign in to comment.