Skip to content

Commit

Permalink
TISCM cache TileEntity Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Mar 6, 2021
1 parent ae8c29c commit d49c88b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,5 +691,6 @@ There are also a few optimizations which is not from lithium mod:
- Cache BoundingBoxList creation in TileEntityHopper and TileEntityPiston
- Pre-allocate 256 size in hashsets/hashmaps to avoid constantly rehash when the amount of TileEntity is small
- Permanently store item burn times in `TileEntityFurnace` to avoid costly map generating each time
- Cache some expensive tile entity serialization data

If necessary, part of the optimization implementation can be switched manually in the `TISCMOptimizationConfig` class
1 change: 1 addition & 0 deletions docs/Features_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,5 +691,6 @@ TISCM 中也有一些在 lithium mod 不包含的优化:
-`TileEntityHopper` 以及 `TileEntityPiston` 中缓存 BoundingBoxList 的创建结果
-`TileEntityList` 中给 hashset/hashmap 预先分配 256 大小的空间以防止在方块实体数量较小时频繁重建容器
-`TileEntityFurnace` 中永久性地储存物品的燃烧时间以避免每次调用都重复创建时间表
- 缓存了部分高代价的方块实体序列化用的数据

如果需要,部分优化的实现可在 `TISCMOptimizationConfig` 类中手动开关
42 changes: 42 additions & 0 deletions patches/net/minecraft/tileentity/TileEntity.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--- a/net/minecraft/tileentity/TileEntity.java
+++ b/net/minecraft/tileentity/TileEntity.java
@@ -1,6 +1,8 @@
package net.minecraft.tileentity;

import javax.annotation.Nullable;
+
+import carpet.utils.TISCMOptimizationConfig;
import net.minecraft.block.state.IBlockState;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.nbt.NBTTagCompound;
@@ -26,6 +28,9 @@
@Nullable
private IBlockState cachedBlockState;

+ // TISCM cache TileEntity Serialization
+ private ResourceLocation tileEntityIdCache;
+
public TileEntity(TileEntityType<?> tileEntityTypeIn)
{
this.type = tileEntityTypeIn;
@@ -59,7 +64,19 @@

private NBTTagCompound writeInternal(NBTTagCompound compound)
{
- ResourceLocation resourcelocation = TileEntityType.getId(this.getType());
+ // TISCM cache TileEntity Serialization
+// ResourceLocation resourcelocation = TileEntityType.getId(this.getType());
+ ResourceLocation resourcelocation;
+ if (TISCMOptimizationConfig.CACHE_TILE_ENTITY_SERIALIZATION && this.tileEntityIdCache != null)
+ {
+ resourcelocation = this.tileEntityIdCache;
+ }
+ else
+ {
+ // vanilla
+ resourcelocation = TileEntityType.getId(this.getType());
+ this.tileEntityIdCache = resourcelocation;
+ }

if (resourcelocation == null)
{
5 changes: 3 additions & 2 deletions patches/net/minecraft/tileentity/TileEntityFurnace.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
@@ -66,8 +67,17 @@
@@ -66,8 +67,18 @@
map.put(itemProvider.asItem(), time);
}

+ // TISCM cache item burn times
+ // Permanently store item burn times to avoid costly map generating each time
+ private static Map<Item, Integer> burnTimesCache = null;
+
Expand All @@ -25,7 +26,7 @@
Map<Item, Integer> map = Maps.newLinkedHashMap();
setBurnTime(map, Items.LAVA_BUCKET, 20000);
setBurnTime(map, Blocks.COAL_BLOCK, 16000);
@@ -118,6 +128,13 @@
@@ -118,6 +129,13 @@
setBurnTime(map, Items.BOWL, 100);
setBurnTime(map, ItemTags.CARPETS, 67);
setBurnTime(map, Blocks.DRIED_KELP_BLOCK, 4001);
Expand Down
52 changes: 40 additions & 12 deletions patches/net/minecraft/tileentity/TileEntityPiston.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
private static final ThreadLocal<EnumFacing> MOVING_ENTITY = new ThreadLocal<EnumFacing>()
{
protected EnumFacing initialValue()
@@ -40,6 +50,19 @@
@@ -40,6 +50,22 @@
private float lastProgress;
private long lastTicked;

+ // TISCM cache TileEntity Serialization
+ private NBTTagCompound pistonStateNBTCache;
+
+ // lithium block.moving_block_shapes
+ private static final VoxelShape[] PISTON_BASE_WITH_MOVING_HEAD_SHAPES = precomputePistonBaseWithMovingHeadShapes();
+ @SuppressWarnings("unchecked")
Expand All @@ -47,13 +50,16 @@
public TileEntityPiston()
{
super(TileEntityType.PISTON);
@@ -52,8 +75,103 @@
@@ -52,8 +78,106 @@
this.pistonFacing = pistonFacingIn;
this.extending = extendingIn;
this.shouldHeadBeRendered = shouldHeadBeRenderedIn;
+
+ // TISCM Cache BoundingBoxList creation
+ this.createVoxelShapeBoundingBoxListCache();
+
+ // TISCM cache TileEntity Serialization
+ this.pistonStateNBTCache = null;
}

+ // lithium block.moving_block_shapes starts
Expand Down Expand Up @@ -151,7 +157,7 @@
public NBTTagCompound getUpdateTag()
{
return this.write(new NBTTagCompound());
@@ -112,15 +230,45 @@
@@ -112,15 +236,45 @@
return !this.isExtending() && this.shouldPistonHeadBeRendered() ? Blocks.PISTON_HEAD.getDefaultState().with(BlockPistonExtension.TYPE, this.pistonState.getBlock() == Blocks.STICKY_PISTON ? PistonType.STICKY : PistonType.DEFAULT).with(BlockPistonExtension.FACING, this.pistonState.get(BlockPistonBase.FACING)) : this.pistonState;
}

Expand Down Expand Up @@ -200,7 +206,7 @@
AxisAlignedBB axisalignedbb = this.moveByPositionAndProgress(this.getMinMaxPiecesAABB(list));
List<Entity> list1 = this.world.getEntitiesWithinAABBExcludingEntity((Entity)null, this.getMovementArea(axisalignedbb, enumfacing, d0).union(axisalignedbb));

@@ -134,7 +282,7 @@
@@ -134,7 +288,7 @@

if (entity.getPushReaction() != EnumPushReaction.IGNORE)
{
Expand All @@ -209,7 +215,7 @@
{
switch (enumfacing.getAxis())
{
@@ -170,14 +318,25 @@
@@ -170,14 +324,25 @@
if (!(d1 <= 0.0D))
{
d1 = Math.min(d1, d0) + 0.01D;
Expand Down Expand Up @@ -238,7 +244,7 @@
}
}
}
@@ -270,9 +429,12 @@
@@ -270,9 +435,12 @@
if (Math.abs(d0 - d1) < 0.01D)
{
d0 = Math.min(d0, p_190605_3_) + 0.01D;
Expand All @@ -254,7 +260,7 @@
}
}
}
@@ -319,9 +481,38 @@
@@ -319,9 +487,38 @@
iblockstate = Block.getValidBlockForPosition(this.pistonState, this.world, this.pos);
}

Expand Down Expand Up @@ -295,7 +301,7 @@
}
}

@@ -351,8 +542,15 @@
@@ -351,8 +548,15 @@
iblockstate = iblockstate.with(BlockStateProperties.WATERLOGGED, Boolean.valueOf(false));
}

Expand All @@ -313,7 +319,7 @@
}
}
}
@@ -378,6 +576,18 @@
@@ -378,22 +582,59 @@
this.lastProgress = this.progress;
this.extending = compound.getBoolean("extending");
this.shouldHeadBeRendered = compound.getBoolean("source");
Expand All @@ -329,10 +335,32 @@
+
+ // Cache BoundingBoxList creation
+ this.createVoxelShapeBoundingBoxListCache();
+
+ // TISCM cache TileEntity Serialization
+ this.pistonStateNBTCache = null;
}

public NBTTagCompound write(NBTTagCompound compound)
@@ -388,12 +598,19 @@
{
super.write(compound);
- compound.put("blockState", NBTUtil.writeBlockState(this.pistonState));
+
+ // TISCM cache TileEntity Serialization
+// compound.put("blockState", NBTUtil.writeBlockState(this.pistonState));
+ NBTTagCompound blockStateNBT;
+ if (TISCMOptimizationConfig.CACHE_TILE_ENTITY_SERIALIZATION && this.pistonStateNBTCache != null)
+ {
+ blockStateNBT = this.pistonStateNBTCache;
+ }
+ else
+ {
+ // vanilla
+ blockStateNBT = NBTUtil.writeBlockState(this.pistonState);
+ this.pistonStateNBTCache = blockStateNBT;
+ }
+ compound.put("blockState", blockStateNBT);
+
compound.putInt("facing", this.pistonFacing.getIndex());
compound.putFloat("progress", this.lastProgress);
compound.putBoolean("extending", this.extending);
compound.putBoolean("source", this.shouldHeadBeRendered);
Expand All @@ -353,7 +381,7 @@

if (!this.extending && this.shouldHeadBeRendered)
{
@@ -424,6 +641,32 @@
@@ -424,6 +665,32 @@
}

float f = this.getExtendedProgress(this.progress);
Expand Down Expand Up @@ -386,7 +414,7 @@
double d0 = (double)((float)this.pistonFacing.getXOffset() * f);
double d1 = (double)((float)this.pistonFacing.getYOffset() * f);
double d2 = (double)((float)this.pistonFacing.getZOffset() * f);
@@ -435,4 +678,37 @@
@@ -435,4 +702,37 @@
{
return this.lastTicked;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/carpet/utils/TISCMOptimizationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public class TISCMOptimizationConfig
{
private static final boolean TISCM_OPTIMIZATION_ENABLE = true;

// TISCM Cache BoundingBoxList creation
public static final boolean CACHE_BOUNDING_BOX_LIST_CREATION = TISCM_OPTIMIZATION_ENABLE && true;
// TISCM Larger tile entity list
public static final boolean LARGER_TILE_ENTITY_LIST = TISCM_OPTIMIZATION_ENABLE && true;
// TISCM cache item burn times
public static final boolean CACHE_ITEM_BURN_TIMES = TISCM_OPTIMIZATION_ENABLE && true;
// TISCM cache TileEntity Serialization
public static final boolean CACHE_TILE_ENTITY_SERIALIZATION = TISCM_OPTIMIZATION_ENABLE && true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TileEntityList implements List<TileEntity> {
//TileEntityList does not support double-add of the same object. But it does support multiple at the same position.
//This collection behaves like a set with insertion order. It also provides a position->TileEntity lookup.

// TISCM Larger tile entity list
// pre-allocate 256 volume in hashsets/hashmaps to avoid constantly rehash when the amount of TileEntity is small
@SuppressWarnings("FieldCanBeLocal")
private final int COLLECTION_DEFAULT_SIZE = TISCMOptimizationConfig.LARGER_TILE_ENTITY_LIST ? 256 : Long2ReferenceOpenHashMap.DEFAULT_INITIAL_SIZE;
Expand Down

0 comments on commit d49c88b

Please sign in to comment.