From 54ba5ecc417097134c83e7d9dc80b925598cb9ef Mon Sep 17 00:00:00 2001 From: George Tay Date: Fri, 26 Jan 2024 15:03:40 +0800 Subject: [PATCH] [#2091] Improve memory usage by refactoring Regex compilation (#2092) Improve memory usage by refactoring Regex compilation Currently, Regex checking is used in conjunction with iteration. This pattern of coding is frowned upon due to the excessive Regex pattern compilation, causing the program to run slower and consume more memory. By moving the Regex pattern compilation outside of the iteration, and by using `Matcher` objects to check if the strings match the Regex performance, we can potentially remove this performance bottleneck. Let's move to refactor the code and remove such instances of Regex use in iterative loops. --- src/main/java/reposense/model/Author.java | 9 +++++++-- src/main/java/reposense/util/StringsUtil.java | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/reposense/model/Author.java b/src/main/java/reposense/model/Author.java index 39438920a8..c790a01f72 100644 --- a/src/main/java/reposense/model/Author.java +++ b/src/main/java/reposense/model/Author.java @@ -5,6 +5,7 @@ import java.nio.file.PathMatcher; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; /** * Represents a Git Author. @@ -74,8 +75,10 @@ public Author(Author another) { * @throws IllegalArgumentException if any of the values do not meet the criteria. */ private static void validateEmails(List emails) throws IllegalArgumentException { + Pattern emailPattern = Pattern.compile(COMMON_EMAIL_REGEX); + for (String email : emails) { - if (!email.matches(COMMON_EMAIL_REGEX)) { + if (!emailPattern.matcher(email).matches()) { throw new IllegalArgumentException(String.format(MESSAGE_UNCOMMON_EMAIL_PATTERN, email)); } } @@ -87,8 +90,10 @@ private static void validateEmails(List emails) throws IllegalArgumentEx * @throws IllegalArgumentException if any of the values do not meet the criteria. */ private static void validateIgnoreGlobs(List ignoreGlobList) throws IllegalArgumentException { + Pattern globPattern = Pattern.compile(COMMON_GLOB_REGEX); + for (String glob : ignoreGlobList) { - if (!glob.matches(COMMON_GLOB_REGEX)) { + if (!globPattern.matcher(glob).matches()) { throw new IllegalArgumentException(String.format(MESSAGE_UNCOMMON_GLOB_PATTERN, glob)); } } diff --git a/src/main/java/reposense/util/StringsUtil.java b/src/main/java/reposense/util/StringsUtil.java index 93411366bb..790c394692 100644 --- a/src/main/java/reposense/util/StringsUtil.java +++ b/src/main/java/reposense/util/StringsUtil.java @@ -15,9 +15,11 @@ public class StringsUtil { public static String filterText(String text, String regex) { String[] split = text.split("\n"); StringBuilder sb = new StringBuilder(); + Pattern regexPattern = Pattern.compile(regex); + for (String line: split) { - if (line.matches(regex)) { - sb.append(line + "\n"); + if (regexPattern.matcher(line).matches()) { + sb.append(line).append("\n"); } }