mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 04:00:30 +00:00
Memorize getRenderBoundingBox
This takes a non-trivial amount of time on the render thread[^1], so worth doing. I don't actually think the allocation is the heavy thing here - VisualVM says it's toWorldPos being slow. I'm not sure why - possibly just all the block property lookups? [^2] [^1]: To be clear, this is with 120 monitors and no other block entities with custom renderers. so not really representative. [^2]: I wish I could provide a narrower range, but it varies so much between me restarting the game. Makes it impossible to benchmark anything!
This commit is contained in:
parent
1196568a7c
commit
78aa757549
@ -70,6 +70,11 @@ public class TileMonitor extends TileGeneric
|
|||||||
private int xIndex = 0;
|
private int xIndex = 0;
|
||||||
private int yIndex = 0;
|
private int yIndex = 0;
|
||||||
|
|
||||||
|
private BlockPos bbPos;
|
||||||
|
private BlockState bbState;
|
||||||
|
private int bbX, bbY, bbWidth, bbHeight;
|
||||||
|
private AxisAlignedBB boundingBox;
|
||||||
|
|
||||||
public TileMonitor( TileEntityType<? extends TileMonitor> type, boolean advanced )
|
public TileMonitor( TileEntityType<? extends TileMonitor> type, boolean advanced )
|
||||||
{
|
{
|
||||||
super( type );
|
super( type );
|
||||||
@ -624,9 +629,25 @@ public class TileMonitor extends TileGeneric
|
|||||||
@Override
|
@Override
|
||||||
public AxisAlignedBB getRenderBoundingBox()
|
public AxisAlignedBB getRenderBoundingBox()
|
||||||
{
|
{
|
||||||
|
// We attempt to cache the bounding box to save having to do property lookups (and allocations!) on every frame.
|
||||||
|
// Unfortunately the AABB does depend on quite a lot of state, so we need to add a bunch of extra fields -
|
||||||
|
// ideally these'd be a single object, but I don't think worth doing until Java has value types.
|
||||||
|
if( boundingBox != null && getBlockState().equals( bbState ) && getBlockPos().equals( bbPos ) &&
|
||||||
|
xIndex == bbX && yIndex == bbY && width == bbWidth && height == bbHeight )
|
||||||
|
{
|
||||||
|
return boundingBox;
|
||||||
|
}
|
||||||
|
|
||||||
|
bbState = getBlockState();
|
||||||
|
bbPos = getBlockPos();
|
||||||
|
bbX = xIndex;
|
||||||
|
bbY = yIndex;
|
||||||
|
bbWidth = width;
|
||||||
|
bbHeight = height;
|
||||||
|
|
||||||
BlockPos startPos = toWorldPos( 0, 0 );
|
BlockPos startPos = toWorldPos( 0, 0 );
|
||||||
BlockPos endPos = toWorldPos( width, height );
|
BlockPos endPos = toWorldPos( width, height );
|
||||||
return new AxisAlignedBB(
|
return boundingBox = new AxisAlignedBB(
|
||||||
Math.min( startPos.getX(), endPos.getX() ),
|
Math.min( startPos.getX(), endPos.getX() ),
|
||||||
Math.min( startPos.getY(), endPos.getY() ),
|
Math.min( startPos.getY(), endPos.getY() ),
|
||||||
Math.min( startPos.getZ(), endPos.getZ() ),
|
Math.min( startPos.getZ(), endPos.getZ() ),
|
||||||
|
Loading…
Reference in New Issue
Block a user