Skip to content

Commit efdbd00

Browse files
Performance optimisations
1 parent a920da0 commit efdbd00

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

lib/rubygems/resolver.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ def initialize(needed, set = nil)
119119
end
120120
end
121121
@version_to_index = Hash.new {|h, pkg| h[pkg] = @sorted_versions[pkg].each_with_index.to_h }
122-
@versions_for_cache = {}
122+
@versions_for_cache = Hash.new {|h, pkg| h[pkg] = {} }
123+
@spec_for_cache = Hash.new {|h, name| h[name] = build_spec_for_cache(name) }
123124
end
124125

125126
##
@@ -192,7 +193,7 @@ def all_versions_for(package)
192193
end
193194

194195
def versions_for(package, range = Gem::PubGrub::VersionRange.any)
195-
@versions_for_cache[[package, range]] ||= begin
196+
@versions_for_cache[package][range] ||= begin
196197
candidates = range.select_versions(@sorted_versions[package])
197198

198199
if Gem::PubGrub::Package.root?(package) ||
@@ -357,18 +358,20 @@ def filter_specs(specs)
357358
end
358359

359360
def spec_for(name, version)
360-
candidates = @all_specs[name].select {|s| s.version == version }
361+
@spec_for_cache[name][version]
362+
end
363+
364+
def build_spec_for_cache(name)
365+
@all_specs[name].group_by(&:version).transform_values do |candidates|
366+
next candidates.first if candidates.length == 1
361367

362-
if candidates.length > 1
363368
# Prefer already-installed specs to avoid unnecessary downloads
364369
installed = candidates.select {|s| s.is_a?(Gem::Resolver::InstalledSpecification) }
365-
return installed.first if installed.length == 1
370+
next installed.first if installed.length == 1
366371
candidates = installed if installed.any?
367372

368373
# Among remaining candidates, prefer the most specific platform
369374
candidates.min_by {|s| Gem::Platform.platform_specificity_match(s.platform, Gem::Platform.local) }
370-
else
371-
candidates.first
372375
end
373376
end
374377

lib/rubygems/resolver/strategy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Gem::Resolver::Strategy
88
def initialize(source)
99
@source = source
10-
@package_priority_cache = {}
10+
@package_priority_cache = Hash.new {|h, pkg| h[pkg] = {} }
1111

1212
@version_indexes = Hash.new do |h, k|
1313
if Gem::PubGrub::Package.root?(k)
@@ -33,7 +33,7 @@ def most_preferred_version_of(package, range)
3333

3434
def next_term_to_try_from(unsatisfied)
3535
unsatisfied.min_by do |package, range|
36-
@package_priority_cache[[package, range]] ||= begin
36+
@package_priority_cache[package][range] ||= begin
3737
matching_versions = @source.versions_for(package, range)
3838
higher_versions = @source.versions_for(package, range.upper_invert)
3939

0 commit comments

Comments
 (0)