Skip to content

Commit d49c88b

Browse files
committed
TISCM cache TileEntity Serialization
1 parent ae8c29c commit d49c88b

7 files changed

Lines changed: 93 additions & 14 deletions

File tree

docs/Features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,5 +691,6 @@ There are also a few optimizations which is not from lithium mod:
691691
- Cache BoundingBoxList creation in TileEntityHopper and TileEntityPiston
692692
- Pre-allocate 256 size in hashsets/hashmaps to avoid constantly rehash when the amount of TileEntity is small
693693
- Permanently store item burn times in `TileEntityFurnace` to avoid costly map generating each time
694+
- Cache some expensive tile entity serialization data
694695

695696
If necessary, part of the optimization implementation can be switched manually in the `TISCMOptimizationConfig` class

docs/Features_cn.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,5 +691,6 @@ TISCM 中也有一些在 lithium mod 不包含的优化:
691691
-`TileEntityHopper` 以及 `TileEntityPiston` 中缓存 BoundingBoxList 的创建结果
692692
-`TileEntityList` 中给 hashset/hashmap 预先分配 256 大小的空间以防止在方块实体数量较小时频繁重建容器
693693
-`TileEntityFurnace` 中永久性地储存物品的燃烧时间以避免每次调用都重复创建时间表
694+
- 缓存了部分高代价的方块实体序列化用的数据
694695

695696
如果需要,部分优化的实现可在 `TISCMOptimizationConfig` 类中手动开关
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--- a/net/minecraft/tileentity/TileEntity.java
2+
+++ b/net/minecraft/tileentity/TileEntity.java
3+
@@ -1,6 +1,8 @@
4+
package net.minecraft.tileentity;
5+
6+
import javax.annotation.Nullable;
7+
+
8+
+import carpet.utils.TISCMOptimizationConfig;
9+
import net.minecraft.block.state.IBlockState;
10+
import net.minecraft.crash.CrashReportCategory;
11+
import net.minecraft.nbt.NBTTagCompound;
12+
@@ -26,6 +28,9 @@
13+
@Nullable
14+
private IBlockState cachedBlockState;
15+
16+
+ // TISCM cache TileEntity Serialization
17+
+ private ResourceLocation tileEntityIdCache;
18+
+
19+
public TileEntity(TileEntityType<?> tileEntityTypeIn)
20+
{
21+
this.type = tileEntityTypeIn;
22+
@@ -59,7 +64,19 @@
23+
24+
private NBTTagCompound writeInternal(NBTTagCompound compound)
25+
{
26+
- ResourceLocation resourcelocation = TileEntityType.getId(this.getType());
27+
+ // TISCM cache TileEntity Serialization
28+
+// ResourceLocation resourcelocation = TileEntityType.getId(this.getType());
29+
+ ResourceLocation resourcelocation;
30+
+ if (TISCMOptimizationConfig.CACHE_TILE_ENTITY_SERIALIZATION && this.tileEntityIdCache != null)
31+
+ {
32+
+ resourcelocation = this.tileEntityIdCache;
33+
+ }
34+
+ else
35+
+ {
36+
+ // vanilla
37+
+ resourcelocation = TileEntityType.getId(this.getType());
38+
+ this.tileEntityIdCache = resourcelocation;
39+
+ }
40+
41+
if (resourcelocation == null)
42+
{

patches/net/minecraft/tileentity/TileEntityFurnace.java.patch

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import com.google.common.collect.Lists;
88
import com.google.common.collect.Maps;
99
import java.util.List;
10-
@@ -66,8 +67,17 @@
10+
@@ -66,8 +67,18 @@
1111
map.put(itemProvider.asItem(), time);
1212
}
1313

14+
+ // TISCM cache item burn times
1415
+ // Permanently store item burn times to avoid costly map generating each time
1516
+ private static Map<Item, Integer> burnTimesCache = null;
1617
+
@@ -25,7 +26,7 @@
2526
Map<Item, Integer> map = Maps.newLinkedHashMap();
2627
setBurnTime(map, Items.LAVA_BUCKET, 20000);
2728
setBurnTime(map, Blocks.COAL_BLOCK, 16000);
28-
@@ -118,6 +128,13 @@
29+
@@ -118,6 +129,13 @@
2930
setBurnTime(map, Items.BOWL, 100);
3031
setBurnTime(map, ItemTags.CARPETS, 67);
3132
setBurnTime(map, Blocks.DRIED_KELP_BLOCK, 4001);

patches/net/minecraft/tileentity/TileEntityPiston.java.patch

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
private static final ThreadLocal<EnumFacing> MOVING_ENTITY = new ThreadLocal<EnumFacing>()
2828
{
2929
protected EnumFacing initialValue()
30-
@@ -40,6 +50,19 @@
30+
@@ -40,6 +50,22 @@
3131
private float lastProgress;
3232
private long lastTicked;
3333

34+
+ // TISCM cache TileEntity Serialization
35+
+ private NBTTagCompound pistonStateNBTCache;
36+
+
3437
+ // lithium block.moving_block_shapes
3538
+ private static final VoxelShape[] PISTON_BASE_WITH_MOVING_HEAD_SHAPES = precomputePistonBaseWithMovingHeadShapes();
3639
+ @SuppressWarnings("unchecked")
@@ -47,13 +50,16 @@
4750
public TileEntityPiston()
4851
{
4952
super(TileEntityType.PISTON);
50-
@@ -52,8 +75,103 @@
53+
@@ -52,8 +78,106 @@
5154
this.pistonFacing = pistonFacingIn;
5255
this.extending = extendingIn;
5356
this.shouldHeadBeRendered = shouldHeadBeRenderedIn;
5457
+
5558
+ // TISCM Cache BoundingBoxList creation
5659
+ this.createVoxelShapeBoundingBoxListCache();
60+
+
61+
+ // TISCM cache TileEntity Serialization
62+
+ this.pistonStateNBTCache = null;
5763
}
5864

5965
+ // lithium block.moving_block_shapes starts
@@ -151,7 +157,7 @@
151157
public NBTTagCompound getUpdateTag()
152158
{
153159
return this.write(new NBTTagCompound());
154-
@@ -112,15 +230,45 @@
160+
@@ -112,15 +236,45 @@
155161
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;
156162
}
157163

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

203-
@@ -134,7 +282,7 @@
209+
@@ -134,7 +288,7 @@
204210

205211
if (entity.getPushReaction() != EnumPushReaction.IGNORE)
206212
{
@@ -209,7 +215,7 @@
209215
{
210216
switch (enumfacing.getAxis())
211217
{
212-
@@ -170,14 +318,25 @@
218+
@@ -170,14 +324,25 @@
213219
if (!(d1 <= 0.0D))
214220
{
215221
d1 = Math.min(d1, d0) + 0.01D;
@@ -238,7 +244,7 @@
238244
}
239245
}
240246
}
241-
@@ -270,9 +429,12 @@
247+
@@ -270,9 +435,12 @@
242248
if (Math.abs(d0 - d1) < 0.01D)
243249
{
244250
d0 = Math.min(d0, p_190605_3_) + 0.01D;
@@ -254,7 +260,7 @@
254260
}
255261
}
256262
}
257-
@@ -319,9 +481,38 @@
263+
@@ -319,9 +487,38 @@
258264
iblockstate = Block.getValidBlockForPosition(this.pistonState, this.world, this.pos);
259265
}
260266

@@ -295,7 +301,7 @@
295301
}
296302
}
297303

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

@@ -313,7 +319,7 @@
313319
}
314320
}
315321
}
316-
@@ -378,6 +576,18 @@
322+
@@ -378,22 +582,59 @@
317323
this.lastProgress = this.progress;
318324
this.extending = compound.getBoolean("extending");
319325
this.shouldHeadBeRendered = compound.getBoolean("source");
@@ -329,10 +335,32 @@
329335
+
330336
+ // Cache BoundingBoxList creation
331337
+ this.createVoxelShapeBoundingBoxListCache();
338+
+
339+
+ // TISCM cache TileEntity Serialization
340+
+ this.pistonStateNBTCache = null;
332341
}
333342

334343
public NBTTagCompound write(NBTTagCompound compound)
335-
@@ -388,12 +598,19 @@
344+
{
345+
super.write(compound);
346+
- compound.put("blockState", NBTUtil.writeBlockState(this.pistonState));
347+
+
348+
+ // TISCM cache TileEntity Serialization
349+
+// compound.put("blockState", NBTUtil.writeBlockState(this.pistonState));
350+
+ NBTTagCompound blockStateNBT;
351+
+ if (TISCMOptimizationConfig.CACHE_TILE_ENTITY_SERIALIZATION && this.pistonStateNBTCache != null)
352+
+ {
353+
+ blockStateNBT = this.pistonStateNBTCache;
354+
+ }
355+
+ else
356+
+ {
357+
+ // vanilla
358+
+ blockStateNBT = NBTUtil.writeBlockState(this.pistonState);
359+
+ this.pistonStateNBTCache = blockStateNBT;
360+
+ }
361+
+ compound.put("blockState", blockStateNBT);
362+
+
363+
compound.putInt("facing", this.pistonFacing.getIndex());
336364
compound.putFloat("progress", this.lastProgress);
337365
compound.putBoolean("extending", this.extending);
338366
compound.putBoolean("source", this.shouldHeadBeRendered);
@@ -353,7 +381,7 @@
353381

354382
if (!this.extending && this.shouldHeadBeRendered)
355383
{
356-
@@ -424,6 +641,32 @@
384+
@@ -424,6 +665,32 @@
357385
}
358386

359387
float f = this.getExtendedProgress(this.progress);
@@ -386,7 +414,7 @@
386414
double d0 = (double)((float)this.pistonFacing.getXOffset() * f);
387415
double d1 = (double)((float)this.pistonFacing.getYOffset() * f);
388416
double d2 = (double)((float)this.pistonFacing.getZOffset() * f);
389-
@@ -435,4 +678,37 @@
417+
@@ -435,4 +702,37 @@
390418
{
391419
return this.lastTicked;
392420
}

src/main/java/carpet/utils/TISCMOptimizationConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ public class TISCMOptimizationConfig
1111
{
1212
private static final boolean TISCM_OPTIMIZATION_ENABLE = true;
1313

14+
// TISCM Cache BoundingBoxList creation
1415
public static final boolean CACHE_BOUNDING_BOX_LIST_CREATION = TISCM_OPTIMIZATION_ENABLE && true;
16+
// TISCM Larger tile entity list
1517
public static final boolean LARGER_TILE_ENTITY_LIST = TISCM_OPTIMIZATION_ENABLE && true;
18+
// TISCM cache item burn times
1619
public static final boolean CACHE_ITEM_BURN_TIMES = TISCM_OPTIMIZATION_ENABLE && true;
20+
// TISCM cache TileEntity Serialization
21+
public static final boolean CACHE_TILE_ENTITY_SERIALIZATION = TISCM_OPTIMIZATION_ENABLE && true;
1722
}

src/main/java/me/jellysquid/mods/lithium/common/util/collections/TileEntityList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class TileEntityList implements List<TileEntity> {
1414
//TileEntityList does not support double-add of the same object. But it does support multiple at the same position.
1515
//This collection behaves like a set with insertion order. It also provides a position->TileEntity lookup.
1616

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

0 commit comments

Comments
 (0)