diff options
| author | Xu Cheng | 2015-09-27 16:52:14 +0800 |
|---|---|---|
| committer | Xu Cheng | 2015-09-30 16:25:30 +0800 |
| commit | 3b520cf195b6d766d134a1f2bd033347c714a143 (patch) | |
| tree | 31001ad53b1fcad563994aa328f82c59a7a805d8 /Library/Homebrew/tap.rb | |
| parent | 6240e896b2b3405dd3ffdf892b60e5eed05707b1 (diff) | |
| download | brew-3b520cf195b6d766d134a1f2bd033347c714a143.tar.bz2 | |
cache taps
There are plenty of IO operations inside Tap object, and it will be more
when implementing formula alias reverse look up(e.g. list all of alias
names for a formula). So let's cache them.
Some benchmark:
$ time brew info $(brew ruby -e 'puts Formula.tap_names') > /dev/null
Before: 6.40s user 2.42s system 96% cpu 9.134 total
After: 4.75s user 0.77s system 97% cpu 5.637 total
Closes Homebrew/homebrew#44377.
Signed-off-by: Xu Cheng <xucheng@me.com>
Diffstat (limited to 'Library/Homebrew/tap.rb')
| -rw-r--r-- | Library/Homebrew/tap.rb | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 6efcc3af3..0313de8e8 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -9,6 +9,17 @@ require "utils/json" class Tap TAP_DIRECTORY = HOMEBREW_LIBRARY/"Taps" + CACHE = {} + + def self.clear_cache + CACHE.clear + end + + def self.fetch(user, repo) + cache_key = "#{user}/#{repo}".downcase + CACHE.fetch(cache_key) { |key| CACHE[key] = Tap.new(user, repo) } + end + extend Enumerable # The user name of this {Tap}. Usually, it's the Github username of @@ -86,31 +97,33 @@ class Tap # an array of all {Formula} files of this {Tap}. def formula_files - dir = [@path/"Formula", @path/"HomebrewFormula", @path].detect(&:directory?) - return [] unless dir - dir.children.select { |p| p.extname == ".rb" } + @formula_files ||= if dir = [@path/"Formula", @path/"HomebrewFormula", @path].detect(&:directory?) + dir.children.select { |p| p.extname == ".rb" } + else + [] + end end # an array of all {Formula} names of this {Tap}. def formula_names - formula_files.map { |f| "#{name}/#{f.basename(".rb")}" } + @formula_names ||= formula_files.map { |f| "#{name}/#{f.basename(".rb")}" } end # an array of all alias files of this {Tap}. # @private def alias_files - Pathname.glob("#{path}/Aliases/*").select(&:file?) + @alias_files ||= Pathname.glob("#{path}/Aliases/*").select(&:file?) end # an array of all aliases of this {Tap}. # @private def aliases - alias_files.map { |f| "#{name}/#{f.basename}" } + @aliases ||= alias_files.map { |f| "#{name}/#{f.basename}" } end # an array of all commands files of this {Tap}. def command_files - Pathname.glob("#{path}/cmd/brew-*").select(&:executable?) + @command_files ||= Pathname.glob("#{path}/cmd/brew-*").select(&:executable?) end def pinned_symlink_path @@ -118,13 +131,15 @@ class Tap end def pinned? - @pinned ||= pinned_symlink_path.directory? + return @pinned if instance_variable_defined?(:@pinned) + @pinned = pinned_symlink_path.directory? end def pin raise TapUnavailableError, name unless installed? raise TapPinStatusError.new(name, true) if pinned? pinned_symlink_path.make_relative_symlink(@path) + @pinned = true end def unpin @@ -132,6 +147,7 @@ class Tap raise TapPinStatusError.new(name, false) unless pinned? pinned_symlink_path.delete pinned_symlink_path.dirname.rmdir_if_possible + @pinned = false end def to_hash @@ -170,7 +186,7 @@ class Tap TAP_DIRECTORY.subdirs.each do |user| user.subdirs.each do |repo| - yield new(user.basename.to_s, repo.basename.to_s.sub("homebrew-", "")) + yield fetch(user.basename.to_s, repo.basename.to_s.sub("homebrew-", "")) end end end |
