From 72b3e470a04873bbd4c30943487bcc5ac278877a Mon Sep 17 00:00:00 2001 From: Xanonymous Date: Sun, 18 Aug 2024 22:23:36 +0100 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20avoid=20dulplicate?= =?UTF-8?q?d=20storage=20access?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcc/gumtree/matchers/BottomUpMatcher.kt | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/core/src/main/kotlin/tw/xcc/gumtree/matchers/BottomUpMatcher.kt b/core/src/main/kotlin/tw/xcc/gumtree/matchers/BottomUpMatcher.kt index f3b0960..de0014e 100644 --- a/core/src/main/kotlin/tw/xcc/gumtree/matchers/BottomUpMatcher.kt +++ b/core/src/main/kotlin/tw/xcc/gumtree/matchers/BottomUpMatcher.kt @@ -16,25 +16,13 @@ class BottomUpMatcher : TreeMatcher { private val realTreePool = NonFrozenGumTreeCachePool private suspend fun lcsFinalMatching( - tree1: GumTree, - tree2: GumTree, + tree1UnMappedChildren: List, + tree2UnMappedChildren: List, storage: TreeMappingStorage, equalFunc: suspend (GumTree, GumTree) -> Boolean ) = coroutineScope { - val unMappedChildrenOfLeftJob = - async { - tree1.getChildren().filter { !storage.isLeftMapped(it) } - } - val unMappedChildrenOfRightJob = - async { - tree2.getChildren().filter { !storage.isRightMapped(it) } - } - - val unMappedChildrenOfLeft = unMappedChildrenOfLeftJob.await() - val unMappedChildrenOfRight = unMappedChildrenOfRightJob.await() - val lcsOfUnMapped = - lcsBaseWithElements(unMappedChildrenOfLeft, unMappedChildrenOfRight) { + lcsBaseWithElements(tree1UnMappedChildren, tree2UnMappedChildren) { left, right -> equalFunc(left, right) } @@ -57,21 +45,12 @@ class BottomUpMatcher : TreeMatcher { } private suspend fun histogramMatching( - tree1: GumTree, - tree2: GumTree, + tree1UnMappedChildren: List, + tree2UnMappedChildren: List, storage: TreeMappingStorage ) = coroutineScope { - val histogramOfLeftJob = - async { - tree1.getChildren().filter { !storage.isLeftMapped(it) }.groupBy { it.info.type } - } - val histogramOfRightJob = - async { - tree2.getChildren().filter { !storage.isRightMapped(it) }.groupBy { it.info.type } - } - - val histogramOfLeft = histogramOfLeftJob.await() - val histogramOfRight = histogramOfRightJob.await() + val histogramOfLeft = tree1UnMappedChildren.groupBy { it.info.type } + val histogramOfRight = tree2UnMappedChildren.groupBy { it.info.type } (histogramOfLeft.keys intersect histogramOfRight.keys).forEach { key -> val leftNodes = histogramOfLeft[key] @@ -93,15 +72,27 @@ class BottomUpMatcher : TreeMatcher { storage: TreeMappingStorage ) { coroutineScope { - lcsFinalMatching(tree1, tree2, storage) { left, right -> + val unMappedChildrenOfLeftJob = + async { + tree1.getChildren().filter { !storage.isLeftMapped(it) } + } + val unMappedChildrenOfRightJob = + async { + tree2.getChildren().filter { !storage.isRightMapped(it) } + } + + val unMappedChildrenOfLeft = unMappedChildrenOfLeftJob.await() + val unMappedChildrenOfRight = unMappedChildrenOfRightJob.await() + + lcsFinalMatching(unMappedChildrenOfLeft, unMappedChildrenOfRight, storage) { left, right -> left isIsomorphicTo right } - lcsFinalMatching(tree1, tree2, storage) { left, right -> + lcsFinalMatching(unMappedChildrenOfLeft, unMappedChildrenOfRight, storage) { left, right -> left isIsoStructuralTo right } - histogramMatching(tree1, tree2, storage) + histogramMatching(unMappedChildrenOfLeft, unMappedChildrenOfRight, storage) } }