Commit 44131d5c authored by Sean McGivern's avatar Sean McGivern

Merge branch 'perf.policy-runner-reduce-loops' into 'master'

DeclarativePolicy: reduce iterations of the step set

See merge request !13352
parents 1f31c845 15bb6e1e
...@@ -76,6 +76,8 @@ module DeclarativePolicy ...@@ -76,6 +76,8 @@ module DeclarativePolicy
@state = State.new @state = State.new
steps_by_score do |step, score| steps_by_score do |step, score|
return if !debug && @state.prevented?
passed = nil passed = nil
case step.action case step.action
when :enable then when :enable then
...@@ -93,10 +95,7 @@ module DeclarativePolicy ...@@ -93,10 +95,7 @@ module DeclarativePolicy
# been prevented. # been prevented.
unless @state.prevented? unless @state.prevented?
passed = step.pass? passed = step.pass?
if passed @state.prevent! if passed
@state.prevent!
return unless debug
end
end end
debug << inspect_step(step, score, passed) if debug debug << inspect_step(step, score, passed) if debug
...@@ -141,13 +140,14 @@ module DeclarativePolicy ...@@ -141,13 +140,14 @@ module DeclarativePolicy
end end
steps = Set.new(@steps) steps = Set.new(@steps)
remaining_enablers = steps.count { |s| s.enable? }
loop do loop do
return if steps.empty? return if steps.empty?
# if the permission hasn't yet been enabled and we only have # if the permission hasn't yet been enabled and we only have
# prevent steps left, we short-circuit the state here # prevent steps left, we short-circuit the state here
@state.prevent! if !@state.enabled? && steps.all?(&:prevent?) @state.prevent! if !@state.enabled? && remaining_enablers == 0
lowest_score = Float::INFINITY lowest_score = Float::INFINITY
next_step = nil next_step = nil
...@@ -162,6 +162,8 @@ module DeclarativePolicy ...@@ -162,6 +162,8 @@ module DeclarativePolicy
steps.delete(next_step) steps.delete(next_step)
remaining_enablers -= 1 if next_step.enable?
yield next_step, lowest_score yield next_step, lowest_score
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment