Ensure the main thread does not spin when no tasks are available

This commit is contained in:
embeddedt 2023-01-22 18:07:48 -05:00
parent a8df933e19
commit 8cdc425d7d
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
3 changed files with 33 additions and 2 deletions

View File

@ -30,7 +30,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.async_jei", true);
this.addMixinRule("perf.thread_priorities", true);
this.addMixinRule("perf.preload_block_classes", false);
this.addMixinRule("perf.defer_voxelshape_optimize", false);
this.addMixinRule("perf.sync_executor_sleep", true);
this.addMixinRule("perf.parallel_blockstate_cache_rebuild", true);
this.addMixinRule("perf.deduplicate_location", true);
this.addMixinRule("safety", true);

View File

@ -0,0 +1,30 @@
package org.embeddedt.modernfix.mixin.perf.sync_executor_sleep;
import net.minecraftforge.fml.ModWorkManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
@Mixin(targets = "net/minecraftforge/fml/ModWorkManager$SyncExecutor")
public class SyncExecutorMixin {
@Shadow private ConcurrentLinkedDeque<Runnable> tasks;
private static final long PARK_TIME = TimeUnit.MILLISECONDS.toNanos(50);
/**
* Currently FML spins in driveOne while waiting for the modloading workers. We can improve this
* by sleeping for 50ms at a time.
* @author embeddedt
* @reason improve CPU efficiency
*/
@Inject(method = "driveOne", at = @At("HEAD"), remap = false)
private void sleepWhileNoTasks(CallbackInfoReturnable<Boolean> cir) {
if(tasks.isEmpty())
LockSupport.parkNanos(PARK_TIME);
}
}

View File

@ -22,7 +22,8 @@
"perf.parallel_blockstate_cache_rebuild.BlocksMixin",
"perf.parallel_blockstate_cache_rebuild.BlockCallbacksMixin",
"perf.parallel_blockstate_cache_rebuild.ShapeCacheMixin",
"perf.deduplicate_location.MixinResourceLocation"
"perf.deduplicate_location.MixinResourceLocation",
"perf.sync_executor_sleep.SyncExecutorMixin"
],
"client": [
"perf.skip_first_datapack_reload.MinecraftMixin",