Skip to content

Commit

Permalink
Merge pull request #4434 from rolandwalker/list_cmd_perf
Browse files Browse the repository at this point in the history
Improve performance of `brew cask list`
  • Loading branch information
rolandwalker committed May 16, 2014
2 parents 20cd41c + 5b2e9d1 commit 7e26f8f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def self.set_up_taps
ohai 'Adding caskroom Tap'
Homebrew.install_tap(new_user, repo)
end
Cask.reset_all_tapped_cask_dirs
end

def self.load(query)
Expand Down
5 changes: 3 additions & 2 deletions lib/cask/cli/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ def self.list_files(cask)
end

def self.list_installed
columns = Cask.installed.map(&:to_s)
installed_casks = Cask.installed
columns = installed_casks.map(&:to_s)
puts_columns columns
columns.empty? ? nil : Cask.installed.length == columns.length
columns.empty? ? nil : installed_casks.length == columns.length
end

def self.help
Expand Down
33 changes: 30 additions & 3 deletions lib/cask/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@ def all
all_titles.map { |c| self.load c }
end

def all_tapped_cask_dirs
return @all_tapped_cask_dirs unless @all_tapped_cask_dirs.nil?
fq_default_tap = tapspath.join(default_tap, 'Casks')
@all_tapped_cask_dirs = Dir.glob(tapspath.join('*', '*', 'Casks')).map { |d| Pathname.new(d) }
# optimization: place the default Tap first
if @all_tapped_cask_dirs.include? fq_default_tap
@all_tapped_cask_dirs = @all_tapped_cask_dirs - [ fq_default_tap ]
@all_tapped_cask_dirs.unshift fq_default_tap
end
@all_tapped_cask_dirs
end

def reset_all_tapped_cask_dirs
# The memoized value should be reset when a Tap is added/removed
# (which is a rare event in our codebase).
@all_tapped_cask_dirs = nil
end

def all_titles
cask_titles = Dir[tapspath.join('*', '*', 'Casks', '*.rb')]
cask_titles = all_tapped_cask_dirs.map { |d| Dir.glob d.join('*.rb') }.flatten
cask_titles.map { |c|
# => "/usr/local/Library/Taps/caskroom/example-tap/Casks/example.rb"
c.sub!(/\.rb$/, '')
Expand All @@ -24,8 +42,17 @@ def all_titles

def installed
installed_cask_dirs = Pathname.glob(caskroom.join("*"))
installed_cask_dirs.map do |dir|
Cask.load(dir.basename.to_s)
# Cask.load has some DWIM which is slow. Optimize here
# by spoon-feeding Cask.load fully-qualified paths.
# todo: speed up Cask::Source::Tapped (main perf drag is calling Cask.all_titles repeatedly)
# todo: ability to specify expected source when calling Cask.load (minor perf benefit)
installed_cask_dirs.map do |install_dir|
cask_name = install_dir.basename.to_s
path_to_cask = all_tapped_cask_dirs.find do |tap_dir|
tap_dir.join("#{cask_name}.rb").exist?
end
cask_name = path_to_cask.join("#{cask_name}.rb") if path_to_cask
Cask.load(cask_name)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/cask/source/tapped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def self.me?(query)
end

def self.path_for_query(query)
# Repeating Cask.all_titles is very slow for operations such as
# brew cask list, but memoizing the value might cause breakage
# elsewhere, given that installation and tap status is permitted
# to change during the course of an invocation.
cask_with_tap = Cask.all_titles.find { |t| t.split('/').last == query.sub(/\.rb$/i,'') }
if cask_with_tap
user, repo, cask = cask_with_tap.split('/')
Expand Down
1 change: 1 addition & 0 deletions lib/cask/source/untapped_qualified.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def self.path_for_query(query)
unless Cask.tapspath.join(tap).exist?
ohai "Adding new tap '#{tap}'"
Homebrew.install_tap(user, repo)
Cask.reset_all_tapped_cask_dirs
end
Cask.tapspath.join(tap, 'Casks', "#{cask}.rb")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cask/source/uri.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Cask::Source::URI
def self.me?(query)
!!(query =~ URI.regexp)
!!(query.to_s =~ URI.regexp)
end

attr_reader :uri
Expand Down

0 comments on commit 7e26f8f

Please sign in to comment.