Skip to content

Commit

Permalink
refactor: Reduce memory usage of MethodMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed May 19, 2024
1 parent 1f6af00 commit aa7f0fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>(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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.util.*

fun interface NameCriteria {

fun matches(name: String): Boolean
fun matches(name: String?): Boolean

companion object {

Expand All @@ -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
Expand Down

0 comments on commit aa7f0fa

Please sign in to comment.