From 75b15ba671acbf7c9d09ea110a2d0de439a495a8 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:43:47 +0200 Subject: [PATCH] improve count statements performance --- .../core/check/structure/DuplicateCode.java | 2 +- .../core/integrated/DuplicateCodeFinder.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/autograder-core/src/main/java/de/firemage/autograder/core/check/structure/DuplicateCode.java b/autograder-core/src/main/java/de/firemage/autograder/core/check/structure/DuplicateCode.java index b2951d56..144f0d64 100644 --- a/autograder-core/src/main/java/de/firemage/autograder/core/check/structure/DuplicateCode.java +++ b/autograder-core/src/main/java/de/firemage/autograder/core/check/structure/DuplicateCode.java @@ -45,7 +45,7 @@ private static boolean isAnyStatementIn(DuplicateCodeFinder.DuplicateCode duplic public static boolean isConsideredDuplicateCode(List left, List right) { var duplicate = new DuplicateCodeFinder.DuplicateCode(left, right); - if (duplicate.size() < MINIMUM_DUPLICATE_STATEMENT_SIZE) { + if (!duplicate.isMoreThanOrEqualTo(MINIMUM_DUPLICATE_STATEMENT_SIZE)) { return false; } diff --git a/autograder-core/src/main/java/de/firemage/autograder/core/integrated/DuplicateCodeFinder.java b/autograder-core/src/main/java/de/firemage/autograder/core/integrated/DuplicateCodeFinder.java index 5e3b658e..ba9fdf24 100644 --- a/autograder-core/src/main/java/de/firemage/autograder/core/integrated/DuplicateCodeFinder.java +++ b/autograder-core/src/main/java/de/firemage/autograder/core/integrated/DuplicateCodeFinder.java @@ -108,6 +108,19 @@ private static int countStatements(CtStatement ctStatement) { return Math.max(count, 1); } + public boolean isMoreThanOrEqualTo(int threshold) { + int size = 0; + // TODO: this could be optimized in the future by stopping getElements when the threshold is reached + for (var statement : this.left) { + size += countStatements(statement); + if (size >= threshold) { + return true; + } + } + + return Math.max(size, 1) >= threshold; + } + public List differences() { return StreamSupport.stream(zip(this.left, this.right).spliterator(), false) .flatMap(entry -> StructuralEqualsVisitor.findDifferences(entry.getKey(), entry.getValue()).stream())