Skip to content

Commit 9d7ef77

Browse files
committed
Merge remote-tracking branch 'origin/1.20' into 1.20.4
2 parents 5e20e25 + 3ad7a8c commit 9d7ef77

File tree

7 files changed

+221
-4
lines changed

7 files changed

+221
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.ticking_chunk_alloc;
2+
3+
import net.minecraft.world.entity.ambient.Bat;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
import org.spongepowered.asm.mixin.injection.At;
6+
import org.spongepowered.asm.mixin.injection.Redirect;
7+
8+
import java.time.LocalDate;
9+
10+
@Mixin(value = Bat.class, priority = 1200)
11+
public class BatMixin {
12+
private static long mfix$lastQueriedTime = -1L;
13+
private static LocalDate mfix$lastQueriedDate = null;
14+
15+
/**
16+
* @author embeddedt
17+
* @reason avoid excessive allocations from continuously querying the date, only get a new date once every 30 seconds
18+
*/
19+
@Redirect(method = "isHalloween", at = @At(value = "INVOKE", target = "Ljava/time/LocalDate;now()Ljava/time/LocalDate;"), require = 0)
20+
private static LocalDate useCachedLocalDate() {
21+
LocalDate date = mfix$lastQueriedDate;
22+
if(date == null || Math.abs(System.currentTimeMillis() - mfix$lastQueriedTime) > 30000) {
23+
mfix$lastQueriedDate = date = LocalDate.now();
24+
mfix$lastQueriedTime = System.currentTimeMillis();
25+
}
26+
return date;
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.ticking_chunk_alloc;
2+
3+
import net.minecraft.world.level.chunk.ChunkGenerator;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
import org.spongepowered.asm.mixin.injection.At;
6+
import org.spongepowered.asm.mixin.injection.Redirect;
7+
8+
import java.util.Collections;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
@Mixin(ChunkGenerator.class)
13+
public class ChunkGeneratorMixin {
14+
/**
15+
* @author embeddedt
16+
* @reason Avoid allocation if the chunk contains no structures
17+
*/
18+
@Redirect(method = "getMobsAt", at = @At(value = "INVOKE", target = "Ljava/util/Map;entrySet()Ljava/util/Set;"), require = 0)
19+
private Set<?> avoidSetAllocation(Map<?, ?> instance) {
20+
if(instance.isEmpty()) {
21+
return Collections.emptySet();
22+
} else {
23+
return instance.entrySet();
24+
}
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.ticking_chunk_alloc;
2+
3+
import com.mojang.datafixers.util.Either;
4+
import net.minecraft.server.level.ChunkHolder;
5+
import net.minecraft.world.level.chunk.LevelChunk;
6+
import org.embeddedt.modernfix.util.EitherUtil;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Overwrite;
9+
import org.spongepowered.asm.mixin.Shadow;
10+
11+
import java.util.concurrent.CompletableFuture;
12+
13+
@Mixin(value = ChunkHolder.class, priority = 500)
14+
public abstract class ChunkHolderMixin {
15+
@Shadow public abstract CompletableFuture<Either<LevelChunk, ChunkHolder.ChunkLoadingFailure>> getTickingChunkFuture();
16+
17+
@Shadow public abstract CompletableFuture<Either<LevelChunk, ChunkHolder.ChunkLoadingFailure>> getFullChunkFuture();
18+
19+
/**
20+
* @author embeddedt
21+
* @reason avoid Optional allocation
22+
*/
23+
@Overwrite
24+
public LevelChunk getTickingChunk() {
25+
CompletableFuture<Either<LevelChunk, ChunkHolder.ChunkLoadingFailure>> completableFuture = this.getTickingChunkFuture();
26+
Either<LevelChunk, ChunkHolder.ChunkLoadingFailure> either = completableFuture.getNow(null);
27+
return either == null ? null : EitherUtil.leftOrNull(either);
28+
}
29+
30+
/**
31+
* @author embeddedt
32+
* @reason avoid Optional allocation
33+
*/
34+
@Overwrite
35+
public LevelChunk getFullChunk() {
36+
CompletableFuture<Either<LevelChunk, ChunkHolder.ChunkLoadingFailure>> completableFuture = this.getFullChunkFuture();
37+
Either<LevelChunk, ChunkHolder.ChunkLoadingFailure> either = completableFuture.getNow(null);
38+
return either == null ? null : EitherUtil.leftOrNull(either);
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.embeddedt.modernfix.util;
2+
3+
import com.mojang.datafixers.util.Either;
4+
5+
import java.lang.invoke.MethodHandle;
6+
import java.lang.invoke.MethodHandles;
7+
import java.lang.invoke.MethodType;
8+
import java.lang.reflect.Field;
9+
10+
public class EitherUtil {
11+
private static final Class<?> LEFT, RIGHT;
12+
private static final MethodHandle LEFT_VAL, RIGHT_VAL;
13+
14+
static {
15+
try {
16+
LEFT = Class.forName("com.mojang.datafixers.util.Either$Left");
17+
RIGHT = Class.forName("com.mojang.datafixers.util.Either$Right");
18+
Field lvalue = LEFT.getDeclaredField("value");
19+
lvalue.setAccessible(true);
20+
Field rvalue = RIGHT.getDeclaredField("value");
21+
rvalue.setAccessible(true);
22+
LEFT_VAL = MethodHandles.publicLookup().unreflectGetter(lvalue).asType(MethodType.methodType(Object.class, Either.class));
23+
RIGHT_VAL = MethodHandles.publicLookup().unreflectGetter(rvalue).asType(MethodType.methodType(Object.class, Either.class));
24+
} catch(ReflectiveOperationException e) {
25+
throw new AssertionError("Failed to hook DFU Either", e);
26+
}
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
public static <L, R> L leftOrNull(Either<L, R> either) {
31+
if(either.getClass() == LEFT) {
32+
try {
33+
return (L)LEFT_VAL.invokeExact(either);
34+
} catch(Throwable e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
return null;
39+
}
40+
41+
@SuppressWarnings("unchecked")
42+
public static <L, R> R rightOrNull(Either<L, R> either) {
43+
if(either.getClass() == RIGHT) {
44+
try {
45+
return (R)RIGHT_VAL.invokeExact(either);
46+
} catch(Throwable e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
return null;
51+
}
52+
}

fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.embeddedt.modernfix.fabric.mixin.perf.dynamic_resources;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
45
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
56
import net.fabricmc.loader.api.FabricLoader;
@@ -75,9 +76,19 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
7576
synchronized (this.field_40571) {
7677
/* to emulate vanilla model loading, treat as top-level */
7778
Optional<Block> blockOpt = Objects.equals(((ModelResourceLocation)arg).getVariant(), "inventory") ? Optional.empty() : BuiltInRegistries.BLOCK.getOptional(new ResourceLocation(arg.getNamespace(), arg.getPath()));
79+
boolean invalidMRL = false;
7880
if(blockOpt.isPresent()) {
7981
/* load via lambda for mods that expect blockstate to get loaded */
80-
for(BlockState state : extendedBakery.getBlockStatesForMRL(blockOpt.get().getStateDefinition(), (ModelResourceLocation)arg)) {
82+
ImmutableList<BlockState> states;
83+
try {
84+
states = extendedBakery.getBlockStatesForMRL(blockOpt.get().getStateDefinition(), (ModelResourceLocation)arg);
85+
} catch(RuntimeException e) {
86+
states = ImmutableList.of();
87+
invalidMRL = true;
88+
// Fall back to getModel
89+
cir.setReturnValue(this.field_40571.getModel(arg));
90+
}
91+
for(BlockState state : states) {
8192
try {
8293
blockStateLoaderHandle.invokeExact(this.field_40571, state);
8394
} catch(Throwable e) {
@@ -87,9 +98,11 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
8798
} else {
8899
this.field_40571.loadTopLevel((ModelResourceLocation)arg);
89100
}
90-
cir.setReturnValue(this.field_40571.topLevelModels.getOrDefault(arg, extendedBakery.mfix$getUnbakedMissingModel()));
91-
// avoid leaks
92-
this.field_40571.topLevelModels.clear();
101+
if(!invalidMRL) {
102+
cir.setReturnValue(this.field_40571.topLevelModels.getOrDefault(arg, extendedBakery.mfix$getUnbakedMissingModel()));
103+
// avoid leaks
104+
this.field_40571.topLevelModels.clear();
105+
}
93106
}
94107
} else
95108
cir.setReturnValue(this.field_40571.getModel(arg));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.embeddedt.modernfix.neoforge.config;
2+
3+
import com.electronwill.nightconfig.core.file.FileWatcher;
4+
import com.google.common.collect.ForwardingCollection;
5+
import com.google.common.collect.ForwardingMap;
6+
import net.neoforged.fml.util.ObfuscationReflectionHelper;
7+
8+
import java.util.Collection;
9+
import java.util.Iterator;
10+
import java.util.Map;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.locks.LockSupport;
13+
14+
/**
15+
* Throttle NightConfig's file watching. There are reports of this consuming excessive CPU time
16+
* (<a href="https://github.com/TheElectronWill/night-config/pull/144">example</a>) and the spammed iterator calls
17+
* end up being 10% of allocations when testing in a dev environment.
18+
*/
19+
public class NightConfigWatchThrottler {
20+
private static final long DELAY = TimeUnit.MILLISECONDS.toNanos(1000);
21+
22+
@SuppressWarnings("rawtypes")
23+
public static void throttle() {
24+
Map watchedDirs = ObfuscationReflectionHelper.getPrivateValue(FileWatcher.class, FileWatcher.defaultInstance(), "watchedDirs");
25+
ObfuscationReflectionHelper.setPrivateValue(FileWatcher.class, FileWatcher.defaultInstance(), new ForwardingMap() {
26+
@Override
27+
protected Map delegate() {
28+
return watchedDirs;
29+
}
30+
31+
private Collection cachedValues;
32+
33+
@Override
34+
public Collection values() {
35+
if(cachedValues == null) {
36+
Collection values = super.values();
37+
cachedValues = new ForwardingCollection() {
38+
@Override
39+
protected Collection delegate() {
40+
return values;
41+
}
42+
43+
@Override
44+
public Iterator iterator() {
45+
// iterator() is called at the beginning of each iteration of the watch loop,
46+
// so it is a good spot to inject the delay.
47+
LockSupport.parkNanos(DELAY);
48+
return super.iterator();
49+
}
50+
};
51+
}
52+
return cachedValues;
53+
}
54+
}, "watchedDirs");
55+
}
56+
}

neoforge/src/main/java/org/embeddedt/modernfix/platform/neoforge/ModernFixPlatformHooksImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.embeddedt.modernfix.api.constants.IntegrationConstants;
2525
import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
2626
import org.embeddedt.modernfix.neoforge.config.NightConfigFixer;
27+
import org.embeddedt.modernfix.neoforge.config.NightConfigWatchThrottler;
2728
import org.embeddedt.modernfix.neoforge.init.ModernFixForge;
2829
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
2930
import org.embeddedt.modernfix.spark.SparkLaunchProfiler;
@@ -106,6 +107,7 @@ public void injectPlatformSpecificHacks() {
106107
}
107108

108109
NightConfigFixer.monitorFileWatcher();
110+
NightConfigWatchThrottler.throttle();
109111
}
110112

111113
public void applyASMTransformers(String mixinClassName, ClassNode targetClass) {

0 commit comments

Comments
 (0)