From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Wed, 15 Nov 2023 23:39:36 -0300 Subject: [PATCH] SparklyPaper: Skip "distanceToSqr" call in "ServerEntity#sendChanges" if the delta movement hasn't changed Original project: https://github.com/SparklyPower/SparklyPaper The "distanceToSqr" call is a bit expensive, so avoiding it is pretty nice, around ~15% calls are skipped with this check We could also check if the x,y,z coordinates are equal, but for now, let's just keep the identity check, which also helps us since Minecraft's code does reuse the original delta movement Vec3 object diff --git a/net/minecraft/server/level/ServerEntity.java b/net/minecraft/server/level/ServerEntity.java index 7fe21b10eefce56dde19baebf9cb6d2d0a8d73ec..b3fe9ea70148cdbefbdb617abaf81fe48ee26685 100644 --- a/net/minecraft/server/level/ServerEntity.java +++ b/net/minecraft/server/level/ServerEntity.java @@ -211,6 +211,7 @@ public class ServerEntity { if (this.entity.hasImpulse || this.trackDelta || this.entity instanceof LivingEntity && ((LivingEntity)this.entity).isFallFlying()) { Vec3 deltaMovement = this.entity.getDeltaMovement(); + if (deltaMovement != this.lastSentMovement) { // SparklyPaper start - skip distanceToSqr call in ServerEntity#sendChanges if the delta movement hasn't changed double d = deltaMovement.distanceToSqr(this.lastSentMovement); if (d > 1.0E-7 || d > 0.0 && deltaMovement.lengthSqr() == 0.0) { this.lastSentMovement = deltaMovement; @@ -228,6 +229,7 @@ public class ServerEntity { this.broadcast.accept(new ClientboundSetEntityMotionPacket(this.entity.getId(), this.lastSentMovement)); } } + } // SparklyPaper end } if (packet != null) {