From 71096c8bdd6dfe030b6da446b81b8c998c1e2a33 Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Sat, 9 Sep 2023 19:06:31 +0200 Subject: [PATCH] Optimize how ChunkSearcher results are assigned to PortalESP groups Removed the ForkJoinPool and made sure the groups are only updated when necessary. --- .../net/wurstclient/hacks/PortalEspHack.java | 92 +++++++------------ .../wurstclient/util/ChunkSearcherMulti.java | 5 +- 2 files changed, 38 insertions(+), 59 deletions(-) diff --git a/src/main/java/net/wurstclient/hacks/PortalEspHack.java b/src/main/java/net/wurstclient/hacks/PortalEspHack.java index f86966edcb..3df54992ed 100644 --- a/src/main/java/net/wurstclient/hacks/PortalEspHack.java +++ b/src/main/java/net/wurstclient/hacks/PortalEspHack.java @@ -8,11 +8,14 @@ package net.wurstclient.hacks; import java.awt.Color; -import java.util.*; -import java.util.concurrent.Callable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutorService; -import java.util.concurrent.ForkJoinPool; -import java.util.concurrent.ForkJoinTask; import java.util.stream.Collectors; import org.lwjgl.opengl.GL11; @@ -23,7 +26,6 @@ import net.minecraft.block.Blocks; import net.minecraft.client.render.GameRenderer; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.chunk.Chunk; import net.wurstclient.Category; @@ -43,7 +45,6 @@ import net.wurstclient.util.ChunkUtils; import net.wurstclient.util.MinPriorityThreadFactory; import net.wurstclient.util.RenderUtils; -import net.wurstclient.util.RotationUtils; public final class PortalEspHack extends Hack implements UpdateListener, PacketInputListener, CameraTransformViewBobbingListener, RenderListener @@ -85,10 +86,9 @@ public final class PortalEspHack extends Hack implements UpdateListener, new HashMap<>(); private final Set chunksToUpdate = Collections.synchronizedSet(new HashSet<>()); - private ExecutorService pool1; + private ExecutorService threadPool; - private ForkJoinPool pool2; - private ForkJoinTask> getMatchingBlocksTask; + private boolean groupsUpToDate; public PortalEspHack() { @@ -104,8 +104,8 @@ public PortalEspHack() @Override public void onEnable() { - pool1 = MinPriorityThreadFactory.newFixedThreadPool(); - pool2 = new ForkJoinPool(); + threadPool = MinPriorityThreadFactory.newFixedThreadPool(); + groupsUpToDate = false; EVENTS.add(UpdateListener.class, this); EVENTS.add(PacketInputListener.class, this); @@ -123,12 +123,8 @@ public void onDisable() EVENTS.remove(CameraTransformViewBobbingListener.class, this); EVENTS.remove(RenderListener.class, this); - stopPool2Tasks(); - pool1.shutdownNow(); - pool2.shutdownNow(); - + threadPool.shutdownNow(); chunksToUpdate.clear(); - groups.forEach(PortalEspBlockGroup::clear); PortalEspRenderer.closeBuffers(); } @@ -153,28 +149,22 @@ public void onCameraTransformViewBobbing( @Override public void onUpdate() { - ArrayList currentBlockList = - groups.parallelStream().map(PortalEspBlockGroup::getBlock) + ArrayList blockList = + groups.stream().map(PortalEspBlockGroup::getBlock) .collect(Collectors.toCollection(ArrayList::new)); - BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos()); int dimensionId = MC.world.getRegistryKey().toString().hashCode(); - addSearchersInRange(currentBlockList, dimensionId); + addSearchersInRange(blockList, dimensionId); removeSearchersOutOfRange(dimensionId); - replaceSearchersWithDifferences(currentBlockList, dimensionId); - replaceSearchersWithChunkUpdate(currentBlockList, dimensionId); + replaceSearchersWithDifferences(blockList, dimensionId); + replaceSearchersWithChunkUpdate(blockList, dimensionId); if(!areAllChunkSearchersDone()) return; - if(getMatchingBlocksTask == null) - startGetMatchingBlocksTask(eyesPos); - - if(!getMatchingBlocksTask.isDone()) - return; - - getMatchingBlocksFromTask(); + if(!groupsUpToDate) + updateGroupBoxes(); } @Override @@ -280,29 +270,20 @@ private void replaceSearchersWithChunkUpdate( private void addSearcher(Chunk chunk, ArrayList blockList, int dimensionId) { + groupsUpToDate = false; ChunkSearcherMulti searcher = new ChunkSearcherMulti(chunk, blockList, dimensionId); searchers.put(chunk.getPos(), searcher); - searcher.startSearching(pool1); + searcher.startSearching(threadPool); } private void removeSearcher(ChunkSearcherMulti searcher) { - stopPool2Tasks(); - + groupsUpToDate = false; searchers.remove(searcher.getPos()); searcher.cancelSearching(); } - private void stopPool2Tasks() - { - if(getMatchingBlocksTask != null) - { - getMatchingBlocksTask.cancel(true); - getMatchingBlocksTask = null; - } - } - private boolean areAllChunkSearchersDone() { for(ChunkSearcherMulti searcher : searchers.values()) @@ -312,26 +293,23 @@ private boolean areAllChunkSearchersDone() return true; } - private void startGetMatchingBlocksTask(BlockPos eyesPos) + private void updateGroupBoxes() { - Callable> task = - () -> searchers.values().parallelStream() - .flatMap(searcher -> searcher.getMatchingBlocks().stream()) - .sorted(Comparator.comparingInt( - result -> eyesPos.getManhattanDistance(result.getPos()))) - .collect(Collectors.toCollection(ArrayList::new)); + groups.forEach(PortalEspBlockGroup::clear); + searchers.values().stream() + .flatMap(ChunkSearcherMulti::getMatchingBlocks) + .forEachOrdered(this::addToGroupBoxes); - getMatchingBlocksTask = pool2.submit(task); + groupsUpToDate = true; } - private void getMatchingBlocksFromTask() + private void addToGroupBoxes(Result result) { - groups.forEach(PortalEspBlockGroup::clear); - - for(Result result : getMatchingBlocksTask.join()) - groups.parallelStream() - .filter(group -> group.getBlock().getClass() == result - .getBlock().getClass()) - .forEach(group -> group.add(result.getPos())); + for(PortalEspBlockGroup group : groups) + if(result.getBlock() == group.getBlock()) + { + group.add(result.getPos()); + break; + } } } diff --git a/src/main/java/net/wurstclient/util/ChunkSearcherMulti.java b/src/main/java/net/wurstclient/util/ChunkSearcherMulti.java index ec19771292..528e5ee890 100644 --- a/src/main/java/net/wurstclient/util/ChunkSearcherMulti.java +++ b/src/main/java/net/wurstclient/util/ChunkSearcherMulti.java @@ -11,6 +11,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.stream.Stream; import net.minecraft.block.Block; import net.minecraft.client.world.ClientWorld; @@ -125,9 +126,9 @@ public int getDimensionId() return dimensionId; } - public ArrayList getMatchingBlocks() + public Stream getMatchingBlocks() { - return matchingBlocks; + return matchingBlocks.stream(); } public ChunkSearcherMulti.Status getStatus()