From 7f32de72afdd8cf56b4f0c41993329f64a39bea7 Mon Sep 17 00:00:00 2001 From: missinglink Date: Mon, 29 Nov 2021 12:54:56 +0100 Subject: [PATCH] feat(performance): add circuit breakers in the ExclusiveCartesianSolver --- solver/ExclusiveCartesianSolver.js | 6 ++++-- solver/super/HashMapSolver.js | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/solver/ExclusiveCartesianSolver.js b/solver/ExclusiveCartesianSolver.js index 02bf0d5d..9993aae5 100644 --- a/solver/ExclusiveCartesianSolver.js +++ b/solver/ExclusiveCartesianSolver.js @@ -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) { @@ -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) } } diff --git a/solver/super/HashMapSolver.js b/solver/super/HashMapSolver.js index 9e309d95..08561108 100644 --- a/solver/super/HashMapSolver.js +++ b/solver/super/HashMapSolver.js @@ -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 @@ -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)) } } @@ -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)) } }