Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache iota sizes to improve performance #794

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading