diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-08-08 00:15:15 +0300 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-08-08 00:15:15 +0300 |
commit | 44131d5c2adfbf2dde16bba6621143037409c68b (patch) | |
tree | e4007923a9c1b1f5ff1699ec7d702734a56dd025 | |
parent | 1f31c8458a65683e71d2fc504a408796a488176a (diff) | |
parent | 15bb6e1ed7a4fb1d6554e193497836b79bc26bda (diff) |
Merge branch 'perf.policy-runner-reduce-loops' into 'master'
DeclarativePolicy: reduce iterations of the step set
See merge request !13352
-rw-r--r-- | lib/declarative_policy/runner.rb | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/declarative_policy/runner.rb b/lib/declarative_policy/runner.rb index b5c615da4e3..56afd1f1392 100644 --- a/lib/declarative_policy/runner.rb +++ b/lib/declarative_policy/runner.rb @@ -76,6 +76,8 @@ module DeclarativePolicy @state = State.new steps_by_score do |step, score| + return if !debug && @state.prevented? + passed = nil case step.action when :enable then @@ -93,10 +95,7 @@ module DeclarativePolicy # been prevented. unless @state.prevented? passed = step.pass? - if passed - @state.prevent! - return unless debug - end + @state.prevent! if passed end debug << inspect_step(step, score, passed) if debug @@ -141,13 +140,14 @@ module DeclarativePolicy end steps = Set.new(@steps) + remaining_enablers = steps.count { |s| s.enable? } loop do return if steps.empty? # if the permission hasn't yet been enabled and we only have # 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 next_step = nil @@ -162,6 +162,8 @@ module DeclarativePolicy steps.delete(next_step) + remaining_enablers -= 1 if next_step.enable? + yield next_step, lowest_score end end |