Merge remote-tracking branch 'origin/main' into 1.18

This commit is contained in:
embeddedt 2023-04-28 10:16:48 -04:00
commit 0a2d904ce5
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
6 changed files with 188 additions and 0 deletions

View File

@ -63,6 +63,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.fast_forge_dummies", true);
this.addMixinRule("perf.dynamic_structure_manager", true);
this.addMixinRule("bugfix.chunk_deadlock", true);
this.addMixinRule("bugfix.paper_chunk_patches", true);
this.addMixinRule("perf.thread_priorities", true);
this.addMixinRule("perf.scan_cache", true);
this.addMixinRule("perf.kubejs", modPresent("kubejs"));

View File

@ -0,0 +1,5 @@
package org.embeddedt.modernfix.duck;
public interface IPaperChunkHolder {
boolean mfix$canAdvanceStatus();
}

View File

@ -0,0 +1,61 @@
package org.embeddedt.modernfix.mixin.bugfix.paper_chunk_patches;
import com.mojang.datafixers.util.Either;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import org.embeddedt.modernfix.duck.IPaperChunkHolder;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Mixin(ChunkHolder.class)
public abstract class ChunkHolderMixin implements IPaperChunkHolder {
@Shadow public abstract CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> getFutureIfPresentUnchecked(ChunkStatus arg);
@Shadow @Final private static List<ChunkStatus> CHUNK_STATUSES;
public ChunkStatus mfix$getChunkHolderStatus() {
for (ChunkStatus curr = ChunkStatus.FULL, next = curr.getParent(); curr != next; curr = next, next = next.getParent()) {
CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> future = this.getFutureIfPresentUnchecked(curr);
Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure> either = future.getNow(null);
if (either == null || !either.left().isPresent()) {
continue;
}
return curr;
}
return null;
}
public ChunkAccess mfix$getAvailableChunkNow() {
// TODO can we just getStatusFuture(EMPTY)?
for (ChunkStatus curr = ChunkStatus.FULL, next = curr.getParent(); curr != next; curr = next, next = next.getParent()) {
CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> future = this.getFutureIfPresentUnchecked(curr);
Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure> either = future.getNow(null);
if (either == null || !either.left().isPresent()) {
continue;
}
return either.left().get();
}
return null;
}
private static ChunkStatus mfix$getNextStatus(ChunkStatus status) {
if (status == ChunkStatus.FULL) {
return status;
}
return CHUNK_STATUSES.get(status.getIndex() + 1);
}
@Override
public boolean mfix$canAdvanceStatus() {
ChunkStatus status = mfix$getChunkHolderStatus();
ChunkAccess chunk = mfix$getAvailableChunkNow();
return chunk != null && (status == null || chunk.getStatus().isOrAfter(mfix$getNextStatus(status)));
}
}

View File

@ -0,0 +1,67 @@
package org.embeddedt.modernfix.mixin.bugfix.paper_chunk_patches;
import com.mojang.datafixers.util.Either;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.util.thread.BlockableEventLoop;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraftforge.server.ServerLifecycleHooks;
import org.embeddedt.modernfix.duck.IPaperChunkHolder;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
@Mixin(ChunkMap.class)
public class ChunkMapMixin {
@Shadow @Final private BlockableEventLoop<Runnable> mainThreadExecutor;
private Executor mainInvokingExecutor;
@Inject(method = "<init>", at = @At("RETURN"), cancellable = true)
private void setup(CallbackInfo ci) {
this.mainInvokingExecutor = (runnable) -> {
if(ServerLifecycleHooks.getCurrentServer().isSameThread())
runnable.run();
else
this.mainThreadExecutor.execute(runnable);
};
}
/* https://github.com/PaperMC/Paper/blob/ver/1.17.1/patches/server/0752-Fix-chunks-refusing-to-unload-at-low-TPS.patch */
@ModifyArg(method = "prepareAccessibleChunk", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;thenApplyAsync(Ljava/util/function/Function;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"), index = 1)
private Executor useMainThreadExecutor(Executor executor) {
return this.mainThreadExecutor;
}
/* https://github.com/PaperMC/Paper/blob/master/patches/removed/1.19.2-legacy-chunksystem/0482-Improve-Chunk-Status-Transition-Speed.patch */
@ModifyArg(method = "prepareEntityTickingChunk", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;thenApplyAsync(Ljava/util/function/Function;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"), index = 1)
private Executor useMainInvokingExecutor(Executor executor) {
return this.mainInvokingExecutor;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Redirect(method = "scheduleChunkGeneration", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;thenComposeAsync(Ljava/util/function/Function;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"))
private CompletableFuture skipWorkerIfPossible(CompletableFuture inputFuture, Function function, Executor executor, ChunkHolder holder) {
Executor targetExecutor = (runnable) -> {
if(((IPaperChunkHolder)holder).mfix$canAdvanceStatus()) {
this.mainInvokingExecutor.execute(runnable);
return;
}
executor.execute(runnable);
};
return inputFuture.thenComposeAsync(function, targetExecutor);
}
}

View File

@ -0,0 +1,51 @@
package org.embeddedt.modernfix.mixin.bugfix.paper_chunk_patches;
import net.minecraft.util.SortedArraySet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.function.Predicate;
@Mixin(SortedArraySet.class)
public abstract class SortedArraySetMixin<T> extends AbstractSet<T> {
@Shadow private int size;
@Shadow private T[] contents;
// Paper start - optimise removeIf
@Override
public boolean removeIf(Predicate<? super T> filter) {
// prev. impl used an iterator, which could be n^2 and creates garbage
int i = 0, len = this.size;
T[] backingArray = this.contents;
for (;;) {
if (i >= len) {
return false;
}
if (!filter.test(backingArray[i])) {
++i;
continue;
}
break;
}
// we only want to write back to backingArray if we really need to
int lastIndex = i; // this is where new elements are shifted to
for (; i < len; ++i) {
T curr = backingArray[i];
if (!filter.test(curr)) { // if test throws we're screwed
backingArray[lastIndex++] = curr;
}
}
// cleanup end
Arrays.fill(backingArray, lastIndex, len, null);
this.size = lastIndex;
return true;
}
}

View File

@ -10,6 +10,9 @@
"bugfix.edge_chunk_not_saved.ChunkManagerMixin",
"perf.modern_resourcepacks.PathResourcePackMixin",
"perf.modern_resourcepacks.VanillaPackResourcesMixin",
"bugfix.paper_chunk_patches.ChunkMapMixin",
"bugfix.paper_chunk_patches.ChunkHolderMixin",
"bugfix.paper_chunk_patches.SortedArraySetMixin",
"perf.dynamic_structure_manager.StructureManagerMixin",
"bugfix.chunk_deadlock.ServerChunkCacheMixin",
"perf.dedicated_reload_executor.MinecraftServerMixin",