Skip to content

Commit 0f06f18

Browse files
committed
Remove LRU caching
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.
1 parent 66681f4 commit 0f06f18

File tree

3 files changed

+13
-219
lines changed

3 files changed

+13
-219
lines changed

minimatch.js

+12-202
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module.exports = minimatch
22
minimatch.Minimatch = Minimatch
33

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

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

10098
function Minimatch (pattern, options) {
10199
if (!(this instanceof Minimatch)) {
102-
return new Minimatch(pattern, options, cache)
100+
return new Minimatch(pattern, options)
103101
}
104102

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

112-
// lru storage.
113-
// these things aren't particularly big, but walking down the string
114-
// and turning it into a regexp can get pretty costly.
115-
var cacheKey = pattern + "\n" + sigmund(options)
116-
var cached = minimatch.cache.get(cacheKey)
117-
if (cached) return cached
118-
minimatch.cache.set(cacheKey, this)
119-
120110
this.options = options
121111
this.set = []
122112
this.pattern = pattern
@@ -218,19 +208,19 @@ function parseNegate () {
218208
// a{2..}b -> a{2..}b
219209
// a{b}c -> a{b}c
220210
minimatch.braceExpand = function (pattern, options) {
221-
return new Minimatch(pattern, options).braceExpand()
211+
return braceExpand(pattern, options)
222212
}
223213

224214
Minimatch.prototype.braceExpand = braceExpand
225215

226-
function pad(n, width, z) {
227-
z = z || '0';
228-
n = n + '';
229-
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
230-
}
231-
232216
function braceExpand (pattern, options) {
233-
options = options || this.options
217+
if (!options) {
218+
if (this instanceof Minimatch)
219+
options = this.options
220+
else
221+
options = {}
222+
}
223+
234224
pattern = typeof pattern === "undefined"
235225
? this.pattern : pattern
236226

@@ -244,187 +234,7 @@ function braceExpand (pattern, options) {
244234
return [pattern]
245235
}
246236

247-
var escaping = false
248-
249-
// examples and comments refer to this crazy pattern:
250-
// a{b,c{d,e},{f,g}h}x{y,z}
251-
// expected:
252-
// abxy
253-
// abxz
254-
// acdxy
255-
// acdxz
256-
// acexy
257-
// acexz
258-
// afhxy
259-
// afhxz
260-
// aghxy
261-
// aghxz
262-
263-
// everything before the first \{ is just a prefix.
264-
// So, we pluck that off, and work with the rest,
265-
// and then prepend it to everything we find.
266-
if (pattern.charAt(0) !== "{") {
267-
this.debug(pattern)
268-
var prefix = null
269-
for (var i = 0, l = pattern.length; i < l; i ++) {
270-
var c = pattern.charAt(i)
271-
this.debug(i, c)
272-
if (c === "\\") {
273-
escaping = !escaping
274-
} else if (c === "{" && !escaping) {
275-
prefix = pattern.substr(0, i)
276-
break
277-
}
278-
}
279-
280-
// actually no sets, all { were escaped.
281-
if (prefix === null) {
282-
this.debug("no sets")
283-
return [pattern]
284-
}
285-
286-
var tail = braceExpand.call(this, pattern.substr(i), options)
287-
return tail.map(function (t) {
288-
return prefix + t
289-
})
290-
}
291-
292-
// now we have something like:
293-
// {b,c{d,e},{f,g}h}x{y,z}
294-
// walk through the set, expanding each part, until
295-
// the set ends. then, we'll expand the suffix.
296-
// If the set only has a single member, then'll put the {} back
297-
298-
// first, handle numeric sets, since they're easier
299-
var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
300-
if (numset) {
301-
this.debug("numset", numset[1], numset[2])
302-
var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
303-
, start = +numset[1]
304-
, needPadding = numset[1][0] === '0'
305-
, startWidth = numset[1].length
306-
, padded
307-
, end = +numset[2]
308-
, inc = start > end ? -1 : 1
309-
, set = []
310-
311-
for (var i = start; i != (end + inc); i += inc) {
312-
padded = needPadding ? pad(i, startWidth) : i + ''
313-
// append all the suffixes
314-
for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
315-
set.push(padded + suf[ii])
316-
}
317-
}
318-
return set
319-
}
320-
321-
// ok, walk through the set
322-
// We hope, somewhat optimistically, that there
323-
// will be a } at the end.
324-
// If the closing brace isn't found, then the pattern is
325-
// interpreted as braceExpand("\\" + pattern) so that
326-
// the leading \{ will be interpreted literally.
327-
var i = 1 // skip the \{
328-
, depth = 1
329-
, set = []
330-
, member = ""
331-
, sawEnd = false
332-
, escaping = false
333-
334-
function addMember () {
335-
set.push(member)
336-
member = ""
337-
}
338-
339-
this.debug("Entering for")
340-
FOR: for (i = 1, l = pattern.length; i < l; i ++) {
341-
var c = pattern.charAt(i)
342-
this.debug("", i, c)
343-
344-
if (escaping) {
345-
escaping = false
346-
member += "\\" + c
347-
} else {
348-
switch (c) {
349-
case "\\":
350-
escaping = true
351-
continue
352-
353-
case "{":
354-
depth ++
355-
member += "{"
356-
continue
357-
358-
case "}":
359-
depth --
360-
// if this closes the actual set, then we're done
361-
if (depth === 0) {
362-
addMember()
363-
// pluck off the close-brace
364-
i ++
365-
break FOR
366-
} else {
367-
member += c
368-
continue
369-
}
370-
371-
case ",":
372-
if (depth === 1) {
373-
addMember()
374-
} else {
375-
member += c
376-
}
377-
continue
378-
379-
default:
380-
member += c
381-
continue
382-
} // switch
383-
} // else
384-
} // for
385-
386-
// now we've either finished the set, and the suffix is
387-
// pattern.substr(i), or we have *not* closed the set,
388-
// and need to escape the leading brace
389-
if (depth !== 0) {
390-
this.debug("didn't close", pattern)
391-
return braceExpand.call(this, "\\" + pattern, options)
392-
}
393-
394-
// x{y,z} -> ["xy", "xz"]
395-
this.debug("set", set)
396-
this.debug("suffix", pattern.substr(i))
397-
var suf = braceExpand.call(this, pattern.substr(i), options)
398-
// ["b", "c{d,e}","{f,g}h"] ->
399-
// [["b"], ["cd", "ce"], ["fh", "gh"]]
400-
var addBraces = set.length === 1
401-
this.debug("set pre-expanded", set)
402-
set = set.map(function (p) {
403-
return braceExpand.call(this, p, options)
404-
}, this)
405-
this.debug("set expanded", set)
406-
407-
408-
// [["b"], ["cd", "ce"], ["fh", "gh"]] ->
409-
// ["b", "cd", "ce", "fh", "gh"]
410-
set = set.reduce(function (l, r) {
411-
return l.concat(r)
412-
})
413-
414-
if (addBraces) {
415-
set = set.map(function (s) {
416-
return "{" + s + "}"
417-
})
418-
}
419-
420-
// now attach the suffixes.
421-
var ret = []
422-
for (var i = 0, l = set.length; i < l; i ++) {
423-
for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
424-
ret.push(set[i] + suf[ii])
425-
}
426-
}
427-
return ret
237+
return expand(pattern)
428238
}
429239

430240
// parse a component of the expanded set.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
"node": "*"
1717
},
1818
"dependencies": {
19-
"brace-expansion": "^1.0.0",
20-
"lru-cache": "2",
21-
"sigmund": "~1.0.0"
19+
"brace-expansion": "^1.0.0"
2220
},
2321
"devDependencies": {
2422
"browserify": "^6.3.3",

test/caching.js

-14
This file was deleted.

0 commit comments

Comments
 (0)