Skip to content

Commit

Permalink
Cache iota sizes to improve performance (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamsTheNerd authored Nov 25, 2024
2 parents 7dde078 + e709b3d commit ddb8174
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public int size() {
return 1;
}

public int depth() {
return 1;
}

public Component display() {
return this.type.display(this.serialize());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import net.minecraft.util.FormattedCharSequence;

import javax.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -81,33 +79,13 @@ public static boolean isTooLargeToSerialize(Iterable<Iota> examinee) {
}

private static boolean isTooLargeToSerialize(Iterable<Iota> examinee, int startingCount) {
// We don't recurse here, just a work queue (or work stack, if we liked.)
// Each element is a found sub-iota, and how deep it is.
//
// TODO: is it worth trying to cache the depth and size statically on a SpellList.
var listsToExamine = new ArrayDeque<>(Collections.singleton(new Pair<>(examinee, 0)));
int totalEltsFound = startingCount; // count the first list
while (!listsToExamine.isEmpty()) {
var iotaPair = listsToExamine.removeFirst();
var sublist = iotaPair.getFirst();
int depth = iotaPair.getSecond();
for (var iota : sublist) {
totalEltsFound += iota.size();
if (totalEltsFound >= HexIotaTypes.MAX_SERIALIZATION_TOTAL) {
return true; // too bad
}
var subIotas = iota.subIotas();
if (subIotas != null) {
if (depth + 1 >= HexIotaTypes.MAX_SERIALIZATION_DEPTH) {
return true;
}

listsToExamine.addLast(new Pair<>(subIotas, depth + 1));
}
}
int totalSize = startingCount;
for (Iota iota : examinee) {
if (iota.depth() >= HexIotaTypes.MAX_SERIALIZATION_DEPTH)
return true;
totalSize += iota.size();
}
// we made it!
return false;
return totalSize >= HexIotaTypes.MAX_SERIALIZATION_TOTAL;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@
import java.util.ArrayList;
import java.util.List;

import static java.lang.Math.max;

/**
* This is a <i>wrapper</i> for {@link SpellList}.
*/
public class ListIota extends Iota {
private final int depth;
private final int size;

public ListIota(@NotNull SpellList list) {
super(HexIotaTypes.LIST, list);
int maxChildDepth = 0;
int totalSize = 1;
for (Iota iota : list) {
totalSize += iota.size();
maxChildDepth = max(maxChildDepth, iota.depth());
}
depth = maxChildDepth + 1;
size = totalSize;
}

public ListIota(@NotNull List<Iota> list) {
Expand Down Expand Up @@ -78,6 +91,16 @@ public boolean toleratesOther(Iota that) {
return this.getList();
}

@Override
public int size() {
return size;
}

@Override
public int depth() {
return depth;
}

public static IotaType<ListIota> TYPE = new IotaType<>() {
@Nullable
@Override
Expand Down

0 comments on commit ddb8174

Please sign in to comment.