From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Paul Sauve Date: Tue, 9 Feb 2021 19:05:58 -0600 Subject: [PATCH] Reduce memory allocations Airplane Copyright (C) 2020 Technove LLC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/src/main/java/net/minecraft/core/BlockPosition.java b/src/main/java/net/minecraft/core/BlockPosition.java index 8edc279e7a3fdfb7e10718f1deee34b7e3fb2f28..73ec17dea5d5668e49c9a6ad679bd3a362960c72 100644 --- a/src/main/java/net/minecraft/core/BlockPosition.java +++ b/src/main/java/net/minecraft/core/BlockPosition.java @@ -438,6 +438,14 @@ public class BlockPosition extends BaseBlockPosition { public BlockPosition b(int i, int j, int k) { return super.b(i, j, k).immutableCopy(); } + // Airplane start - version of b that doesn't copy + public BlockPosition addValues(int x, int y, int z) { + ((BaseBlockPosition)this).a += x; + ((BaseBlockPosition)this).b += y; + ((BaseBlockPosition)this).e += z; + return this; + } + // Airplane end @Override public BlockPosition shift(EnumDirection enumdirection, int i) { diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java index b28995ecfd7f45e6b6197be96c418aa0d05d3383..914c7a1b18151f29183cfe9474313ce18e7c4ae2 100644 --- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java +++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java @@ -705,7 +705,9 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { return d2 * d2 + d3 * d3; } - private static int b(ChunkCoordIntPair chunkcoordintpair, EntityPlayer entityplayer, boolean flag) { + // Airplane start - create copy that accepts x/z instead of allocating pair + private static int b(ChunkCoordIntPair chunkcoordintpair, EntityPlayer entityplayer, boolean flag) { return someDistanceCalculation(chunkcoordintpair.x, chunkcoordintpair.z, entityplayer, flag); } + private static int someDistanceCalculation(int x, int z, EntityPlayer entityplayer, boolean flag) { int i; int j; @@ -719,12 +721,16 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { j = MathHelper.floor(entityplayer.locZ() / 16.0D); } - return a(chunkcoordintpair, i, j); + return someOtherDistanceCalculation(x, z, i, j); + // Airplane end } - private static int a(ChunkCoordIntPair chunkcoordintpair, int i, int j) { - int k = chunkcoordintpair.x - i; - int l = chunkcoordintpair.z - j; + // Airplane start - create copy that accepts x/z instead of allocating pair + private static int a(ChunkCoordIntPair chunkcoordintpair, int i, int j) { return someOtherDistanceCalculation(chunkcoordintpair.x, chunkcoordintpair.z, i, j); } + private static int someOtherDistanceCalculation(int x, int z, int i, int j) { + int k = x - i; + int l = z - j; + // Airplane end return Math.max(Math.abs(k), Math.abs(l)); } @@ -2541,11 +2547,17 @@ Sections go from 0..16. Now whenever a section is not empty, it can potentially boolean flag1 = this.tracker.attachedToPlayer; if (!flag1) { + // Airplane start - use int/longs instead of ChunkCoordIntPair + /* ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(this.tracker.chunkX, this.tracker.chunkZ); - PlayerChunk playerchunk = PlayerChunkMap.this.getVisibleChunk(chunkcoordintpair.pair()); + */ + int x = this.tracker.chunkX, z = this.tracker.chunkZ; + long chunkcoordintpair = ChunkCoordIntPair.pair(x, z); + PlayerChunk playerchunk = PlayerChunkMap.this.getVisibleChunk(chunkcoordintpair); if (playerchunk != null && playerchunk.getSendingChunk() != null && PlayerChunkMap.this.playerChunkManager.isChunkSent(entityplayer, MathHelper.floor(this.tracker.locX()) >> 4, MathHelper.floor(this.tracker.locZ()) >> 4)) { // Paper - no-tick view distance // Tuinity - don't broadcast in chunks the player hasn't received - flag1 = PlayerChunkMap.b(chunkcoordintpair, entityplayer, false) <= PlayerChunkMap.this.viewDistance; + flag1 = PlayerChunkMap.someDistanceCalculation(x, z, entityplayer, false) <= PlayerChunkMap.this.viewDistance; + // Airplane end } } @@ -2575,8 +2587,10 @@ Sections go from 0..16. Now whenever a section is not empty, it can potentially } private int b() { + // Airplane start + int i = this.trackingDistance; // move out of if statement + if (!this.tracker.passengers.isEmpty()) { Collection collection = this.tracker.getAllPassengers(); - int i = this.trackingDistance; Iterator iterator = collection.iterator(); while (iterator.hasNext()) { @@ -2588,6 +2602,8 @@ Sections go from 0..16. Now whenever a section is not empty, it can potentially i = j; } } + } + // Airplane end return this.a(i); } diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java index ce428fd701fe4a0505a629edb322f19d730f8132..136faf61770011a830cc58259d1dad11830ca808 100644 --- a/src/main/java/net/minecraft/server/level/WorldServer.java +++ b/src/main/java/net/minecraft/server/level/WorldServer.java @@ -1107,7 +1107,28 @@ public class WorldServer extends World implements GeneratorAccessSeed { gameprofilerfiller.enter("tick"); if (!entity.dead && !(entity instanceof EntityComplexPart)) { + // Airplane start - inline this.a to prevent creation of lambda + /* this.a(this::entityJoinedWorld, entity); + */ + boolean doMidTick = false; // usually there's a returns in the catch, so treat it like that + try { + this.entityJoinedWorld(entity); + doMidTick = true; + } catch (Throwable throwable) { + if (throwable instanceof ThreadDeath) throw throwable; // Paper + // Paper start - Prevent tile entity and entity crashes + String msg = "Entity threw exception at " + entity.world.getWorld().getName() + ":" + entity.locX() + "," + entity.locY() + "," + entity.locZ(); + System.err.println(msg); + throwable.printStackTrace(); + getServer().getPluginManager().callEvent(new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerInternalException(msg, throwable))); + entity.dead = true; + // Paper end + } + if (doMidTick) { + MinecraftServer.getServer().executeMidTickTasks(); // Tuinity - execute chunk tasks mid tick + } + // Airplane end } gameprofilerfiller.exit(); @@ -1451,9 +1472,14 @@ public class WorldServer extends World implements GeneratorAccessSeed { ++entity.ticksLived; GameProfilerFiller gameprofilerfiller = this.getMethodProfiler(); + // Airplane start - create debug lambda once, todo do we even WANT the method profiler? + /* gameprofilerfiller.a(() -> { return IRegistry.ENTITY_TYPE.getKey(entity.getEntityType()).toString(); }); + */ + gameprofilerfiller.a(entity.getEntityType().getEntityName); + // Airplane end gameprofilerfiller.c("tickNonPassenger"); if (isActive) { // Paper - EAR 2 TimingHistory.activatedEntityTicks++; // Paper diff --git a/src/main/java/net/minecraft/world/entity/EntityTypes.java b/src/main/java/net/minecraft/world/entity/EntityTypes.java index 80c229c1852199fda85c03453d64cae33e413e89..7f70dda656ff9d802200f18139d2695e58c551c7 100644 --- a/src/main/java/net/minecraft/world/entity/EntityTypes.java +++ b/src/main/java/net/minecraft/world/entity/EntityTypes.java @@ -270,6 +270,8 @@ public class EntityTypes { private MinecraftKey bq; private final EntitySize br; + public java.util.function.Supplier getEntityName = () -> IRegistry.ENTITY_TYPE.getKey(this).toString(); // Airplane - create lambda ones + private static EntityTypes a(String s, EntityTypes.Builder entitytypes_builder) { // CraftBukkit - decompile error return (EntityTypes) IRegistry.a((IRegistry) IRegistry.ENTITY_TYPE, s, (Object) entitytypes_builder.a(s)); } diff --git a/src/main/java/net/minecraft/world/level/World.java b/src/main/java/net/minecraft/world/level/World.java index 3e08e56f67516333e7d880fdc595447a3ae4dc83..827a0d436be843033e7bf1880e833a1e282c123c 100644 --- a/src/main/java/net/minecraft/world/level/World.java +++ b/src/main/java/net/minecraft/world/level/World.java @@ -1103,19 +1103,19 @@ public abstract class World implements GeneratorAccess, AutoCloseable { public void a(Consumer consumer, Entity entity) { try { - consumer.accept(entity); + consumer.accept(entity); // Airplane - error on change } catch (Throwable throwable) { if (throwable instanceof ThreadDeath) throw throwable; // Paper // Paper start - Prevent tile entity and entity crashes String msg = "Entity threw exception at " + entity.world.getWorld().getName() + ":" + entity.locX() + "," + entity.locY() + "," + entity.locZ(); System.err.println(msg); throwable.printStackTrace(); - getServer().getPluginManager().callEvent(new ServerExceptionEvent(new ServerInternalException(msg, throwable))); + getServer().getPluginManager().callEvent(new ServerExceptionEvent(new ServerInternalException(msg, throwable))); // Airplane - error on change entity.dead = true; return; // Paper end } - MinecraftServer.getServer().executeMidTickTasks(); // Tuinity - execute chunk tasks mid tick + MinecraftServer.getServer().executeMidTickTasks(); // Tuinity - execute chunk tasks mid tick // Airplane - error on change } // Paper start - Prevent armor stands from doing entity lookups @Override diff --git a/src/main/java/net/minecraft/world/level/block/BlockDirtSnowSpreadable.java b/src/main/java/net/minecraft/world/level/block/BlockDirtSnowSpreadable.java index 712596420af83e6e1b9d147ae2fd8d8a1f36e1b9..9c29fa3efac7e16df81b8a44934e3286bb37f1f6 100644 --- a/src/main/java/net/minecraft/world/level/block/BlockDirtSnowSpreadable.java +++ b/src/main/java/net/minecraft/world/level/block/BlockDirtSnowSpreadable.java @@ -54,8 +54,14 @@ public abstract class BlockDirtSnowSpreadable extends BlockDirtSnow { if (worldserver.getLightLevel(blockposition.up()) >= 9) { IBlockData iblockdata1 = this.getBlockData(); + // Airplane start - use mutable position + BlockPosition.MutableBlockPosition blockposition1 = new BlockPosition.MutableBlockPosition(); for (int i = 0; i < 4; ++i) { + blockposition1.setValues(blockposition).addValues(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1); + /* BlockPosition blockposition1 = blockposition.b(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1); + */ + // Airplane end if (worldserver.getType(blockposition1).a(Blocks.DIRT) && c(iblockdata1, (IWorldReader) worldserver, blockposition1)) { org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockSpreadEvent(worldserver, blockposition, blockposition1, (IBlockData) iblockdata1.set(BlockDirtSnowSpreadable.a, worldserver.getType(blockposition1.up()).a(Blocks.SNOW))); // CraftBukkit