Skip to content

Commit 91caf90

Browse files
committed
Optimize push_state_for_requirements by removing N^2 lookup
1 parent 8053207 commit 91caf90

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/molinillo/resolution.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def initialize(specification_provider, resolver_ui, requested, base)
155155
@original_requested = requested
156156
@base = base
157157
@states = []
158+
@states_by_requirement = {}
158159
@iteration_counter = 0
159160
@parents_of = Hash.new { |h, k| h[k] = [] }
160161
end
@@ -174,8 +175,7 @@ def resolve
174175
debug(depth) { "Creating possibility state for #{requirement} (#{possibilities.count} remaining)" }
175176
state.pop_possibility_state.tap do |s|
176177
if s
177-
states.push(s)
178-
activated.tag(s)
178+
push_state(s)
179179
end
180180
end
181181
end
@@ -200,6 +200,10 @@ def resolve
200200
attr_accessor :states
201201
private :states
202202

203+
# @return [Hash{Object => ResolutionState}] states keyed by their requirement
204+
attr_accessor :states_by_requirement
205+
private :states
206+
203207
private
204208

205209
# Sets up the resolution process
@@ -586,7 +590,7 @@ def requirement_for_existing_name(name)
586590
# `requirement`.
587591
def find_state_for(requirement)
588592
return nil unless requirement
589-
states.find { |i| requirement == i.requirement }
593+
states_by_requirement[requirement]
590594
end
591595

592596
# @param [Object] underlying_error
@@ -755,7 +759,7 @@ def push_state_for_requirements(new_requirements, requires_sort = true, new_acti
755759
new_requirement = nil
756760
loop do
757761
new_requirement = new_requirements.shift
758-
break if new_requirement.nil? || states.none? { |s| s.requirement == new_requirement }
762+
break if new_requirement.nil? || states_by_requirement[new_requirement].nil?
759763
end
760764
new_name = new_requirement ? name_for(new_requirement) : ''.freeze
761765
possibilities = possibilities_for_requirement(new_requirement)
@@ -831,9 +835,14 @@ def handle_missing_or_push_dependency_state(state)
831835
state.activated.detach_vertex_named(state.name)
832836
push_state_for_requirements(state.requirements.dup, false, state.activated)
833837
else
834-
states.push(state).tap { activated.tag(state) }
838+
push_state(state)
835839
end
836840
end
841+
842+
def push_state(state)
843+
states_by_requirement[state.requirement] = state
844+
states.push(state).tap { activated.tag(state) }
845+
end
837846
end
838847
end
839848
end

0 commit comments

Comments
 (0)