From ca3bf94a48eaca6d052e290e55daf5786128d170 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Tue, 20 Aug 2024 20:25:46 +0200 Subject: [PATCH] Prevent allocation in loops --- .../staropensource/sosengine/base/Engine.java | 10 ++++--- .../StarOpenSourceVersioningSystem.java | 2 +- .../internal/placeholders/JvmArguments.java | 3 ++- .../base/utility/DependencyResolver.java | 26 ++++++++++++------- .../sosengine/base/utility/Miscellaneous.java | 2 ++ .../sosengine/unittests/UnitLogger.java | 3 ++- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/base/src/main/java/de/staropensource/sosengine/base/Engine.java b/base/src/main/java/de/staropensource/sosengine/base/Engine.java index 64f8c2d6..bb2c8bfe 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -325,15 +325,17 @@ public final class Engine extends SubsystemClass { Set<@NotNull Class> annotatedClasses = reflections.getTypesAnnotatedWith(EngineSubsystem.class); // Initialize classes, get dependency vector and add to 'subsystemsMutable' + Object initializedClassRaw; + SubsystemClass initializedClass; for (Class clazz : annotatedClasses) { try { // Create new instance - Object initializedClassRaw = clazz.getDeclaredConstructor().newInstance(); - SubsystemClass initializedClass = null; + initializedClassRaw = clazz.getDeclaredConstructor().newInstance(); + initializedClass = null; // Check if class implements SubsystemMainClass - if (initializedClassRaw instanceof SubsystemClass subsystemInstance) - initializedClass = subsystemInstance; + if (initializedClassRaw instanceof SubsystemClass) + initializedClass = (SubsystemClass) initializedClassRaw; else logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Does not implement " + SubsystemClass.class.getName()); diff --git a/base/src/main/java/de/staropensource/sosengine/base/data/versioning/StarOpenSourceVersioningSystem.java b/base/src/main/java/de/staropensource/sosengine/base/data/versioning/StarOpenSourceVersioningSystem.java index dda30bdf..f76a73bb 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/data/versioning/StarOpenSourceVersioningSystem.java +++ b/base/src/main/java/de/staropensource/sosengine/base/data/versioning/StarOpenSourceVersioningSystem.java @@ -131,7 +131,7 @@ public final class StarOpenSourceVersioningSystem implements VersioningSystem { String companion = null; String fork = null; - // Iterate through all chraracters + // Iterate through all characters for (Character character : versionString.toCharArray()) { switch (parsingId) { case 0 -> { // 'v' character diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/JvmArguments.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/JvmArguments.java index 3c261920..8fa89446 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/JvmArguments.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/JvmArguments.java @@ -44,7 +44,8 @@ public final class JvmArguments implements Placeholder { StringBuilder arguments = new StringBuilder(); for (String argument : JvmInformation.getArguments()) { - if (!arguments.isEmpty()) arguments.append(" "); + if (!arguments.isEmpty()) + arguments.append(" "); arguments .append("\"") diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java b/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java index be8f5ab1..a2ed4494 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java @@ -158,17 +158,23 @@ public final class DependencyResolver { // provides + // 0 = identifier + // 1 = version equal + // 2 = version smaller + // 3 = version bigger + int mode; + boolean[] duplicateCheck; + StringBuilder identifier; + StringBuilder versionEqual; + StringBuilder versionSmaller; + StringBuilder versionBigger; for (String dependency : vector.getDependencies()) { - // 0 = identifier - // 1 = version equal - // 2 = version smaller - // 3 = version bigger - int mode = 0; - boolean[] duplicateCheck = new boolean[3]; - StringBuilder identifier = new StringBuilder(); - StringBuilder versionEqual = new StringBuilder(); - StringBuilder versionSmaller = new StringBuilder(); - StringBuilder versionBigger = new StringBuilder(); + mode = 0; + duplicateCheck = new boolean[3]; + identifier = new StringBuilder(); + versionEqual = new StringBuilder(); + versionSmaller = new StringBuilder(); + versionBigger = new StringBuilder(); // Get variables for (char character : dependency.toCharArray()) { diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java index 569dc907..78671b8a 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java @@ -126,6 +126,7 @@ public final class Miscellaneous { for (String separator : separators) if (countOccurrences(string, separator) == requiredOccurrences) return separator; + return null; } @@ -142,6 +143,7 @@ public final class Miscellaneous { for (String separator : separators) if (countOccurrences(string, separator) >= minimumOccurrences) return separator; + return null; } diff --git a/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java b/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java index 0871c754..81715f82 100644 --- a/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java +++ b/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java @@ -69,8 +69,9 @@ public class UnitLogger { levelName = "TEST"; StringBuilder args = new StringBuilder(); + boolean stringQuotes; for (Object arg : additionalStuff) { - boolean stringQuotes = arg instanceof String; + stringQuotes = arg instanceof String; // Print full class path for enums if (arg instanceof Enum) {