From 8a5b73fda809ea0725bfeb5a9531bb5b8658687f Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Tue, 19 Nov 2024 13:50:54 +0100 Subject: [PATCH] Improve performance of DataComparator, required for CyclopsMC/IntegratedTerminals#139 --- .../ingredient/DataComparator.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cyclops/commoncapabilities/ingredient/DataComparator.java b/src/main/java/org/cyclops/commoncapabilities/ingredient/DataComparator.java index b36f48705..6e7f105ef 100644 --- a/src/main/java/org/cyclops/commoncapabilities/ingredient/DataComparator.java +++ b/src/main/java/org/cyclops/commoncapabilities/ingredient/DataComparator.java @@ -23,6 +23,7 @@ public class DataComparator implements Comparator { public static Comparator INSTANCE = new DataComparator(null); private final Set ignoreDataComponentTypes; + private Set> ignoreDataComponentTypeInstances; public DataComparator(@Nullable Set ignoreDataComponentTypes) { this.ignoreDataComponentTypes = ignoreDataComponentTypes; @@ -35,16 +36,38 @@ public int compare(DataComponentMap o1, DataComponentMap o2) { protected int compare(DataComponentMap o1, DataComponentMap o2, @Nullable Set ignoreDataComponentTypes) { // Return immediately if identical - if (o1 == o2) { + if (o1 == o2 || o1.equals(o2)) { return 0; } // Determine keys to compare Set> k1 = o1.keySet(); Set> k2 = o2.keySet(); + + // If relevant, ignore data components if (ignoreDataComponentTypes != null) { - k1 = k1.stream().filter(k -> !ignoreDataComponentTypes.contains(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(k))).collect(Collectors.toSet()); - k2 = k2.stream().filter(k -> !ignoreDataComponentTypes.contains(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(k))).collect(Collectors.toSet()); + if (ignoreDataComponentTypeInstances == null) { + ignoreDataComponentTypeInstances = ignoreDataComponentTypes.stream().map(BuiltInRegistries.DATA_COMPONENT_TYPE::get).collect(Collectors.toSet()); + } + boolean filterK1 = false; + boolean filterK2 = false; + for (DataComponentType dataComponentType : ignoreDataComponentTypeInstances) { + if (!filterK1 && k1.contains(dataComponentType)) { + filterK1 = true; + } + if (!filterK2 && k2.contains(dataComponentType)) { + filterK2 = true; + } + if (filterK1 && filterK2) { + break; + } + } + if (filterK1) { + k1 = k1.stream().filter(k -> !ignoreDataComponentTypeInstances.contains(k)).collect(Collectors.toSet()); + } + if (filterK2) { + k2 = k2.stream().filter(k -> !ignoreDataComponentTypeInstances.contains(k)).collect(Collectors.toSet()); + } } // Check if keys are equal