perf: creative node registers once, no server stack walk #29

Merged
layue13 merged 1 commits from perf/creative-node-registry into main 2026-11-07 06:18:49 +00:00
3 changed files with 106 additions and 1 deletions

No files matched your search

@@ -40,6 +40,11 @@ public class BlockCreativeNode extends BlockContainer {
}
public AxisAlignedBB getCollisionBoundingBoxFromPool(World w, int x, int y, int z) {
if(!w.isRemote) {
// rendering never happens on the server, so the stack walk below can only find nothing
return AxisAlignedBB.getBoundingBox(0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D);
}
StackTraceElement[] var5 = Thread.currentThread().getStackTrace();
int var6 = var5.length;
@@ -35,6 +35,7 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable {
public Color targetColor = new Color(16777215);
public Color color = new Color(16777215);
public boolean hasUpdatedOnce;
private boolean locationRegistered;
public void updateEntity() {
@@ -44,12 +45,30 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable {
if(!this.hasUpdatedOnce) {
this.hasUpdatedOnce = !this.hasUpdatedOnce;
} else {
} else if(!this.locationRegistered) {
this.generateId();
}
}
public void invalidate() {
this.unregisterLocation();
super.invalidate();
}
public void onChunkUnload() {
this.unregisterLocation();
super.onChunkUnload();
}
private void unregisterLocation() {
if(this.id != null && TileNode.locations != null) {
TileNode.locations.remove(this.id);
}
this.locationRegistered = false;
}
public AspectList getAspects() {
return (new AspectList()).add(Aspect.AIR, 134217).add(Aspect.WATER, 134217).add(Aspect.FIRE, 134217).add(Aspect.ORDER, 134217).add(Aspect.ENTROPY, 134217).add(Aspect.EARTH, 134217);
}
@@ -232,6 +251,7 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable {
t.add(Integer.valueOf(this.yCoord));
t.add(Integer.valueOf(this.zCoord));
TileNode.locations.put(this.id, t);
this.locationRegistered = true;
}
return this.id;
@@ -0,0 +1,80 @@
package com.pengu.thaumcraft.additions.tileentity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.world.World;
import net.minecraft.world.WorldProviderSurface;
import net.minecraft.world.WorldServer;
import org.junit.Before;
import org.junit.Test;
import thaumcraft.common.tiles.TileNode;
/** TDLM/thaumicadditions#17: a creative node registers its location once, not every tick, and unregisters on removal. */
public class CreativeNodeRegistryTest {
private TileCreativeNode node;
@SuppressWarnings("unchecked")
private static <T> T allocate(Class<T> type) throws Exception {
Constructor<?> ctor = sun.reflect.ReflectionFactory.getReflectionFactory()
.newConstructorForSerialization(type, Object.class.getDeclaredConstructor());
return (T) ctor.newInstance();
}
@Before
public void setUp() throws Exception {
if (TileNode.locations == null) {
Field f = TileNode.class.getDeclaredField("locations");
f.setAccessible(true);
f.set(null, new HashMap<String, ArrayList>());
}
TileNode.locations.clear();
World world = allocate(FixtureWorld.class);
Field provider = World.class.getDeclaredField("provider");
provider.setAccessible(true);
provider.set(world, new WorldProviderSurface());
this.node = allocate(TileCreativeNode.class);
this.node.setWorldObj(world);
this.node.xCoord = 3;
this.node.yCoord = 70;
this.node.zCoord = 5;
}
@Test
public void locationIsPutOnceAcrossManyTicks() {
this.node.updateEntity(); // first tick only marks the node as updated
this.node.updateEntity();
ArrayList first = TileNode.locations.get("0:3:70:5");
assertNotNull(first);
for (int i = 0; i < 1000; i++) {
this.node.updateEntity();
}
assertSame("no re-registration on later ticks", first, TileNode.locations.get("0:3:70:5"));
assertEquals(1, TileNode.locations.size());
}
@Test
public void unloadingRemovesTheLocationSoItCanBeRegisteredAgain() {
this.node.updateEntity();
this.node.updateEntity();
assertNotNull(TileNode.locations.get("0:3:70:5"));
this.node.onChunkUnload();
assertNull(TileNode.locations.get("0:3:70:5"));
this.node.updateEntity();
assertNotNull(TileNode.locations.get("0:3:70:5"));
}
public static class FixtureWorld extends WorldServer {
FixtureWorld() {
super(null, null, "node", 0, null, null);
}
}
}