From e8459c1ff6a8e58f6d7578a99ea164deadc30884 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sat, 21 Apr 2012 13:47:29 -0500 Subject: [PATCH] Optimize AI and EntityLiving ticking Reduce usage of getCubes as it is an expensive call. Remove iterator usage and object creation from PathfinderGoalSelector methods as these are called very often. Update EntityLiving goal selectors less often as this is still quite an expensive task. --- .../net/minecraft/server/EntityLiving.java | 50 +++++++++++-------- .../server/PathfinderGoalSelector.java | 31 +++++++----- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java index 8c279ec7ab..1f19227588 100644 --- a/src/main/java/net/minecraft/server/EntityLiving.java +++ b/src/main/java/net/minecraft/server/EntityLiving.java @@ -1090,22 +1090,28 @@ public abstract class EntityLiving extends Entity { --this.aN; this.setPosition(d0, d1, d2); this.c(this.yaw, this.pitch); - List list = this.world.getCubes(this, this.boundingBox.shrink(0.03125D, 0.0D, 0.03125D)); - - if (list.size() > 0) { - double d4 = 0.0D; - - for (int i = 0; i < list.size(); ++i) { - AxisAlignedBB axisalignedbb = (AxisAlignedBB) list.get(i); - - if (axisalignedbb.e > d4) { - d4 = axisalignedbb.e; - } - } - - d1 += d4 - this.boundingBox.b; + // CraftBukkit start - getCubes is expensive, use an approximation + if (this.world.getTypeId(MathHelper.floor(d0), MathHelper.floor(d1), MathHelper.floor(d2)) != 0) { + d1 += 1.0D; this.setPosition(d0, d1, d2); } + //List list = this.world.getCubes(this, this.boundingBox.shrink(0.03125D, 0.0D, 0.03125D)); + + //if (list.size() > 0) { + // double d4 = 0.0D; + + // for (int i = 0; i < list.size(); ++i) { + // AxisAlignedBB axisalignedbb = (AxisAlignedBB) list.get(i); + + // if (axisalignedbb.e > d4) { + // d4 = axisalignedbb.e; + // } + // } + + // d1 += d4 - this.boundingBox.b; + // this.setPosition(d0, d1, d2); + //} + // CraftBukkit end } // MethodProfiler.a("ai"); // CraftBukkit - not in production code @@ -1116,13 +1122,13 @@ public abstract class EntityLiving extends Entity { this.aY = 0.0F; } else if (this.aF()) { if (this.c_()) { - MethodProfiler.a("newAi"); + // MethodProfiler.a("newAi"); // CraftBukkit - not in production code this.z_(); - MethodProfiler.a(); + // MethodProfiler.a(); // CraftBukkit - not in production code } else { - MethodProfiler.a("oldAi"); + // MethodProfiler.a("oldAi"); // CraftBukkit - not in production code this.d_(); - MethodProfiler.a(); + // MethodProfiler.a(); // CraftBukkit - not in production code this.X = this.yaw; } } @@ -1234,10 +1240,14 @@ public abstract class EntityLiving extends Entity { this.m.a(); //MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a("targetSelector"); // CraftBukkit - not in production code - this.targetSelector.a(); + // CraftBukkit start - run goal selectors every other tick + if ((this.aV & 1) == 0) { + this.targetSelector.a(); //MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a("goalSelector"); // CraftBukkit - not in production code - this.goalSelector.a(); + this.goalSelector.a(); + } + // CraftBukkit end //MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a("navigation"); // CraftBukkit - not in production code this.navigation.d(); diff --git a/src/main/java/net/minecraft/server/PathfinderGoalSelector.java b/src/main/java/net/minecraft/server/PathfinderGoalSelector.java index 43dc1b9f20..054777c412 100644 --- a/src/main/java/net/minecraft/server/PathfinderGoalSelector.java +++ b/src/main/java/net/minecraft/server/PathfinderGoalSelector.java @@ -7,6 +7,7 @@ public class PathfinderGoalSelector { private ArrayList a = new ArrayList(); private ArrayList b = new ArrayList(); + private ArrayList arraylist = new ArrayList(); // CraftBukkit - create list for object reuse public PathfinderGoalSelector() {} @@ -15,11 +16,12 @@ public class PathfinderGoalSelector { } public void a() { - ArrayList arraylist = new ArrayList(); - Iterator iterator = this.a.iterator(); + //ArrayList arraylist = new ArrayList(); // CraftBukkit + arraylist.clear(); // CraftBukkit - prepare reused list - while (iterator.hasNext()) { - PathfinderGoalSelectorItem pathfindergoalselectoritem = (PathfinderGoalSelectorItem) iterator.next(); + // CraftBukkit start - don't use iterators for private fields + for (int i = 0; i < this.a.size(); i++) { + PathfinderGoalSelectorItem pathfindergoalselectoritem = (PathfinderGoalSelectorItem) this.a.get(i); boolean flag = this.b.contains(pathfindergoalselectoritem); if (flag) { @@ -36,6 +38,7 @@ public class PathfinderGoalSelector { this.b.add(pathfindergoalselectoritem); } } + // CraftBukkit end boolean flag1 = false; @@ -46,8 +49,10 @@ public class PathfinderGoalSelector { Iterator iterator1; PathfinderGoalSelectorItem pathfindergoalselectoritem1; - for (iterator1 = arraylist.iterator(); iterator1.hasNext(); pathfindergoalselectoritem1.a.c()) { - pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator1.next(); + //CraftBukkit start - don't use iterators for private fields + for (int i = 0; i < arraylist.size(); i++) { + pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) arraylist.get(i); + pathfindergoalselectoritem1.a.c(); if (flag1) { System.out.println(pathfindergoalselectoritem1.a.toString() + ", "); } @@ -57,19 +62,20 @@ public class PathfinderGoalSelector { System.out.println("Running: "); } - for (iterator1 = this.b.iterator(); iterator1.hasNext(); pathfindergoalselectoritem1.a.e()) { - pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator1.next(); + for (int i = 0; i < this.b.size(); i++) { + pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) this.b.get(i); + pathfindergoalselectoritem1.a.e(); if (flag1) { System.out.println(pathfindergoalselectoritem1.a.toString()); } } + // CraftBukkit end } private boolean a(PathfinderGoalSelectorItem pathfindergoalselectoritem) { - Iterator iterator = this.a.iterator(); - - while (iterator.hasNext()) { - PathfinderGoalSelectorItem pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator.next(); + // CraftBukkit start - don't use iterators for private fields + for (int i = 0; i < this.a.size(); i++) { + PathfinderGoalSelectorItem pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) this.a.get(i); if (pathfindergoalselectoritem1 != pathfindergoalselectoritem) { if (pathfindergoalselectoritem.b >= pathfindergoalselectoritem1.b) { @@ -81,6 +87,7 @@ public class PathfinderGoalSelector { } } } + // CraftBukkit end return true; }