From aa7f0faec0a050f7237a4f3a7ae03b924c65eebf Mon Sep 17 00:00:00 2001 From: Felipe Zorzo Date: Sat, 18 May 2024 22:24:35 -0300 Subject: [PATCH] refactor: Reduce memory usage of MethodMatcher --- .../plsqlopen/api/matchers/MethodMatcher.kt | 27 +++++++++++++------ .../plsqlopen/api/matchers/NameCriteria.kt | 7 ++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/MethodMatcher.kt b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/MethodMatcher.kt index 648337a2..b1a5e281 100644 --- a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/MethodMatcher.kt +++ b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/MethodMatcher.kt @@ -101,27 +101,38 @@ class MethodMatcher private constructor() fun matches(originalNode: AstNode): Boolean { val node = normalize(originalNode) - val nodes = node.getChildren(PlSqlGrammar.VARIABLE_NAME, PlSqlGrammar.IDENTIFIER_NAME).toMutableList() - if (nodes.isEmpty()) { + var i = -1 + val nodes = arrayOfNulls(3) + for (child in node.children) { + if (i < 2 && child.type === PlSqlGrammar.VARIABLE_NAME || child.type === PlSqlGrammar.IDENTIFIER_NAME) { + nodes[++i] = child.tokenOriginalValue + } + } + + fun hasMoreItensToCheck() = i > -1 + fun nextNode() = nodes[i--] + + if (!hasMoreItensToCheck()) { return false } - var matches = methodNameCriteria?.let { nameAcceptable(nodes.removeAt(nodes.lastIndex), it) } ?: true + var matches = methodNameCriteria?.let { nameAcceptable(nextNode(), it) } ?: true packageNameCriteria?.let { - matches = matches and (nodes.isNotEmpty() && nameAcceptable(nodes.removeAt(nodes.lastIndex), it)) + matches = matches and (hasMoreItensToCheck() && nameAcceptable(nextNode(), it)) } schemaNameCriteria?.let { - matches = matches and (schemaIsOptional && nodes.isEmpty() || nodes.isNotEmpty() && nameAcceptable(nodes.removeAt(nodes.lastIndex), it)) + matches = matches and (schemaIsOptional && !hasMoreItensToCheck() || + hasMoreItensToCheck() && nameAcceptable(nextNode(), it)) } - return matches && nodes.isEmpty() && argumentsAcceptable(originalNode) + return matches && !hasMoreItensToCheck() && argumentsAcceptable(originalNode) } - private fun nameAcceptable(node: AstNode, criteria: NameCriteria): Boolean { - methodName = node.tokenOriginalValue + private fun nameAcceptable(name: String?, criteria: NameCriteria): Boolean { + methodName = name ?: "" return criteria.matches(methodName) } diff --git a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/NameCriteria.kt b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/NameCriteria.kt index 6aeb335c..a6e28d7e 100644 --- a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/NameCriteria.kt +++ b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/matchers/NameCriteria.kt @@ -23,7 +23,7 @@ import java.util.* fun interface NameCriteria { - fun matches(name: String): Boolean + fun matches(name: String?): Boolean companion object { @@ -38,8 +38,9 @@ fun interface NameCriteria { @JvmStatic fun startsWith(prefix: String): NameCriteria = NameCriteria { name -> - name.uppercase(Locale.getDefault()) - .startsWith(prefix.uppercase(Locale.getDefault())) + name != null && + name.uppercase(Locale.getDefault()) + .startsWith(prefix.uppercase(Locale.getDefault())) } @JvmStatic