aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2022-04-12 18:23:52 +0800
committershedaniel <daniel@shedaniel.me>2022-04-12 18:23:52 +0800
commit2d787c6e3a1ce87838434c4acdf08ad1f8f5e80c (patch)
tree9eb4863a3e5e6288d88694e86eae7a2e4eef970e /runtime
parent512ceb8aa4c1ac1bcf8233612b5e6a6fd575d790 (diff)
downloadRoughlyEnoughItems-2d787c6e3a1ce87838434c4acdf08ad1f8f5e80c.tar.gz
RoughlyEnoughItems-2d787c6e3a1ce87838434c4acdf08ad1f8f5e80c.tar.bz2
RoughlyEnoughItems-2d787c6e3a1ce87838434c4acdf08ad1f8f5e80c.zip
Try to prevent async search cache from getting stuck
Diffstat (limited to 'runtime')
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java114
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/SearchFilterPrepareWatcher.java4
2 files changed, 61 insertions, 57 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java
index 0844257ad..227d09b4d 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java
@@ -242,72 +242,74 @@ public class Argument<T, R> {
public static void prepareFilter(Collection<EntryStack<?>> stacks, Collection<ArgumentType<?, ?>> argumentTypes) {
if (prepareStage != null || currentStages != null) return;
- prepareStart = Util.getEpochMillis();
- prepareStacks = stacks;
- prepareStage = new IntIntMutablePair(0, argumentTypes.size());
- currentStages = new IntIntPair[argumentTypes.size()];
- List<HashedEntryStackWrapper> hashedStacks = CollectionUtils.map(stacks, HashedEntryStackWrapper::new);
- int searchPartitionSize = ConfigObject.getInstance().getAsyncSearchPartitionSize();
- boolean async = ConfigObject.getInstance().shouldAsyncSearch() && stacks.size() > searchPartitionSize * 4;
- List<CompletableFuture<Long2ObjectMap<Object>>> futures = Lists.newArrayList();
- List<Pair<ArgumentType<?, ?>, CompletableFuture<Long2ObjectMap<Object>>>> pairs = Lists.newArrayList();
-
- for (ArgumentType<?, ?> argumentType : argumentTypes) {
- prepareStage.first(prepareStage.firstInt() + 1);
- Long2ObjectMap<Object> map = getSearchCache(argumentType);
- IntIntPair currentStage = currentStages[prepareStage.firstInt() - 1] = new IntIntMutablePair(0, hashedStacks.size());
+ try {
+ prepareStart = Util.getEpochMillis();
+ prepareStacks = stacks;
+ prepareStage = new IntIntMutablePair(0, argumentTypes.size());
+ currentStages = new IntIntPair[argumentTypes.size()];
+ List<HashedEntryStackWrapper> hashedStacks = CollectionUtils.map(stacks, HashedEntryStackWrapper::new);
+ int searchPartitionSize = ConfigObject.getInstance().getAsyncSearchPartitionSize();
+ boolean async = ConfigObject.getInstance().shouldAsyncSearch() && stacks.size() > searchPartitionSize * 4;
+ List<CompletableFuture<Long2ObjectMap<Object>>> futures = Lists.newArrayList();
+ List<Pair<ArgumentType<?, ?>, CompletableFuture<Long2ObjectMap<Object>>>> pairs = Lists.newArrayList();
- if (async) {
- for (Collection<HashedEntryStackWrapper> partitionStacks : CollectionUtils.partition(hashedStacks, searchPartitionSize)) {
- CompletableFuture<Long2ObjectMap<Object>> future = CompletableFuture.supplyAsync(() -> {
- Long2ObjectMap<Object> out = new Long2ObjectArrayMap<>(searchPartitionSize + 1);
- for (HashedEntryStackWrapper stack : partitionStacks) {
- if (map.get(stack.hashExact()) == null) {
- Object data = argumentType.cacheData(stack.unwrap());
-
- if (data != null) {
- out.put(stack.hashExact(), data);
+ for (ArgumentType<?, ?> argumentType : argumentTypes) {
+ prepareStage.first(prepareStage.firstInt() + 1);
+ Long2ObjectMap<Object> map = getSearchCache(argumentType);
+ IntIntPair currentStage = currentStages[prepareStage.firstInt() - 1] = new IntIntMutablePair(0, hashedStacks.size());
+
+ if (async) {
+ for (Collection<HashedEntryStackWrapper> partitionStacks : CollectionUtils.partition(hashedStacks, searchPartitionSize)) {
+ CompletableFuture<Long2ObjectMap<Object>> future = CompletableFuture.supplyAsync(() -> {
+ Long2ObjectMap<Object> out = new Long2ObjectArrayMap<>(searchPartitionSize + 1);
+ for (HashedEntryStackWrapper stack : partitionStacks) {
+ if (map.get(stack.hashExact()) == null) {
+ Object data = argumentType.cacheData(stack.unwrap());
+
+ if (data != null) {
+ out.put(stack.hashExact(), data);
+ }
}
}
- }
- return out;
- }).whenComplete((objectLong2ObjectMap, throwable) -> {
- currentStage.first(currentStage.firstInt() + partitionStacks.size());
- });
- futures.add(future);
- pairs.add(Pair.of(argumentType, future));
- }
- } else {
- for (HashedEntryStackWrapper stack : hashedStacks) {
- currentStage.first(currentStage.firstInt() + 1);
-
- if (map.get(stack.hashExact()) == null) {
- Object data = argumentType.cacheData(stack.unwrap());
+ return out;
+ }).whenComplete((objectLong2ObjectMap, throwable) -> {
+ currentStage.first(currentStage.firstInt() + partitionStacks.size());
+ });
+ futures.add(future);
+ pairs.add(Pair.of(argumentType, future));
+ }
+ } else {
+ for (HashedEntryStackWrapper stack : hashedStacks) {
+ currentStage.first(currentStage.firstInt() + 1);
- if (data != null) {
- map.put(stack.hashExact(), data);
+ if (map.get(stack.hashExact()) == null) {
+ Object data = argumentType.cacheData(stack.unwrap());
+
+ if (data != null) {
+ map.put(stack.hashExact(), data);
+ }
}
}
}
}
- }
-
- if (async) {
- try {
- CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(10, TimeUnit.SECONDS);
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
- e.printStackTrace();
- }
- for (Pair<ArgumentType<?, ?>, CompletableFuture<Long2ObjectMap<Object>>> pair : pairs) {
- Long2ObjectMap<Object> now = pair.second().getNow(null);
- if (now != null) getSearchCache(pair.left()).putAll(now);
+
+ if (async) {
+ try {
+ CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(10, TimeUnit.SECONDS);
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {
+ e.printStackTrace();
+ }
+ for (Pair<ArgumentType<?, ?>, CompletableFuture<Long2ObjectMap<Object>>> pair : pairs) {
+ Long2ObjectMap<Object> now = pair.second().getNow(null);
+ if (now != null) getSearchCache(pair.left()).putAll(now);
+ }
}
+ } finally {
+ prepareStart = null;
+ prepareStacks = null;
+ prepareStage = null;
+ currentStages = null;
}
-
- prepareStart = null;
- prepareStacks = null;
- prepareStage = null;
- currentStages = null;
}
public ArgumentType<?, ?> getArgument() {
diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/SearchFilterPrepareWatcher.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/SearchFilterPrepareWatcher.java
index a6b8a641d..f5fa521da 100644
--- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/SearchFilterPrepareWatcher.java
+++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/SearchFilterPrepareWatcher.java
@@ -56,7 +56,9 @@ public class SearchFilterPrepareWatcher implements HintProvider {
int currentStageTotal = currentStage == null ? 0 : currentStage.secondInt();
double prepareStageProgress = prepareStageTotal == 0 ? 0 : prepareStageCurrent / (double) prepareStageTotal;
double currentStageProgress = currentStageTotal == 0 ? 0 : currentStageCurrent / (double) currentStageTotal;
- lastProcess = prepareStageTotal == 0 ? 0 : Math.max(0, prepareStageProgress - (1 - currentStageProgress) / prepareStageTotal);
+ double lastProcess = prepareStageTotal == 0 ? 0 : Math.max(0, prepareStageProgress - (1 - currentStageProgress) / prepareStageTotal);
+ if (lastProcess < 0.05 || lastProcess > 0.95) return Collections.emptyList();
+ this.lastProcess = lastProcess;
return ImmutableList.of(new TranslatableComponent("text.rei.caching.search"),
new TranslatableComponent("text.rei.caching.search.step", prepareStageCurrent, prepareStageTotal, Math.round(lastProcess * 100)));
}