From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Wed, 23 Dec 2020 09:55:43 +0200
Subject: [PATCH] Don't unnecessarily copy the passenger list

It is only copied if it's needed, and that is in EntityTrackerEntry, when we've detected a change.

diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index 51c4ff1bd6948ab46d723c37af39c76cdf662be5..a48b56839a3516baa7d3373295b259a24012109e 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -3282,7 +3282,12 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
     }
 
     public List<Entity> getPassengers() {
+        // Yatopia start - don't copy passengers list
+        /*
         return (List) (this.passengers.isEmpty() ? Collections.emptyList() : Lists.newArrayList(this.passengers));
+         */
+        return this.passengers.isEmpty() ? Collections.emptyList() : this.passengers;
+        // Yatopia end
     }
 
     public boolean w(Entity entity) {
diff --git a/src/main/java/net/minecraft/server/EntityTrackerEntry.java b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
index 228236bce14bfdf930570b453862dcfaae9e4823..0be11d1263d84b270c25ec8249832164dc75409d 100644
--- a/src/main/java/net/minecraft/server/EntityTrackerEntry.java
+++ b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
@@ -78,7 +78,7 @@ public class EntityTrackerEntry {
         List<Entity> list = this.tracker.getPassengers();
 
         if (!list.equals(this.p)) {
-            this.p = list;
+            this.p = com.google.common.collect.ImmutableList.copyOf(list); // Yatopia - only copy list if something has changed
             this.broadcastIncludingSelf(new PacketPlayOutMount(this.tracker)); // CraftBukkit
         }
 
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 6c39796e672a6412e85f69a6a3dfdb1037e51d30..eba7a33cb0c2f0bc18eda709dd1c2b341ddb36df 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -1430,7 +1430,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
             this.chunkCheck(entity);
             } finally { timer.stopTiming(); } // Paper - timings
             if (entity.inChunk) {
-                Iterator iterator = entity.getPassengers().iterator();
+                Iterator iterator = new java.util.ArrayList<>(entity.getPassengers()).iterator(); // Yatopia - copy the passengers list here too
 
                 while (iterator.hasNext()) {
                     Entity entity1 = (Entity) iterator.next();
