Skip to content

Commit 0b47514

Browse files
perf(detector): hoist inline Pattern.compile to static finals (RAN-32) (#69)
Removes regex recompilation from hot paths in three Java detectors. Each inline Pattern.compile() call inside detect() now references a private static final Pattern compiled once at class load. - ConfigDefDetector: VALUE_KEY_RE, QUOTED_STRING_RE - KafkaDetector: QUOTED_TOPIC_RE - SpringRestDetector: BARE_QUOTED_RE Pure refactor — same regex source, same flags. All 344 affected detector tests pass. Co-authored-by: Paperclip <noreply@paperclip.ing>
1 parent cab71a4 commit 0b47514

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/main/java/io/github/randomcodespace/iq/detector/jvm/java/ConfigDefDetector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class ConfigDefDetector extends AbstractJavaParserDetector {
4444
private static final Pattern DEFINE_RE = Pattern.compile("\\.define\\s*\\(\\s*\"([^\"]+)\"");
4545
private static final Pattern VALUE_RE = Pattern.compile("@Value\\s*\\(\\s*\"\\$\\{([^}]+)\\}\"");
4646
private static final Pattern CONFIG_PROPS_RE = Pattern.compile("@ConfigurationProperties\\s*\\(\\s*(?:prefix\\s*=\\s*)?\"([^\"]+)\"");
47+
private static final Pattern VALUE_KEY_RE = Pattern.compile("\"\\$\\{([^}]+)\\}\"");
48+
private static final Pattern QUOTED_STRING_RE = Pattern.compile("\"([^\"]+)\"");
4749

4850
private static final String VALUE_ANNOTATION = "Value";
4951
private static final String CONFIG_PROPS_ANNOTATION = "ConfigurationProperties";
@@ -157,15 +159,15 @@ private DetectorResult detectWithAst(CompilationUnit cu, DetectorContext ctx) {
157159
private Optional<String> extractValueKey(AnnotationExpr ann) {
158160
// @Value("${some.key}") or @Value(value = "${some.key}")
159161
String raw = ann.toString();
160-
Matcher m = Pattern.compile("\"\\$\\{([^}]+)\\}\"").matcher(raw);
162+
Matcher m = VALUE_KEY_RE.matcher(raw);
161163
if (m.find()) return Optional.of(m.group(1));
162164
return Optional.empty();
163165
}
164166

165167
private Optional<String> extractAnnotationStringValue(AnnotationExpr ann) {
166168
// @ConfigurationProperties("prefix") or @ConfigurationProperties(prefix = "prefix")
167169
String raw = ann.toString();
168-
Matcher m = Pattern.compile("\"([^\"]+)\"").matcher(raw);
170+
Matcher m = QUOTED_STRING_RE.matcher(raw);
169171
if (m.find()) return Optional.of(m.group(1));
170172
return Optional.empty();
171173
}

src/main/java/io/github/randomcodespace/iq/detector/jvm/java/KafkaDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class KafkaDetector extends AbstractRegexDetector {
3939
private static final Pattern KAFKA_SEND_RE = Pattern.compile(
4040
"(?:kafkaTemplate|KafkaTemplate)\\s*\\.send\\s*\\(\\s*\"([^\"]+)\"");
4141
private static final Pattern GROUP_ID_RE = Pattern.compile("groupId\\s*=\\s*\"([^\"]+)\"");
42+
private static final Pattern QUOTED_TOPIC_RE = Pattern.compile("\"([^\"]+)\"");
4243

4344
@Override
4445
public String getName() {
@@ -84,7 +85,7 @@ public DetectorResult detect(DetectorContext ctx) {
8485
Matcher m = KAFKA_LISTENER_RE.matcher(lines[i]);
8586
if (!m.find()) {
8687
if (i > 0 && lines[i - 1].contains("@KafkaListener")) {
87-
Matcher fallback = Pattern.compile("\"([^\"]+)\"").matcher(lines[i]);
88+
Matcher fallback = QUOTED_TOPIC_RE.matcher(lines[i]);
8889
if (fallback.find()) {
8990
String topic = fallback.group(1);
9091
String topicId = ensureTopicNode(topic, seenTopics, nodes, registry);

src/main/java/io/github/randomcodespace/iq/detector/jvm/java/SpringRestDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class SpringRestDetector extends AbstractJavaParserDetector {
7474
// ---- HTTP client patterns (for CALLS edge emission) ----
7575
private static final Pattern REST_TEMPLATE_RE = Pattern.compile("RestTemplate");
7676
private static final Pattern WEB_CLIENT_RE = Pattern.compile("WebClient");
77+
private static final Pattern BARE_QUOTED_RE = Pattern.compile("\"([^\"]*)\"");
7778
private static final Pattern FEIGN_CLIENT_RE = Pattern.compile(
7879
"@FeignClient\\s*\\(\\s*(?:name\\s*=\\s*)?[\"']([^\"']+)[\"']");
7980

@@ -342,7 +343,7 @@ private DetectorResult detectWithRegex(DetectorContext ctx) {
342343

343344
String path = extractAttr(attrStr, VALUE_RE);
344345
if (path == null && attrStr != null) {
345-
Matcher bare = Pattern.compile("\"([^\"]*)\"").matcher(attrStr);
346+
Matcher bare = BARE_QUOTED_RE.matcher(attrStr);
346347
if (bare.find()) path = bare.group(1);
347348
}
348349
if (path == null) path = "";

0 commit comments

Comments
 (0)