1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

let filter_out_descendants do less passes

Whatever the inner loop selects, we already know is a descendant and
can be filtered out right away from dirs_sorted_by_nparts to skip
useless iterations.
This commit is contained in:
Xavier Noria 2015-11-11 21:35:34 +01:00
parent 5da4be67a0
commit 18b44ff1e4

View file

@ -130,22 +130,22 @@ module ActiveSupport
end end
# Filters out directories which are descendants of others in the collection (stable). # Filters out directories which are descendants of others in the collection (stable).
def filter_out_descendants(directories) def filter_out_descendants(dirs)
return directories if directories.length < 2 return dirs if dirs.length < 2
sorted_by_nparts = directories.sort_by { |dir| dir.each_filename.to_a.length } dirs_sorted_by_nparts = dirs.sort_by { |dir| dir.each_filename.to_a.length }
descendants = [] descendants = []
until sorted_by_nparts.empty? until dirs_sorted_by_nparts.empty?
dir = sorted_by_nparts.shift dir = dirs_sorted_by_nparts.shift
descendants.concat sorted_by_nparts.select { |possible_descendant| dirs_sorted_by_nparts.reject! do |possible_descendant|
dir.ascendant_of?(possible_descendant) dir.ascendant_of?(possible_descendant) && descendants << possible_descendant
} end
end end
# Array#- preserves order. # Array#- preserves order.
directories - descendants dirs - descendants
end end
end end
end end