-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significantly reduce the number of ChunkCoordIntPair allocations (#312)
* Significantly reduce the number of ChunkCoordIntPair allocations * spotlessApply (#315) * Use a LongChunkCoordIntPairSet instead * Still need to bypass the original loop * small tweaks * Wrap the "NEW" operation instead of making our own loop -- requires a UnMixin bump 0.1.14+ * Document Unimixins version dep jump * Switch to an opt in unsafe iterator (Reuse), default to the safe one (allocatiosn) * spotlessApply (#316) * Fix running in an obf env, also update toArray() functions * Disable the WorldServer Mixin if Optifine is present since it messes with the field --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c988120
commit e84dd95
Showing
13 changed files
with
320 additions
and
3 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
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
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
149 changes: 149 additions & 0 deletions
149
src/main/java/com/mitchej123/hodgepodge/hax/LongChunkCoordIntPairSet.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,149 @@ | ||
package com.mitchej123.hodgepodge.hax; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import net.minecraft.world.ChunkCoordIntPair; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import com.mitchej123.hodgepodge.mixins.interfaces.MutableChunkCoordIntPair; | ||
import com.mitchej123.hodgepodge.util.ChunkPosUtil; | ||
|
||
import it.unimi.dsi.fastutil.longs.LongIterator; | ||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet; | ||
import it.unimi.dsi.fastutil.longs.LongSet; | ||
|
||
public class LongChunkCoordIntPairSet implements Set<ChunkCoordIntPair> { | ||
|
||
private final MutableChunkCoordIntPair reusableChunkCoordIntPair = (MutableChunkCoordIntPair) new ChunkCoordIntPair( | ||
0, | ||
0); | ||
|
||
public ChunkCoordIntPair fromLongUnsafe(long pos) { | ||
return (ChunkCoordIntPair) reusableChunkCoordIntPair | ||
.setChunkPos(ChunkPosUtil.getPackedX(pos), ChunkPosUtil.getPackedZ(pos)); | ||
} | ||
|
||
public ChunkCoordIntPair fromLongSafe(long pos) { | ||
return new ChunkCoordIntPair(ChunkPosUtil.getPackedX(pos), ChunkPosUtil.getPackedZ(pos)); | ||
} | ||
|
||
private LongSet longSet = new LongOpenHashSet(); | ||
|
||
@Override | ||
public int size() { | ||
return longSet.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return longSet.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
if (o instanceof ChunkCoordIntPair chunkCoordIntPair) { | ||
return longSet.contains(ChunkPosUtil.toLong(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos)); | ||
} else if (o instanceof Long l) { | ||
return longSet.contains(l); | ||
} | ||
return false; | ||
} | ||
|
||
public boolean contains(long l) { | ||
return longSet.contains(l); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Iterator<ChunkCoordIntPair> iterator() { | ||
return longSet.stream().map(this::fromLongSafe).iterator(); | ||
} | ||
|
||
public Iterator<ChunkCoordIntPair> unsafeIterator() { | ||
// Reuses the same ChunkCoordIntPair object for every iteration, use this when you know the code won't | ||
// be storing the result anywhere | ||
return longSet.stream().map(this::fromLongUnsafe).iterator(); | ||
} | ||
|
||
public LongIterator longIterator() { | ||
return longSet.iterator(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Object[] toArray() { | ||
return longSet.stream().map(this::fromLongSafe).toArray(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public <T> T[] toArray(@NotNull T[] a) { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public boolean add(ChunkCoordIntPair chunkCoordIntPair) { | ||
return this.longSet.add(ChunkPosUtil.toLong(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos)); | ||
} | ||
|
||
public boolean addLong(long l) { | ||
return this.longSet.add(l); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
if (o instanceof ChunkCoordIntPair chunkCoordIntPair) { | ||
return longSet.remove(ChunkPosUtil.toLong(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos)); | ||
} else if (o instanceof Long l) { | ||
return longSet.remove(l); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean containsAll(@NotNull Collection<?> c) { | ||
return longSet.containsAll(c); | ||
} | ||
|
||
@Override | ||
public boolean addAll(@NotNull Collection<? extends ChunkCoordIntPair> c) { | ||
boolean added = false; | ||
for (ChunkCoordIntPair chunkCoordIntPair : c) { | ||
added |= this.longSet.add(ChunkPosUtil.toLong(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos)); | ||
} | ||
return added; | ||
} | ||
|
||
@Override | ||
public boolean retainAll(@NotNull Collection<?> c) { | ||
boolean removed = false; | ||
for (long l : longSet) { | ||
if (!c.contains(fromLongUnsafe(l))) { | ||
removed |= longSet.remove(l); | ||
} | ||
} | ||
return removed; | ||
} | ||
|
||
@Override | ||
public boolean removeAll(@NotNull Collection<?> c) { | ||
boolean removed = false; | ||
for (Object o : c) { | ||
if (o instanceof ChunkCoordIntPair chunkCoordIntPair) { | ||
removed |= longSet | ||
.remove(ChunkPosUtil.toLong(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos)); | ||
} else if (o instanceof Long l) { | ||
removed |= longSet.remove(l); | ||
} | ||
} | ||
return removed; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
this.longSet.clear(); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinChunkCoordIntPair.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,32 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft; | ||
|
||
import net.minecraft.world.ChunkCoordIntPair; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
import com.mitchej123.hodgepodge.mixins.interfaces.MutableChunkCoordIntPair; | ||
|
||
@Mixin(ChunkCoordIntPair.class) | ||
public class MixinChunkCoordIntPair implements MutableChunkCoordIntPair { | ||
|
||
@Shadow | ||
public int chunkXPos; | ||
@Shadow | ||
public int chunkZPos; | ||
|
||
public void setChunkXPos(int chunkXPos) { | ||
this.chunkXPos = chunkXPos; | ||
} | ||
|
||
public void setChunkZPos(int chunkZPos) { | ||
this.chunkZPos = chunkZPos; | ||
} | ||
|
||
public MutableChunkCoordIntPair setChunkPos(int chunkXPos, int chunkZPos) { | ||
this.chunkXPos = chunkXPos; | ||
this.chunkZPos = chunkZPos; | ||
return this; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...ava/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinWorldClient_FixAllocations.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,28 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import net.minecraft.client.multiplayer.WorldClient; | ||
|
||
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.Redirect; | ||
|
||
import com.mitchej123.hodgepodge.hax.LongChunkCoordIntPairSet; | ||
|
||
@Mixin(WorldClient.class) | ||
public abstract class MixinWorldClient_FixAllocations { | ||
|
||
@Shadow | ||
private final Set previousActiveChunkSet = new LongChunkCoordIntPairSet(); | ||
|
||
@Redirect( | ||
method = "func_147456_g", | ||
at = @At(value = "INVOKE", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;")) | ||
private Iterator<?> fixAllocations(Set instance) { | ||
return ((LongChunkCoordIntPairSet) instance).unsafeIterator(); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...ava/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinWorldServer_FixAllocations.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,24 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import net.minecraft.world.WorldServer; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
import com.mitchej123.hodgepodge.hax.LongChunkCoordIntPairSet; | ||
|
||
@Mixin(WorldServer.class) | ||
public abstract class MixinWorldServer_FixAllocations { | ||
|
||
@Redirect( | ||
method = "func_147456_g", | ||
at = @At(value = "INVOKE", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;")) | ||
private Iterator<?> fixAllocations(Set instance) { | ||
return ((LongChunkCoordIntPairSet) instance).unsafeIterator(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinWorld_FixAllocations.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,34 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft; | ||
|
||
import java.util.Set; | ||
|
||
import net.minecraft.world.ChunkCoordIntPair; | ||
import net.minecraft.world.World; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.mitchej123.hodgepodge.hax.LongChunkCoordIntPairSet; | ||
import com.mitchej123.hodgepodge.mixins.interfaces.MutableChunkCoordIntPair; | ||
|
||
@Mixin(World.class) | ||
public abstract class MixinWorld_FixAllocations { | ||
|
||
@Shadow | ||
protected Set activeChunkSet = new LongChunkCoordIntPairSet(); | ||
|
||
@Unique | ||
private final MutableChunkCoordIntPair reusableCCIP = (MutableChunkCoordIntPair) new ChunkCoordIntPair(0, 0); | ||
|
||
@WrapOperation( | ||
at = @At(value = "NEW", target = "Lnet/minecraft/world/ChunkCoordIntPair;"), | ||
method = "setActivePlayerChunksAndCheckLight") | ||
private ChunkCoordIntPair reuseMutableChunkCoordIntPair(int x, int z, Operation<ChunkCoordIntPair> original) { | ||
return (ChunkCoordIntPair) reusableCCIP.setChunkPos(x, z); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mitchej123/hodgepodge/mixins/interfaces/MutableChunkCoordIntPair.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,10 @@ | ||
package com.mitchej123.hodgepodge.mixins.interfaces; | ||
|
||
public interface MutableChunkCoordIntPair { | ||
|
||
void setChunkXPos(int chunkXPos); | ||
|
||
void setChunkZPos(int chunkZPos); | ||
|
||
MutableChunkCoordIntPair setChunkPos(int chunkXPos, int chunkZPos); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mitchej123/hodgepodge/util/ChunkPosUtil.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,19 @@ | ||
package com.mitchej123.hodgepodge.util; | ||
|
||
public class ChunkPosUtil { | ||
|
||
public static long INT_MASK = (1L << Integer.SIZE) - 1; | ||
|
||
public static int getPackedX(long pos) { | ||
return (int) (pos & INT_MASK); | ||
} | ||
|
||
public static int getPackedZ(long pos) { | ||
return (int) (pos >>> 32 & INT_MASK); | ||
} | ||
|
||
public static long toLong(int x, int z) { | ||
return (long) x & 4294967295L | ((long) z & 4294967295L) << 32; | ||
} | ||
|
||
} |