Skip to content

Commit

Permalink
Optimize push_state_for_requirements by removing N^2 lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
amorde committed Mar 5, 2022
1 parent 8053207 commit 99563ef
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/molinillo/resolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def initialize(specification_provider, resolver_ui, requested, base)
@original_requested = requested
@base = base
@states = []
@states_by_requirement = {}
@iteration_counter = 0
@parents_of = Hash.new { |h, k| h[k] = [] }
end
Expand Down Expand Up @@ -200,6 +201,10 @@ def resolve
attr_accessor :states
private :states

# @return [Hash{Object => ResolutionState}] states keyed by their requirement
attr_accessor :states_by_requirement
private :states

private

# Sets up the resolution process
Expand Down Expand Up @@ -586,7 +591,7 @@ def requirement_for_existing_name(name)
# `requirement`.
def find_state_for(requirement)
return nil unless requirement
states.find { |i| requirement == i.requirement }
states_by_requirement[requirement]
end

# @param [Object] underlying_error
Expand Down Expand Up @@ -755,7 +760,7 @@ def push_state_for_requirements(new_requirements, requires_sort = true, new_acti
new_requirement = nil
loop do
new_requirement = new_requirements.shift
break if new_requirement.nil? || states.none? { |s| s.requirement == new_requirement }
break if new_requirement.nil? || states_by_requirement[new_requirement].nil?
end
new_name = new_requirement ? name_for(new_requirement) : ''.freeze
possibilities = possibilities_for_requirement(new_requirement)
Expand Down Expand Up @@ -831,6 +836,7 @@ def handle_missing_or_push_dependency_state(state)
state.activated.detach_vertex_named(state.name)
push_state_for_requirements(state.requirements.dup, false, state.activated)
else
states_by_requirement[state.requirement] = state
states.push(state).tap { activated.tag(state) }
end
end
Expand Down

0 comments on commit 99563ef

Please sign in to comment.