From 17f5bd64b054ac1def78ba59e4cc3d73871a673a Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Tue, 8 Oct 2019 13:20:40 +0200 Subject: [PATCH] Optimize Dispatcher: ensure allMatches are calculated lazily on request. --- .../mps/logic/reactor/core/Dispatcher.kt | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt index 15451c52..d2d3516b 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt @@ -46,10 +46,11 @@ class Dispatcher (val ruleIndex: RuleIndex) { private val ruletag2probe : HashMap - private val allMatches = arrayListOf() + private val matching: Iterable? constructor() { this.ruletag2probe = hashMapOf() + this.matching = null ruletag2matcher.entries.forEach { e -> ruletag2probe.put(e.key, e.value.probe()) } @@ -57,29 +58,30 @@ class Dispatcher (val ruleIndex: RuleIndex) { constructor(predState: DispatchingFrontState) { this.ruletag2probe = hashMapOf() + this.matching = null ruletag2matcher.entries.forEach { e -> ruletag2probe[e.key] = predState[e.key] ?: e.value.probe() } } - private constructor(pred: DispatchingFront) { + private constructor(pred: DispatchingFront, matching: Iterable? = null) { this.ruletag2probe = pred.ruletag2probe - } - - private constructor(pred: DispatchingFront, matching: Iterable) { - this.ruletag2probe = pred.ruletag2probe - matching.forEach { probe -> - if (RULE_MATCHER_PROBE_PERSISTENT) { - ruletag2probe[probe.rule().uniqueTag()] = probe - } - allMatches.addAll(probe.matches()) - } + this.matching = matching } /** * Returns all matches satisfied by the constraint occurrences received so far. */ - fun matches() : Iterable = allMatches + fun matches() : Iterable { + val allMatches = arrayListOf() + matching?.forEach { probe -> + if (RULE_MATCHER_PROBE_PERSISTENT) { + ruletag2probe[probe.rule().uniqueTag()] = probe + } + allMatches.addAll(probe.matches()) + } + return allMatches + } fun state() : DispatchingFrontState = ruletag2probe //.asMap()