Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(perf): stop JVM and C# db lexicons firing on in-memory calls (#2898)

Raghav Chamadiyae59007f7e820
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/csharp.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/csharp.py
index 77a16ab9d..60e043f2b 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/csharp.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/csharp.py
@@ -90,6 +90,10 @@ ADO_METHODS: frozenset[str] = frozenset(
"OpenAsync",
}
)
+# EF and ADO.NET verbs run on a context, query or command receiver. A bare
+# ``SaveChangesAsync()`` is the enclosing class's own method (a JSON-backed
+# store's save), not a DbContext round-trip.
+_RECEIVER_DB_METHODS: frozenset[str] = EF_ASYNC_METHODS | EF_EXEC_METHODS | ADO_METHODS
# Dapper extension verbs (collide with EF / plain method names) — gated on db.
DAPPER_METHODS: frozenset[str] = frozenset(
{
@@ -224,7 +228,7 @@ class CSharpPerfDialect(BasePerfDialect):
root_kind = io_names.get(root)
db_ev = has_db_import or root_kind == "db"
- if method in EF_ASYNC_METHODS or method in EF_EXEC_METHODS or method in ADO_METHODS:
+ if is_attribute and method in _RECEIVER_DB_METHODS:
return "db"
if method in HTTP_ASYNC_METHODS:
return "network"
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py
index f8d5feb85..a55809818 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py
@@ -32,9 +32,12 @@ if TYPE_CHECKING:
from tree_sitter import Node
# Spring-Data derived query methods: ``findByName`` / ``getAllByStatus`` /
-# ``countByOwner`` … — an extremely distinctive signature, treated as db without
-# an import gate.
+# ``countByOwner`` … The name alone is not enough (``Settings.getByPrefix``,
+# ``InetAddress.getByAddress`` and ``RoutingNodes.getByShardId`` are in-memory),
+# so :func:`is_repository_call` also asks for a repository-named receiver or a
+# db library import.
_SPRING_DERIVED = re.compile(r"^(find|get|query|count|exists|stream|read|delete)By[A-Z]")
+_REPOSITORY_RECEIVER = re.compile(r"(?:repository|repo|dao)$", re.IGNORECASE)
# JDBC — unambiguous cursor/statement round-trips.
JDBC_METHODS: frozenset[str] = frozenset(
@@ -42,12 +45,17 @@ JDBC_METHODS: frozenset[str] = frozenset(
)
# JPA / Hibernate EntityManager + Query.
JPA_METHODS: frozenset[str] = frozenset(
- {"getResultList", "getSingleResult", "getResultStream", "createQuery", "createNativeQuery"}
+ {"getResultList", "getSingleResult", "getResultStream", "createNativeQuery"}
)
# Spring-Data CrudRepository finishers that are distinctive enough on their own.
-SPRING_REPO_METHODS: frozenset[str] = frozenset(
- {"saveAll", "findAll", "findAllById", "deleteAll", "deleteAllById"}
-)
+SPRING_REPO_METHODS: frozenset[str] = frozenset({"saveAll", "findAllById", "deleteAllById"})
+# Repository / JPA verbs other APIs share (Lucene ``IndexWriter.deleteAll``,
+# ``ModuleFinder.findAll``, Kotlin ``Regex.findAll``, query builders'
+# ``createQuery``): db only with the evidence :func:`is_repository_call` asks for.
+_SHARED_REPO_METHODS: frozenset[str] = frozenset({"createQuery", "findAll", "deleteAll"})
+# Statement, EntityManager and repository verbs: unambiguous, but only on a
+# receiver (a bare ``executeQuery(ctx)`` is the enclosing class's own method).
+RECEIVER_DB_METHODS: frozenset[str] = JDBC_METHODS | JPA_METHODS | SPRING_REPO_METHODS
# Spring RestTemplate — distinctive HTTP round-trip method names.
REST_TEMPLATE_METHODS: frozenset[str] = frozenset(
{"getForObject", "postForObject", "getForEntity", "postForEntity", "exchange", "patchForObject"}
@@ -84,6 +92,58 @@ NET_CONSTRUCTORS: frozenset[str] = frozenset({"Socket", "ServerSocket"})
# find/get/execute/save/count) so generic verbs like ``list`` / ``stream`` /
# ``delete`` do not over-fire in a db-importing file.
AMBIGUOUS_DB: frozenset[str] = frozenset({"find", "get", "execute", "save", "count"})
+# JDBC has no ``find`` / ``get`` / ``save`` / ``count`` verb, so JDBC evidence
+# licenses only ``execute``; the rest need an ORM / repository library.
+_JDBC_AMBIGUOUS_DB: frozenset[str] = frozenset({"execute"})
+# The JDBC types whose import shows a file runs statements.
+_JDBC_EXECUTION_TYPES: frozenset[str] = frozenset(
+ {
+ "Connection",
+ "Statement",
+ "PreparedStatement",
+ "CallableStatement",
+ "ResultSet",
+ "DataSource",
+ "DriverManager",
+ }
+)
+# Names a ``java.sql`` / ``javax.sql`` import binds that run nothing: the
+# package segments and the value, metadata and driver-side types. A file that
+# imports only these (``JDBCType`` in a type mapper, ``DatabaseMetaData``
+# constants) is no evidence that its ``map.get`` is a query. ``SQL*`` names
+# (``SQLException``, ``SQLType``, ...) are matched by prefix.
+_JDBC_PASSIVE_NAMES: frozenset[str] = frozenset(
+ {
+ "java",
+ "javax",
+ "sql",
+ "JDBCType",
+ "Types",
+ "Date",
+ "Time",
+ "Timestamp",
+ "DatabaseMetaData",
+ "ResultSetMetaData",
+ "ParameterMetaData",
+ "DriverPropertyInfo",
+ "BatchUpdateException",
+ "DataTruncation",
+ "Driver",
+ "DriverAction",
+ "Array",
+ "Blob",
+ "Clob",
+ "NClob",
+ "Ref",
+ "RowId",
+ "RowIdLifetime",
+ "Struct",
+ "Savepoint",
+ "Wrapper",
+ "ClientInfoStatus",
+ }
+)
+_JDBC_PASSIVE_PREFIX = re.compile(r"^SQL[A-Z]")
# Heavy clients to hoist, not ``new`` each iteration. A ``new RestTemplate()``
# arrives as an ``object_creation_expression`` whose extracted "method" is the
@@ -103,6 +163,58 @@ JAVA_LOCK_METHODS: frozenset[str] = frozenset({"lock", "lockInterruptibly"})
_PARTITION_CALLS: frozenset[str] = frozenset({"partition", "chunked", "batches"})
+def _is_passive_jdbc_name(name: str) -> bool:
+ # An all-caps name is a static-imported constant (``Types.BIGINT``). No
+ # library evidence is lost: every import also binds its package segments.
+ return name in _JDBC_PASSIVE_NAMES or bool(_JDBC_PASSIVE_PREFIX.match(name)) or name.isupper()
+
+
+def _db_import_evidence(io_names: dict[str, str]) -> str | None:
+ """``"library"``, ``"jdbc"`` or ``None``: what this file's db imports show.
+
+ A db library other than JDBC (Hibernate, JPA, Spring Data, Slick, ...) is
+ ``"library"``; JDBC execution types alone are ``"jdbc"``; passive JDBC
+ names (``JDBCType``, ``SQLException``) show nothing.
+ """
+ jdbc = False
+ for name, kind in io_names.items():
+ if kind != "db" or _is_passive_jdbc_name(name):
+ continue
+ if name not in _JDBC_EXECUTION_TYPES:
+ return "library"
+ jdbc = True
+ return "jdbc" if jdbc else None
+
+
+def ambiguous_db_verbs(io_names: dict[str, str]) -> frozenset[str]:
+ """The :data:`AMBIGUOUS_DB` verbs this file's db imports license.
+
+ Shared by the JVM dialects in place of "any db-kind import".
+ """
+ evidence = _db_import_evidence(io_names)
+ if evidence == "library":
+ return AMBIGUOUS_DB
+ return _JDBC_AMBIGUOUS_DB if evidence == "jdbc" else frozenset()
+
+
+def is_repository_call(method: str, root: str, io_names: dict[str, str]) -> bool:
+ """``repo.findByName(...)`` / ``repo.findAll()``: a repository query, not an in-memory call.
+
+ Needs a derived-query name or a shared repository verb, plus a receiver
+ named like a repository or DAO, or a db library (Spring Data, JPA,
+ Hibernate, ...) imported in the file.
+ """
+ if not (_SPRING_DERIVED.match(method) or method in _SHARED_REPO_METHODS):
+ return False
+ return bool(_REPOSITORY_RECEIVER.search(root)) or _db_import_evidence(io_names) == "library"
+
+
+def _receiver_root(receiver: str) -> str:
+ """``a.b.c`` -> ``a``; ``this.repo.x`` -> ``repo`` (``this`` names no import or receiver)."""
+ parts = receiver.split(".")
+ return parts[1] if parts[0] == "this" and len(parts) > 1 else parts[0]
+
+
class JavaPerfDialect(BasePerfDialect):
language = "java"
markers = frozenset(
@@ -151,7 +263,7 @@ class JavaPerfDialect(BasePerfDialect):
return None
obj = call_node.child_by_field_name("object")
if obj is not None and obj.text:
- return obj.text.decode("utf-8", "replace").split(".")[0]
+ return _receiver_root(obj.text.decode("utf-8", "replace"))
# A bare ``foo()`` (no receiver) — the name is the root.
name = call_node.child_by_field_name("name")
if name is not None and name.text:
@@ -177,17 +289,19 @@ class JavaPerfDialect(BasePerfDialect):
io_names: dict[str, str],
has_db_import: bool,
) -> str | None:
+ # ``has_db_import`` is "any db-kind import", which counts a type-only
+ # ``java.sql.JDBCType``; :func:`ambiguous_db_verbs` reads the same
+ # ``io_names`` and tells the import kinds apart.
root_kind = io_names.get(root)
- db_ev = has_db_import or root_kind == "db"
net_ev = root_kind == "network" or "network" in io_names.values()
if method in FS_CONSTRUCTORS:
return "filesystem"
if method in NET_CONSTRUCTORS:
return "network"
- if method in JDBC_METHODS or method in JPA_METHODS or method in SPRING_REPO_METHODS:
+ if is_attribute and method in RECEIVER_DB_METHODS:
return "db"
- if _SPRING_DERIVED.match(method):
+ if is_repository_call(method, root, io_names):
return "db"
if method in REST_TEMPLATE_METHODS:
return "network"
@@ -199,8 +313,8 @@ class JavaPerfDialect(BasePerfDialect):
return "filesystem"
if method == "exec": # Runtime.getRuntime().exec(...)
return "subprocess"
- if is_attribute and db_ev and method in AMBIGUOUS_DB:
- return "db"
+ if is_attribute and method in AMBIGUOUS_DB:
+ return "db" if method in ambiguous_db_verbs(io_names) else None
return None
def loop_call_marker(
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
index 43836f9f3..fa2019c0e 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
@@ -45,18 +45,17 @@ from typing import TYPE_CHECKING
from .base import BasePerfDialect
from .java import (
- _SPRING_DERIVED,
AMBIGUOUS_DB,
FILES_METHODS,
FS_CONSTRUCTORS,
JAVA_LOCK_METHODS,
JAVA_RESOURCE_CTORS,
JAVA_RESOURCE_METHODS,
- JDBC_METHODS,
- JPA_METHODS,
NET_CONSTRUCTORS,
+ RECEIVER_DB_METHODS,
REST_TEMPLATE_METHODS,
- SPRING_REPO_METHODS,
+ ambiguous_db_verbs,
+ is_repository_call,
)
if TYPE_CHECKING:
@@ -265,7 +264,6 @@ class KotlinPerfDialect(BasePerfDialect):
has_db_import: bool,
) -> str | None:
root_kind = io_names.get(root)
- db_ev = has_db_import or root_kind == "db"
net_ev = root_kind == "network" or "network" in io_names.values()
# JVM interop — verbatim from the Java lexicon.
@@ -273,13 +271,13 @@ class KotlinPerfDialect(BasePerfDialect):
return "filesystem"
if method in NET_CONSTRUCTORS:
return "network"
- if method in JDBC_METHODS or method in JPA_METHODS or method in SPRING_REPO_METHODS:
+ # No receiver requirement here, unlike Java: a Kotlin extension or
+ # scope function calls ``executeUpdate()`` on an implicit receiver.
+ if method in RECEIVER_DB_METHODS:
return "db"
- # A Spring-Data derived query is called on a repository INSTANCE, so the
- # receiver is lower-cased. Requiring that excludes the static-factory
- # collisions the bare ``…By[A-Z]`` pattern otherwise picks up —
- # ``InetAddress.getByName(host)`` matched it in ktor.
- if _SPRING_DERIVED.match(method) and is_attribute and root[:1].islower():
+ # A Spring-Data derived query is called on a repository INSTANCE, never
+ # a static factory such as ``InetAddress.getByName(host)`` (ktor).
+ if is_attribute and is_repository_call(method, root, io_names):
return "db"
if method in REST_TEMPLATE_METHODS:
return "network"
@@ -295,7 +293,11 @@ class KotlinPerfDialect(BasePerfDialect):
return "db"
if is_attribute and net_ev and method in KOTLIN_NET_METHODS:
return "network"
- if is_attribute and db_ev and method in KOTLIN_AMBIGUOUS_DB:
+ if (
+ is_attribute
+ and method in KOTLIN_AMBIGUOUS_DB
+ and method in ambiguous_db_verbs(io_names)
+ ):
return "db"
return None
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/scala.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/scala.py
index ebfec2480..8753dc809 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/scala.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/scala.py
@@ -33,16 +33,15 @@ from typing import TYPE_CHECKING
from .base import BasePerfDialect
from .java import (
- _SPRING_DERIVED,
AMBIGUOUS_DB,
FILES_METHODS,
FS_CONSTRUCTORS,
JAVA_LOCK_METHODS,
JAVA_RESOURCE_CTORS,
- JDBC_METHODS,
- JPA_METHODS,
NET_CONSTRUCTORS,
- SPRING_REPO_METHODS,
+ RECEIVER_DB_METHODS,
+ ambiguous_db_verbs,
+ is_repository_call,
)
if TYPE_CHECKING:
@@ -65,6 +64,7 @@ DOOBIE_METHODS: frozenset[str] = frozenset({"transact"})
# Slick's ``db.run(action)`` - ``run`` is generic, so it is gated on file-level
# db evidence like the shared AMBIGUOUS_DB stratum.
SLICK_AMBIGUOUS_DB: frozenset[str] = frozenset({"run"})
+_SCALA_AMBIGUOUS_DB: frozenset[str] = AMBIGUOUS_DB | SLICK_AMBIGUOUS_DB
# http4s client verbs, gated on network evidence (``expect`` collides with the
# test-assertion vocabulary when un-gated).
HTTP4S_METHODS: frozenset[str] = frozenset({"expect", "fetch"})
@@ -134,7 +134,6 @@ class ScalaPerfDialect(BasePerfDialect):
has_db_import: bool,
) -> str | None:
root_kind = io_names.get(root)
- db_ev = has_db_import or root_kind == "db"
net_ev = root_kind == "network" or "network" in io_names.values()
# JVM interop - verbatim from the Java lexicon.
@@ -142,9 +141,9 @@ class ScalaPerfDialect(BasePerfDialect):
return "filesystem"
if method in NET_CONSTRUCTORS:
return "network"
- if method in JDBC_METHODS or method in JPA_METHODS or method in SPRING_REPO_METHODS:
+ if is_attribute and method in RECEIVER_DB_METHODS:
return "db"
- if _SPRING_DERIVED.match(method):
+ if is_repository_call(method, root, io_names):
return "db"
if root == "Files" and method in FILES_METHODS:
return "filesystem"
@@ -167,8 +166,10 @@ class ScalaPerfDialect(BasePerfDialect):
return "network"
if method in HTTP4S_METHODS and is_attribute and net_ev:
return "network"
- if is_attribute and db_ev and (method in AMBIGUOUS_DB or method in SLICK_AMBIGUOUS_DB):
- return "db"
+ if is_attribute and method in _SCALA_AMBIGUOUS_DB:
+ # Slick's ``run`` needs the same library evidence as ``get``.
+ verb = "get" if method in SLICK_AMBIGUOUS_DB else method
+ return "db" if verb in ambiguous_db_verbs(io_names) else None
return None
# -- loops ----------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/analysis/health/perf/io_boundaries.py b/packages/core/src/repowise/core/analysis/health/perf/io_boundaries.py
index c7c858080..55b6f49ec 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/io_boundaries.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/io_boundaries.py
@@ -35,7 +35,20 @@ from ....ingestion.external_systems.io_kind import classify_io_kind
# Tokens that are syntax, not bindable import names.
_IMPORT_KW: frozenset[str] = frozenset(
- {"import", "from", "as", "require", "const", "let", "var", "default", "type", "typeof", "use"}
+ {
+ "import",
+ "from",
+ "as",
+ "require",
+ "const",
+ "let",
+ "var",
+ "default",
+ "type",
+ "typeof",
+ "use",
+ "static",
+ }
)
diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.py
index 049edf715..a803676c1 100644
--- a/tests/unit/health/test_perf_dialects.py
+++ b/tests/unit/health/test_perf_dialects.py
@@ -81,6 +81,70 @@ _JAVA_CASES = [
[],
"a plain helper call in a loop is not a sink",
),
+ (
+ "class A{void m(java.util.List<String> ks, Settings settings){"
+ "for(String k:ks){ settings.getByPrefix(k); }}}",
+ [],
+ "a ...By[A-Z] name on a non-repository receiver is not a derived query",
+ ),
+ (
+ "class A{void m(java.util.List<byte[]> bs){"
+ "for(byte[] b:bs){ java.net.InetAddress.getByAddress(b); }}}",
+ [],
+ "a static factory named like a derived query is not db",
+ ),
+ (
+ "class A{void m(java.util.List<String> ids, UserRepository userRepository){"
+ "for(String id:ids){ userRepository.findByEmail(id); }}}",
+ [("io_in_loop", "db")],
+ "a derived query on a repository-named receiver is db without an import",
+ ),
+ (
+ "import org.springframework.data.jpa.repository.JpaRepository;\n"
+ "class A{void m(java.util.List<String> ids, Users users){"
+ "for(String id:ids){ users.findByEmail(id); }}}",
+ [("io_in_loop", "db")],
+ "a derived query in a file importing Spring Data is db",
+ ),
+ (
+ "import java.sql.JDBCType;\n"
+ "class A{void m(java.util.List<String> ks, java.util.Map<String,String> m){"
+ "for(String k:ks){ m.get(k); }}}",
+ [],
+ "a type-only java.sql import is not db evidence",
+ ),
+ (
+ "import static java.sql.Types.BIGINT;\n"
+ "class A{void m(java.util.List<String> ks, java.util.Map<String,String> m){"
+ "for(String k:ks){ m.get(k); }}}",
+ [],
+ "a static-imported java.sql constant is not db evidence",
+ ),
+ (
+ "import java.sql.Statement;\n"
+ "class A{void m(java.util.List<String> ks, java.util.Map<String,String> m){"
+ "for(String k:ks){ m.get(k); }}}",
+ [],
+ "JDBC evidence does not license get, which JDBC has no verb for",
+ ),
+ (
+ "import java.sql.Statement;\n"
+ "class A{void m(java.util.List<String> qs, Statement st){"
+ "for(String q:qs){ st.execute(q); }}}",
+ [("io_in_loop", "db")],
+ "JDBC evidence licenses execute",
+ ),
+ (
+ "class A{void m(java.util.List<Ctx> cs){for(Ctx c:cs){ executeQuery(c); }}}",
+ [],
+ "a bare executeQuery is the class's own method, not a JDBC statement",
+ ),
+ (
+ "class A{void m(java.util.List<java.lang.module.ModuleFinder> fs){"
+ "for(java.lang.module.ModuleFinder f:fs){ f.findAll(); }}}",
+ [],
+ "a shared repository verb on a non-repository receiver is not db",
+ ),
]
@@ -199,6 +263,13 @@ _CSHARP_CASES = [
[("io_in_loop", "db"), ("serial_await_in_loop", "db")],
"EF *Async family is unambiguous db (no import gate)",
),
+ (
+ "class A{ void M(System.Collections.Generic.List<int> ids){"
+ "foreach(var id in ids){ _ = SaveChangesAsync(); }}"
+ " System.Threading.Tasks.Task SaveChangesAsync() => null; }",
+ [],
+ "a bare SaveChangesAsync is the class's own method, not a DbContext call",
+ ),
]
@@ -1160,6 +1231,18 @@ _KOTLIN_CASES = [
[("io_in_loop", "db")],
"a derived query on a repository instance still classifies",
),
+ (
+ "fun m(ks: List<String>, settings: Settings) {\n"
+ " for (k in ks) {\n settings.getByPrefix(k)\n }\n}\n",
+ [],
+ "a lower-case receiver alone does not make a ...By[A-Z] call a derived query",
+ ),
+ (
+ "fun m(texts: List<String>, re: Regex) {\n"
+ " for (t in texts) {\n re.findAll(t)\n }\n}\n",
+ [],
+ "Regex.findAll is not a repository findAll",
+ ),
(
"fun m(xs: List<String>) {\n"
' for (x in xs) {\n val r = Regex("\\\\b$x\\\\b")\n }\n}\n',