Skip to content

Commit

Permalink
Merge pull request #157 from pelias/circuit-breaker
Browse files Browse the repository at this point in the history
performance: add circuit breakers in the ExclusiveCartesianSolver
  • Loading branch information
orangejulius authored Nov 29, 2021
2 parents a4ed181 + 7f32de7 commit f8fdef0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions solver/ExclusiveCartesianSolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Solution = require('./Solution')
const HashMapSolver = require('./super/HashMapSolver')
const MAX_RECURSION = 10
const MAX_SOLUTIONS = 50000

class ExclusiveCartesianSolver extends HashMapSolver {
solve (tokenizer) {
Expand All @@ -20,10 +22,10 @@ class ExclusiveCartesianSolver extends HashMapSolver {
copy.pair.push(arg[i].pair[j])
}
if (i === max) {
if (copy.pair.length) {
if (copy.pair.length && r.length < MAX_SOLUTIONS) {
r.push(copy)
}
} else {
} else if (i < MAX_RECURSION) {
helper(copy, i + 1)
}
}
Expand Down
3 changes: 3 additions & 0 deletions solver/super/HashMapSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BaseSolver = require('./BaseSolver')
const Span = require('../../tokenization/Span')
const Solution = require('../Solution')
const SolutionPair = require('../SolutionPair')
const MAX_PAIRS_PER_LABEL = 8

class HashMapSolver extends BaseSolver {
// you should provide this function in your subclass
Expand All @@ -27,6 +28,7 @@ class HashMapSolver extends BaseSolver {
map[classification.label].pair.push(new SolutionPair(new Span(), classification))
}
}
if (map[classification.label].pair.length >= MAX_PAIRS_PER_LABEL) { continue }
map[classification.label].pair.push(new SolutionPair(phrase, classification))
}
}
Expand All @@ -46,6 +48,7 @@ class HashMapSolver extends BaseSolver {
map[classification.label].pair.push(new SolutionPair(new Span(), classification))
}
}
if (map[classification.label].pair.length >= MAX_PAIRS_PER_LABEL) { continue }
map[classification.label].pair.push(new SolutionPair(word, classification))
}
}
Expand Down

0 comments on commit f8fdef0

Please sign in to comment.