-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract chunk system threads to general common threads
This allows us to use them for chunk rendering, which should reduce competition between the background executor pool and the chunk system for cpu resources.
- Loading branch information
1 parent
1d60cdb
commit b31223e
Showing
6 changed files
with
101 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ca.spottedleaf.moonrise.common.util; | ||
|
||
import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadPool; | ||
import ca.spottedleaf.moonrise.common.config.PlaceholderConfig; | ||
import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class MoonriseCommon { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ChunkTaskScheduler.class); | ||
|
||
public static final PrioritisedThreadPool WORKER_POOL; | ||
public static final int WORKER_THREADS; | ||
static { | ||
int defaultWorkerThreads = Runtime.getRuntime().availableProcessors() / 2; | ||
if (defaultWorkerThreads <= 4) { | ||
defaultWorkerThreads = defaultWorkerThreads <= 3 ? 1 : 2; | ||
} else { | ||
defaultWorkerThreads = defaultWorkerThreads / 2; | ||
} | ||
defaultWorkerThreads = Integer.getInteger("Moonrise.WorkerThreadCount", Integer.valueOf(defaultWorkerThreads)); | ||
|
||
int workerThreads = PlaceholderConfig.workerThreads; | ||
|
||
if (workerThreads < 0) { | ||
workerThreads = defaultWorkerThreads; | ||
} else { | ||
workerThreads = Math.max(1, workerThreads); | ||
} | ||
|
||
WORKER_POOL = new PrioritisedThreadPool( | ||
"Moonrise Chunk System Worker Pool", workerThreads, | ||
(final Thread thread, final Integer id) -> { | ||
thread.setName("Moonrise Chunk System Worker #" + id.intValue()); | ||
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | ||
@Override | ||
public void uncaughtException(final Thread thread, final Throwable throwable) { | ||
LOGGER.error("Uncaught exception in thread " + thread.getName(), throwable); | ||
} | ||
}); | ||
}, (long)(20.0e6)); // 20ms | ||
WORKER_THREADS = workerThreads; | ||
} | ||
|
||
private MoonriseCommon() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/ca/spottedleaf/moonrise/mixin/render/SectionRenderDispatcherMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ca.spottedleaf.moonrise.mixin.render; | ||
|
||
import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor; | ||
import ca.spottedleaf.moonrise.common.util.MoonriseCommon; | ||
import net.minecraft.client.renderer.chunk.SectionRenderDispatcher; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
@Mixin(SectionRenderDispatcher.class) | ||
public abstract class SectionRenderDispatcherMixin { | ||
|
||
@Unique | ||
private static final PrioritisedExecutor RENDER_EXECUTOR = MoonriseCommon.WORKER_POOL.createExecutor( | ||
"Moonrise Render Executor", 1, MoonriseCommon.WORKER_THREADS | ||
); | ||
|
||
/** | ||
* @reason Change executor to use our thread pool | ||
* Note: even at normal priority, our worker pool will try to share resources equally rather than having it | ||
* be a free-for-all | ||
* @author Spottedleaf | ||
*/ | ||
@Redirect( | ||
method = "runTask", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" | ||
) | ||
) | ||
private <U> CompletableFuture<U> changeExecutor(final Supplier<U> supplier, final Executor executor) { | ||
return CompletableFuture.supplyAsync( | ||
supplier, | ||
(final Runnable task) -> { | ||
RENDER_EXECUTOR.queueRunnable(task, PrioritisedExecutor.Priority.NORMAL); | ||
} | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters