Skip to content

Commit

Permalink
feat: Make UnknownDatatype a singleton to reduce memory allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Oct 3, 2024
1 parent 4f142b1 commit 5e51f43
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ open class DefaultTypeSolver {
} else if (node.type === PlSqlGrammar.LITERAL) {
return solveLiteral(node)
}
return UnknownDatatype()
return UnknownDatatype
}

open fun solveDatatype(node: AstNode, scope: Scope?): PlSqlDatatype {
var type: PlSqlDatatype = UnknownDatatype()
var type: PlSqlDatatype = UnknownDatatype
if (node.hasDirectChildren(PlSqlGrammar.CHARACTER_DATAYPE)) {
type = CharacterDatatype(node)
} else if (node.hasDirectChildren(PlSqlGrammar.NUMERIC_DATATYPE)) {
Expand All @@ -59,13 +59,13 @@ open class DefaultTypeSolver {
type = JsonDatatype()
} else {
val datatype = node.firstChild
type = scope?.getSymbol(datatype.tokenValue, Symbol.Kind.TYPE)?.datatype ?: UnknownDatatype()
type = scope?.getSymbol(datatype.tokenValue, Symbol.Kind.TYPE)?.datatype ?: UnknownDatatype
}
return type
}

open fun solveLiteral(node: AstNode): PlSqlDatatype {
var type: PlSqlDatatype = UnknownDatatype()
var type: PlSqlDatatype = UnknownDatatype
if (node.hasDirectChildren(PlSqlGrammar.NULL_LITERAL) || isEmptyString(node)) {
type = NullDatatype()
} else if (node.hasDirectChildren(PlSqlGrammar.CHARACTER_LITERAL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
}

val datatype = node.getFirstChildOrNull(PlSqlKeyword.RETURN)?.nextSibling
val type = if (datatype != null) solveType(datatype) else UnknownDatatype()
val type = if (datatype != null) solveType(datatype) else UnknownDatatype
val symbolKind = when(node.type) {
PlSqlGrammar.CREATE_PROCEDURE -> Symbol.Kind.PROCEDURE
PlSqlGrammar.PROCEDURE_DECLARATION -> Symbol.Kind.PROCEDURE
Expand Down Expand Up @@ -243,15 +243,15 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
}
enterScope(node, autonomousTransaction = false, exceptionHandler = false)
} else {
val symbol = createSymbol(identifier, Symbol.Kind.PACKAGE, UnknownDatatype())
val symbol = createSymbol(identifier, Symbol.Kind.PACKAGE, UnknownDatatype)
enterScope(node, autonomousTransaction = false, exceptionHandler = false)
symbol.innerScope = currentScope
}
}

private fun visitCursor(node: AstNode) {
val identifier = node.getFirstChild(PlSqlGrammar.IDENTIFIER_NAME)
val symbol = createSymbol(identifier, Symbol.Kind.CURSOR, UnknownDatatype())
val symbol = createSymbol(identifier, Symbol.Kind.CURSOR, UnknownDatatype)
enterScope(node)
symbol.innerScope = currentScope
}
Expand All @@ -275,7 +275,7 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
control.hasDirectChildren(PlSqlGrammar.DYNAMIC_SQL)) {
RowtypeDatatype()
} else {
UnknownDatatype()
UnknownDatatype
}

for (declaration in declarations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SemanticAstNode(type: AstNodeType, name: String, token: Token?) : AstNode(
(child as SemanticAstNode).symbol = symbol
}

var plSqlDatatype: PlSqlDatatype = UnknownDatatype()
var plSqlDatatype: PlSqlDatatype = UnknownDatatype
get() = this.symbol?.datatype ?: field

val plSqlType: PlSqlType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open class Symbol(val node: AstNode?,

val type: PlSqlType = datatype?.type ?: PlSqlType.UNKNOWN

val datatype: PlSqlDatatype = datatype ?: UnknownDatatype()
val datatype: PlSqlDatatype = datatype ?: UnknownDatatype

val modifiers: List<AstNode>
get() = Collections.unmodifiableList(internalModifiers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.sonar.plugins.plsqlopen.api.symbols.datatype

import org.sonar.plugins.plsqlopen.api.symbols.PlSqlType

class UnknownDatatype : PlSqlDatatype {
object UnknownDatatype : PlSqlDatatype {
override val type = PlSqlType.UNKNOWN

override val name: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SymbolTableImplTest {
val scope = ScopeImpl(null, newAstNodeForTest("scope"), isAutonomousTransaction = false, hasExceptionHandler = false)

val symbolTable = SymbolTableImpl()
val symbol = symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype())
val symbol = symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype)

assertThat(symbolTable.symbols.find { it.declaration == node }).isEqualTo(symbol)
assertThat(symbolTable.symbols.find { it.declaration == node2 }).isNull()
Expand All @@ -84,7 +84,7 @@ class SymbolTableImplTest {
val scope = ScopeImpl(null, newAstNodeForTest("scope"), isAutonomousTransaction = false, hasExceptionHandler = false)

val symbolTable = SymbolTableImpl()
symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype())
symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype)

assertThat(symbolTable.symbols.find { it.declaration == node }?.scope).isEqualTo(scope)
assertThat(symbolTable.symbols.find { it.declaration == node2 }?.scope).isNull()
Expand All @@ -97,8 +97,8 @@ class SymbolTableImplTest {
val node = newAstNodeForTest("foo")

val symbolTable = SymbolTableImpl()
val symbol1 = symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype())
val symbol2 = symbolTable.declareSymbol(node, Kind.VARIABLE, scope, UnknownDatatype())
val symbol1 = symbolTable.declareSymbol(node, Kind.CURSOR, scope, UnknownDatatype)
val symbol2 = symbolTable.declareSymbol(node, Kind.VARIABLE, scope, UnknownDatatype)

assertThat(symbolTable.symbols.filter { it.kind == Kind.CURSOR }).containsExactly(symbol1)
assertThat(symbolTable.symbols.filter { it.kind == Kind.VARIABLE }).containsExactly(symbol2)
Expand All @@ -113,8 +113,8 @@ class SymbolTableImplTest {
val node2 = newAstNodeForTest("FOO")

val symbolTable = SymbolTableImpl()
val symbol1 = symbolTable.declareSymbol(node1, Kind.CURSOR, scope, UnknownDatatype())
val symbol2 = symbolTable.declareSymbol(node2, Kind.VARIABLE, scope, UnknownDatatype())
val symbol1 = symbolTable.declareSymbol(node1, Kind.CURSOR, scope, UnknownDatatype)
val symbol2 = symbolTable.declareSymbol(node2, Kind.VARIABLE, scope, UnknownDatatype)

assertThat(symbolTable.symbols.filter { it.name.equals("foo", ignoreCase = true) }).containsExactly(symbol1, symbol2)
assertThat(symbolTable.symbols.filter { it.name.equals("bar", ignoreCase = true) }).isEmpty()
Expand Down

0 comments on commit 5e51f43

Please sign in to comment.