[Rake-devel] [PATCH] not using Dir.glob in FileList.exclude

Ittay Dror ittay.dror at gmail.com
Wed Nov 5 02:58:10 EST 2008


This patch removes the use of Dir#[] when calculating excludes from a 
FileList object.

Using Dir#[] scans the filesystem for matching files which can both 
create a performance problem and be inaccurate, in case I want to 
exclude a non existing patterns, or Dir.chdir was called since the 
FileList was generated.

The patch instead uses File#fnmatch? to match the pattern to the list of 
files. The patch also removes calculate_exclude_regexp and instead does 
all work in #exclude?, the assumption being that it is not normal to 
have many exclude patterns anyway nor that their grouping into one 
regular expression improves performance greatly.

Ittay


Signed-off-by: Ittay Dror <ittay.dror at gmail.com>
---
lib/rake.rb |   37 ++++++++++++-------------------------
 1 files changed, 12 insertions(+), 25 deletions(-)

diff --git a/lib/rake.rb b/lib/rake.rb
index 5ce88e8..ce09cc6 100755
--- a/lib/rake.rb
+++ b/lib/rake.rb
@@ -1273,7 +1273,6 @@ module Rake
       @pending = false
       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
       @exclude_procs = DEFAULT_IGNORE_PROCS.dup
-      @exclude_re = nil
       @items = []
       patterns.each { |pattern| include(pattern) }
       yield self if block_given?
@@ -1336,7 +1335,6 @@ module Rake
     def clear_exclude
       @exclude_patterns = []
       @exclude_procs = []
-      calculate_exclude_regexp if ! @pending
       self
     end
 
@@ -1384,26 +1382,6 @@ module Rake
       self
     end
 
-    def calculate_exclude_regexp
-      ignores = []
-      @exclude_patterns.each do |pat|
-        case pat
-        when Regexp
-          ignores << pat
-        when /[*?]/
-          Dir[pat].each do |p| ignores << p end
-        else
-          ignores << Regexp.quote(pat)
-        end
-      end
-      if ignores.empty?
-        @exclude_re = /^$/
-      else
-        re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
-        @exclude_re = Regexp.new(re_str)
-      end
-    end
-
     def resolve_add(fn)
       case fn
       when %r{[*?\[\{]}
@@ -1415,7 +1393,6 @@ module Rake
     private :resolve_add
 
     def resolve_exclude
-      calculate_exclude_regexp
       reject! { |fn| exclude?(fn) }
       self
     end
@@ -1538,8 +1515,18 @@ module Rake
 
     # Should the given file name be excluded?
     def exclude?(fn)
-      calculate_exclude_regexp unless @exclude_re
-      fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
+      return true if @exclude_patterns.any? do |pat|
+        case pat
+        when Regexp
+          fn =~ pat
+        when /[*?]/
+          File.fnmatch? pat, fn, File::FNM_PATHNAME
+        else
+          fn == pat
+        end
+      end
+
+      @exclude_procs.any? { |p| p.call(fn) }
     end
 
     DEFAULT_IGNORE_PATTERNS = [
-- 
1.6.0.36.g3814c

-- 
--
Ittay Dror <ittay.dror at gmail.com>




More information about the Rake-devel mailing list