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 | |
| 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')
| -rw-r--r-- | Library/Homebrew/cmd/info.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/readall.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/tap-info.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/tap-pin.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/tap-unpin.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/tap.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/test-bot.rb | 6 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/untap.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/update.rb | 4 | ||||
| -rw-r--r-- | Library/Homebrew/formula.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/formulary.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/tap.rb | 34 | 
12 files changed, 40 insertions, 22 deletions
| diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index a2b491e05..52d39ca15 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -69,7 +69,7 @@ module Homebrew    def github_info(f)      if f.tap?        user, repo = f.tap.split("/", 2) -      tap = Tap.new user, repo.gsub(/^homebrew-/, "") +      tap = Tap.fetch user, repo.gsub(/^homebrew-/, "")        if remote = tap.remote          path = f.path.relative_path_from(tap.path)          github_remote_path(remote, path) diff --git a/Library/Homebrew/cmd/readall.rb b/Library/Homebrew/cmd/readall.rb index 81eaba37d..5e87fcbdb 100644 --- a/Library/Homebrew/cmd/readall.rb +++ b/Library/Homebrew/cmd/readall.rb @@ -46,7 +46,7 @@ module Homebrew      if ARGV.named.empty?        formulae = Formula.files      else -      tap = Tap.new(*tap_args) +      tap = Tap.fetch(*tap_args)        raise TapUnavailableError, tap.name unless tap.installed?        formulae = tap.formula_files      end diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index 1354264df..dc979af7e 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -6,7 +6,7 @@ module Homebrew        taps = Tap      else        taps = ARGV.named.map do |name| -        Tap.new(*tap_args(name)) +        Tap.fetch(*tap_args(name))        end      end diff --git a/Library/Homebrew/cmd/tap-pin.rb b/Library/Homebrew/cmd/tap-pin.rb index 40888428a..80f1f762a 100644 --- a/Library/Homebrew/cmd/tap-pin.rb +++ b/Library/Homebrew/cmd/tap-pin.rb @@ -3,7 +3,7 @@ require "cmd/tap"  module Homebrew    def tap_pin      ARGV.named.each do |name| -      tap = Tap.new(*tap_args(name)) +      tap = Tap.fetch(*tap_args(name))        tap.pin        ohai "Pinned #{tap.name}"      end diff --git a/Library/Homebrew/cmd/tap-unpin.rb b/Library/Homebrew/cmd/tap-unpin.rb index 4462ce68d..641b7c945 100644 --- a/Library/Homebrew/cmd/tap-unpin.rb +++ b/Library/Homebrew/cmd/tap-unpin.rb @@ -3,7 +3,7 @@ require "cmd/tap"  module Homebrew    def tap_unpin      ARGV.named.each do |name| -      tap = Tap.new(*tap_args(name)) +      tap = Tap.fetch(*tap_args(name))        tap.unpin        ohai "Unpinned #{tap.name}"      end diff --git a/Library/Homebrew/cmd/tap.rb b/Library/Homebrew/cmd/tap.rb index 291407aff..294106b3c 100644 --- a/Library/Homebrew/cmd/tap.rb +++ b/Library/Homebrew/cmd/tap.rb @@ -23,7 +23,7 @@ module Homebrew      # ensure git is installed      Utils.ensure_git_installed! -    tap = Tap.new user, repo +    tap = Tap.fetch user, repo      return false if tap.installed?      ohai "Tapping #{tap}"      remote = clone_target || "https://github.com/#{tap.user}/homebrew-#{tap.repo}" diff --git a/Library/Homebrew/cmd/test-bot.rb b/Library/Homebrew/cmd/test-bot.rb index 66f23d873..658f042f9 100644 --- a/Library/Homebrew/cmd/test-bot.rb +++ b/Library/Homebrew/cmd/test-bot.rb @@ -39,20 +39,20 @@ module Homebrew    def resolve_test_tap      tap = ARGV.value("tap") -    return Tap.new(*tap_args(tap)) if tap +    return Tap.fetch(*tap_args(tap)) if tap      if ENV["UPSTREAM_BOT_PARAMS"]        bot_argv = ENV["UPSTREAM_BOT_PARAMS"].split " "        bot_argv.extend HomebrewArgvExtension        tap = bot_argv.value("tap") -      return Tap.new(*tap_args(tap)) if tap +      return Tap.fetch(*tap_args(tap)) if tap      end      if git_url = ENV["UPSTREAM_GIT_URL"] || ENV["GIT_URL"]        # Also can get tap from Jenkins GIT_URL.        url_path = git_url.sub(%r{^https?://github\.com/}, "").chomp("/")        HOMEBREW_TAP_ARGS_REGEX =~ url_path -      return Tap.new($1, $3) if $1 && $3 && $3 != "homebrew" +      return Tap.fetch($1, $3) if $1 && $3 && $3 != "homebrew"      end      # return nil means we are testing core repo. diff --git a/Library/Homebrew/cmd/untap.rb b/Library/Homebrew/cmd/untap.rb index 22dab7383..c70a0d007 100644 --- a/Library/Homebrew/cmd/untap.rb +++ b/Library/Homebrew/cmd/untap.rb @@ -6,7 +6,7 @@ module Homebrew      raise "Usage is `brew untap <tap-name>`" if ARGV.empty?      ARGV.named.each do |tapname| -      tap = Tap.new(*tap_args(tapname)) +      tap = Tap.fetch(*tap_args(tapname))        raise TapUnavailableError, tap.name unless tap.installed?        puts "Untapping #{tap}... (#{tap.path.abv})" diff --git a/Library/Homebrew/cmd/update.rb b/Library/Homebrew/cmd/update.rb index 917244a8d..b50348bcf 100644 --- a/Library/Homebrew/cmd/update.rb +++ b/Library/Homebrew/cmd/update.rb @@ -63,6 +63,8 @@ module Homebrew        end      end +    Tap.clear_cache +      # automatically tap any migrated formulae's new tap      report.select_formula(:D).each do |f|        next unless (dir = HOMEBREW_CELLAR/f).exist? @@ -372,7 +374,7 @@ class Report          user = $1          repo = $2.sub("homebrew-", "")          oldname = path.basename(".rb").to_s -        next unless newname = Tap.new(user, repo).formula_renames[oldname] +        next unless newname = Tap.fetch(user, repo).formula_renames[oldname]        else          oldname = path.basename(".rb").to_s          next unless newname = FORMULA_RENAMES[oldname] diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index d6ed61183..5af681f98 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -273,7 +273,7 @@ class Formula        end      elsif tap?        user, repo = tap.split("/") -      formula_renames = Tap.new(user, repo.sub("homebrew-", "")).formula_renames +      formula_renames = Tap.fetch(user, repo.sub("homebrew-", "")).formula_renames        if formula_renames.value?(name)          formula_renames.to_a.rassoc(name).first        end diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index a21d6e731..2bdaca752 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -145,7 +145,7 @@ class Formulary      def initialize(tapped_name)        user, repo, name = tapped_name.split("/", 3).map(&:downcase) -      @tap = Tap.new user, repo.sub(/^homebrew-/, "") +      @tap = Tap.fetch user, repo.sub(/^homebrew-/, "")        name = @tap.formula_renames.fetch(name, name)        path = @tap.formula_files.detect { |file| file.basename(".rb").to_s == name } 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 | 
