Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz authored and Yehuda Katz committed Nov 15, 2010
1 parent 92d0c96 commit 80015b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
23 changes: 20 additions & 3 deletions lib/bundler/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ def self.build
i
end

attr_reader :specs
protected :specs

def initialize
@cache = {}
@specs = Hash.new { |h,k| h[k] = [] }
Expand All @@ -17,7 +20,10 @@ def initialize_copy(o)
super
@cache = {}
@specs = Hash.new { |h,k| h[k] = [] }
merge!(o)

o.specs.each do |name, array|
@specs[name] = array.dup
end
end

def empty?
Expand Down Expand Up @@ -59,7 +65,7 @@ def <<(spec)
arr = @specs[spec.name]

arr.delete_if do |s|
s.version == spec.version && s.platform == spec.platform
same_version?(s.version, spec.version) && s.platform == spec.platform
end

arr << spec
Expand Down Expand Up @@ -91,10 +97,21 @@ def ==(o)

def search_by_spec(spec)
@specs[spec.name].select do |s|
s.version == spec.version && Gem::Platform.new(s.platform) == Gem::Platform.new(spec.platform)
same_version?(s.version, spec.version) && Gem::Platform.new(s.platform) == Gem::Platform.new(spec.platform)
end
end

def same_version?(a, b)
regex = /^(.*?)(?:\.0)*$/

ret = a.to_s[regex, 1] == b.to_s[regex, 1]
end

def spec_satisfies_dependency?(spec, dep)
return false unless dep.name === spec.name
dep.requirement.satisfied_by?(spec.version)
end

def search_by_dependency(dependency)
@cache[dependency.hash] ||= begin
specs = @specs[dependency.name]
Expand Down
33 changes: 15 additions & 18 deletions lib/bundler/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def installed_specs
@installed_specs ||= begin
idx = Index.new
have_bundler = false
Gem::SourceIndex.from_installed_gems.to_a.reverse.each do |dont_use_this_var, spec|
Gem.source_index.to_a.reverse.each do |dont_use_this_var, spec|
next if spec.name == 'bundler' && spec.version.to_s != VERSION
have_bundler = true if spec.name == 'bundler'
spec.source = self
Expand All @@ -185,27 +185,24 @@ def installed_specs

def cached_specs
@cached_specs ||= begin
idx = Index.new
@caches.each do |path|
Dir["#{path}/*.gem"].each do |gemfile|
next if gemfile =~ /bundler\-[\d\.]+?\.gem/

# Try to skip decompressing the gem to get at the gemspec if possible
cached_gemspec = gemfile.gsub(%r{cache/(.*?)\.gem}, 'specifications/\1.gemspec')
s = Gem::Specification.load(cached_gemspec) if File.exist?(cached_gemspec)

begin
s ||= Gem::Format.from_file_by_path(gemfile).spec
rescue Gem::Package::FormatError
raise GemspecError, "Could not read gem at #{gemfile}. It may be corrupted."
end
idx = installed_specs.dup

path = Bundler.app_cache
Dir["#{path}/*.gem"].each do |gemfile|
next if gemfile =~ /bundler\-[\d\.]+?\.gem/

s.source = self
idx << s
begin
s ||= Gem::Format.from_file_by_path(gemfile).spec
rescue Gem::Package::FormatError
raise GemspecError, "Could not read gem at #{gemfile}. It may be corrupted."
end

s.source = self
idx << s
end
idx
end

idx
end

def remote_specs
Expand Down

0 comments on commit 80015b7

Please sign in to comment.