Skip to content

Commit 070b7b6

Browse files
committed
Add some simple patches to cut down allocation rate when ticking chunks
1 parent a0fdb3e commit 070b7b6

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
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+
}

0 commit comments

Comments
 (0)