@@ -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
0 commit comments