diff options
| author | shedaniel <daniel@shedaniel.me> | 2020-02-18 18:15:30 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2020-02-18 18:15:30 +0800 |
| commit | a70fbdbde00410a732d1bdc5cee3f0a6d8048675 (patch) | |
| tree | aef2891a89ce6be5f0806cd9f9b17f8f5635ca16 /src/main/java | |
| parent | bbf531e70a04d2cbaa894f3ad2e6fe8d3d569efc (diff) | |
| download | RoughlyEnoughItems-a70fbdbde00410a732d1bdc5cee3f0a6d8048675.tar.gz RoughlyEnoughItems-a70fbdbde00410a732d1bdc5cee3f0a6d8048675.tar.bz2 RoughlyEnoughItems-a70fbdbde00410a732d1bdc5cee3f0a6d8048675.zip | |
4.0.3-unstable
- Adds async search
- Adds debug search option
- Set entry size range from 50%-400% to 25%-400%
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src/main/java')
4 files changed, 71 insertions, 9 deletions
diff --git a/src/main/java/me/shedaniel/rei/api/ConfigObject.java b/src/main/java/me/shedaniel/rei/api/ConfigObject.java index bc60eef0c..e882f2e9d 100644 --- a/src/main/java/me/shedaniel/rei/api/ConfigObject.java +++ b/src/main/java/me/shedaniel/rei/api/ConfigObject.java @@ -122,4 +122,12 @@ public interface ConfigObject { List<EntryStack> getFilteredStacks(); + @ApiStatus.Experimental + boolean shouldAsyncSearch(); + + @ApiStatus.Experimental + int getNumberAsyncSearch(); + + @ApiStatus.Experimental + boolean doDebugSearchTimeRequired(); } diff --git a/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java b/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java index c96e1cd9a..a309fc3db 100644 --- a/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java +++ b/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java @@ -40,7 +40,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.*; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -633,12 +633,43 @@ public class EntryListWidget extends WidgetWithBounds { boolean checkCraftable = ConfigManager.getInstance().isCraftableOnlyEnabled() && !ScreenHelper.inventoryStacks.isEmpty(); List<EntryStack> workingItems = checkCraftable ? RecipeHelper.getInstance().findCraftableEntriesByItems(CollectionUtils.map(ScreenHelper.inventoryStacks, EntryStack::create)) : null; List<EntryStack> stacks = EntryRegistry.getInstance().getPreFilteredList(); - if (stacks instanceof CopyOnWriteArrayList) { - for (EntryStack stack : stacks) { - if (canLastSearchTermsBeAppliedTo(stack)) { - if (workingItems != null && CollectionUtils.findFirstOrNullEqualsEntryIgnoreAmount(workingItems, stack) == null) - continue; - list.add(stack.copy().setting(EntryStack.Settings.RENDER_COUNTS, EntryStack.Settings.FALSE).setting(EntryStack.Settings.Item.RENDER_ENCHANTMENT_GLINT, RENDER_ENCHANTMENT_GLINT)); + if (stacks instanceof CopyOnWriteArrayList && !stacks.isEmpty()) { + if (ConfigObject.getInstance().shouldAsyncSearch()) { + int size = ConfigObject.getInstance().getNumberAsyncSearch(); + List<CompletableFuture<List<EntryStack>>> completableFutures = Lists.newArrayList(); + for (int i = 0; i < stacks.size(); i += size) { + int[] start = {i}; + completableFutures.add(CompletableFuture.supplyAsync(() -> { + int end = Math.min(stacks.size() - 1, start[0] + size); + List<EntryStack> filtered = Lists.newArrayList(); + for (; start[0] < end; start[0]++) { + EntryStack stack = stacks.get(start[0]); + if (canLastSearchTermsBeAppliedTo(stack)) { + if (workingItems != null && CollectionUtils.findFirstOrNullEqualsEntryIgnoreAmount(workingItems, stack) == null) + continue; + filtered.add(stack.copy().setting(EntryStack.Settings.RENDER_COUNTS, EntryStack.Settings.FALSE).setting(EntryStack.Settings.Item.RENDER_ENCHANTMENT_GLINT, RENDER_ENCHANTMENT_GLINT)); + } + } + return filtered; + })); + } + try { + CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).get(30, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + for (CompletableFuture<List<EntryStack>> future : completableFutures) { + List<EntryStack> now = future.getNow(null); + if (now != null) + list.addAll(now); + } + } else { + for (EntryStack stack : stacks) { + if (canLastSearchTermsBeAppliedTo(stack)) { + if (workingItems != null && CollectionUtils.findFirstOrNullEqualsEntryIgnoreAmount(workingItems, stack) == null) + continue; + list.add(stack.copy().setting(EntryStack.Settings.RENDER_COUNTS, EntryStack.Settings.FALSE).setting(EntryStack.Settings.Item.RENDER_ENCHANTMENT_GLINT, RENDER_ENCHANTMENT_GLINT)); + } } } } @@ -678,7 +709,7 @@ public class EntryListWidget extends WidgetWithBounds { favoritesListWidget.updateSearch(this, searchTerm); long ended = System.nanoTime(); long time = ended - started; - if (RoughlyEnoughItemsCore.isDebugModeEnabled()) + if (ConfigObject.getInstance().doDebugSearchTimeRequired()) RoughlyEnoughItemsCore.LOGGER.info("[REI] Search Used: %.2fms", time * 1e-6); updateEntriesPosition(); } diff --git a/src/main/java/me/shedaniel/rei/impl/ConfigObjectImpl.java b/src/main/java/me/shedaniel/rei/impl/ConfigObjectImpl.java index 8457f09aa..26cac3fc5 100644 --- a/src/main/java/me/shedaniel/rei/impl/ConfigObjectImpl.java +++ b/src/main/java/me/shedaniel/rei/impl/ConfigObjectImpl.java @@ -289,6 +289,24 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { return filtering.filteredStacks; } + @Override + @ApiStatus.Experimental + public boolean shouldAsyncSearch() { + return performance.asyncSearch; + } + + @Override + @ApiStatus.Experimental + public int getNumberAsyncSearch() { + return performance.numberAsyncSearch; + } + + @Override + @ApiStatus.Experimental + public boolean doDebugSearchTimeRequired() { + return technical.debugSearchTimeRequired; + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) @interface DontApplyFieldName {} @@ -348,7 +366,7 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { @Comment("Declares the location of the favorites list.") private boolean displayFavoritesOnTheLeft = true; @Comment("Declares whether favorites tooltip should be displayed.") private boolean displayFavoritesTooltip = false; @Comment("Declares whether favorites will be searched.") private boolean searchFavorites = true; - @UsePercentage(min = 0.5, max = 4.0) private double entrySize = 1.0; + @UsePercentage(min = 0.25, max = 4.0) private double entrySize = 1.0; private boolean useCompactTabs = true; private boolean lowerConfigButton = false; } @@ -360,6 +378,7 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { @Comment("Declares the command used to change weather.") private String weatherCommand = "/weather {weather}"; private boolean registerRecipesInAnotherThread = true; private boolean debugRenderTimeRequired = false; + @Comment("Experimental: Declares whether search time should be debugged.") private boolean debugSearchTimeRequired = false; } public static class Modules { @@ -373,6 +392,9 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { public static class Performance { @Comment("Whether REI should render entry's enchantment glint") private boolean renderEntryEnchantmentGlint = true; private boolean newFastEntryRendering = true; + @Comment("Experimental: Declares whether REI should search async.") private boolean asyncSearch = true; + @Comment("Experimental: Declares how many entries should be grouped one async search.") @ConfigEntry.BoundedDiscrete(min = 25, max = 400) + private int numberAsyncSearch = 75; } public static class Filtering { diff --git a/src/main/java/me/shedaniel/rei/tests/plugin/REITestPlugin.java b/src/main/java/me/shedaniel/rei/tests/plugin/REITestPlugin.java index 40463b31d..7d9efa8e9 100644 --- a/src/main/java/me/shedaniel/rei/tests/plugin/REITestPlugin.java +++ b/src/main/java/me/shedaniel/rei/tests/plugin/REITestPlugin.java @@ -51,6 +51,7 @@ public class REITestPlugin implements REIPluginV0 { public EntryStack transformStack(EntryStack stack) { stack.setAmount(random.nextInt(Byte.MAX_VALUE)); + stack.setting(EntryStack.Settings.CHECK_AMOUNT, EntryStack.Settings.TRUE); return stack; } |
