Skip to content

Commit

Permalink
Improve performance of DataComparator, required for CyclopsMC/Integra…
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 19, 2024
1 parent 7e7370e commit 8a5b73f
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DataComparator implements Comparator<DataComponentMap> {
public static Comparator<DataComponentMap> INSTANCE = new DataComparator(null);

private final Set<ResourceLocation> ignoreDataComponentTypes;
private Set<DataComponentType<?>> ignoreDataComponentTypeInstances;

public DataComparator(@Nullable Set<ResourceLocation> ignoreDataComponentTypes) {
this.ignoreDataComponentTypes = ignoreDataComponentTypes;
Expand All @@ -35,16 +36,38 @@ public int compare(DataComponentMap o1, DataComponentMap o2) {

protected int compare(DataComponentMap o1, DataComponentMap o2, @Nullable Set<ResourceLocation> ignoreDataComponentTypes) {
// Return immediately if identical
if (o1 == o2) {
if (o1 == o2 || o1.equals(o2)) {
return 0;
}

// Determine keys to compare
Set<DataComponentType<?>> k1 = o1.keySet();
Set<DataComponentType<?>> 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
Expand Down

0 comments on commit 8a5b73f

Please sign in to comment.