Skip to content

Commit

Permalink
Optimizes looking for at least one Ruby file
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed May 26, 2024
1 parent b525643 commit 105b16b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/zeitwerk/loader/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,27 @@ module Zeitwerk::Loader::Helpers
end
end

# Looks for a Ruby file using breadth-first search. This type of search is
# important to list as less directories as possible and return fast in the
# common case in which there are Ruby files.
#
# @sig (String) -> bool
private def has_at_least_one_ruby_file?(dir)
to_visit = [dir]

until to_visit.empty?
dir = to_visit.shift
while (dir = to_visit.shift)
children = Dir.children(dir)

children.each do |basename|
next if hidden?(basename)

abspath = File.join(dir, basename)
next if ignored_path?(abspath)

ls(dir) do |_basename, abspath, ftype|
if ftype == :file
return true
if dir?(abspath)
to_visit << abspath unless roots.key?(abspath)
else
to_visit << abspath
return true if ruby?(abspath)
end
end
end
Expand Down

0 comments on commit 105b16b

Please sign in to comment.