Skip to content

Commit 63d88fb

Browse files
authored
Merge pull request #50 from MetricsHub/copilot/sub-pr-49
perf: switch wildcard dedup to O(1) set-based approach
2 parents f4541e1 + a4a6b02 commit 63d88fb

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/main/java/org/metricshub/jflat/JFlat.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.io.StringReader;
2626
import java.text.ParseException;
2727
import java.util.ArrayList;
28+
import java.util.HashSet;
2829
import java.util.Map.Entry;
30+
import java.util.Set;
2931
import java.util.TreeMap;
3032
import javax.json.Json;
3133
import javax.json.JsonArray;
@@ -399,6 +401,8 @@ public StringBuilder toCSV(String csvEntryKey, String[] csvProperties, String se
399401

400402
// Temporary list where we will store the new entries
401403
ArrayList<String> newEntries = new ArrayList<String>();
404+
// Set of lower-cased paths already added, used for O(1) case-insensitive dedup
405+
Set<String> seenLowercasePaths = new HashSet<String>();
402406

403407
for (String existingEntry : entries) {
404408
if (isWildcard) {
@@ -436,18 +440,10 @@ public StringBuilder toCSV(String csvEntryKey, String[] csvProperties, String se
436440
childName = remainder.substring(0, Math.min(slashPos, bracketPos));
437441
}
438442

439-
// Add the child path if it's valid and not already in the list
443+
// Add the child path if it's valid and not already seen (case-insensitive)
440444
if (!childName.isEmpty()) {
441445
String childPath = prefix + childName;
442-
// Deduplicate (case-insensitive, consistent with the TreeMap)
443-
boolean alreadyAdded = false;
444-
for (String added : newEntries) {
445-
if (added.equalsIgnoreCase(childPath)) {
446-
alreadyAdded = true;
447-
break;
448-
}
449-
}
450-
if (!alreadyAdded) {
446+
if (seenLowercasePaths.add(childPath.toLowerCase())) {
451447
newEntries.add(childPath);
452448
}
453449
}

0 commit comments

Comments
 (0)