reduce iterations by keeping a count of remaining enablers

rather than iterating the whole remaining step set with
.all?(&:prevent?)
This commit is contained in:
http://jneen.net/ 2017-08-07 12:36:13 -07:00
parent bc648ae5d2
commit 8a167554a9

View file

@ -141,13 +141,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 +163,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