From eee1fedc1e044b02a6c4230a481c57f9355e8213 Mon Sep 17 00:00:00 2001 From: Aikar Date: Wed, 30 Dec 2020 14:02:44 -0500 Subject: [PATCH] Micro Optimize DataBits There was 2 unnecessary lines of code in a core method used for DataBits that repeatedly calculated a static value. We now just precalculate that in the constructor and reuse the value instead. This is a micro optimization, but this is some of the hottest code in the server so it should provide some minor gains as well as improve inlining. Additionally, to further help inlining, mark the DataBits methods as final. --- Spigot-Server-Patches/Optimize-DataBits.patch | 84 +++++++++++++++++++ .../Remove-Debug-checks-from-DataBits.patch | 45 ---------- 2 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 Spigot-Server-Patches/Optimize-DataBits.patch delete mode 100644 Spigot-Server-Patches/Remove-Debug-checks-from-DataBits.patch diff --git a/Spigot-Server-Patches/Optimize-DataBits.patch b/Spigot-Server-Patches/Optimize-DataBits.patch new file mode 100644 index 0000000000..0dc8872bd8 --- /dev/null +++ b/Spigot-Server-Patches/Optimize-DataBits.patch @@ -0,0 +1,84 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Aikar +Date: Tue, 5 Apr 2016 21:38:58 -0400 +Subject: [PATCH] Optimize DataBits + +Remove Debug checks as these are super hot and causing noticeable hits + +Before: http://i.imgur.com/nQsMzAE.png +After: http://i.imgur.com/nJ46crB.png + +Optimize redundant converting of static fields into an unsigned long each call by precomputing it in ctor + +diff --git a/src/main/java/net/minecraft/server/DataBits.java b/src/main/java/net/minecraft/server/DataBits.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/server/DataBits.java ++++ b/src/main/java/net/minecraft/server/DataBits.java +@@ -0,0 +0,0 @@ public class DataBits { + private final long d; + private final int e; + private final int f; +- private final int g; +- private final int h; ++ private final int g;private final long g_unsigned; // Paper - referenced in b(int) with 2 Integer.toUnsignedLong calls ++ private final int h;private final long h_unsigned; // Paper + private final int i; + + public DataBits(int i, int j) { +@@ -0,0 +0,0 @@ public class DataBits { + this.f = (char) (64 / i); + int k = 3 * (this.f - 1); + +- this.g = DataBits.a[k + 0]; +- this.h = DataBits.a[k + 1]; ++ this.g = DataBits.a[k + 0]; this.g_unsigned = Integer.toUnsignedLong(this.g); // Paper ++ this.h = DataBits.a[k + 1]; this.h_unsigned = Integer.toUnsignedLong(this.h); // Paper + this.i = DataBits.a[k + 2]; + int l = (j + this.f - 1) / this.f; + +@@ -0,0 +0,0 @@ public class DataBits { + } + + private int b(int i) { +- long j = Integer.toUnsignedLong(this.g); +- long k = Integer.toUnsignedLong(this.h); ++ //long j = Integer.toUnsignedLong(this.g); // Paper ++ //long k = Integer.toUnsignedLong(this.h); // Paper + +- return (int) ((long) i * j + k >> 32 >> this.i); ++ return (int) ((long) i * this.g_unsigned + this.h_unsigned >> 32 >> this.i); // Paper + } + +- public int a(int i, int j) { +- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); +- Validate.inclusiveBetween(0L, this.d, (long) j); ++ public final int a(int i, int j) { // Paper - make final for inline ++ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper ++ //Validate.inclusiveBetween(0L, this.d, (long) j); // Paper + int k = this.b(i); + long l = this.b[k]; + int i1 = (i - k * this.f) * this.c; +@@ -0,0 +0,0 @@ public class DataBits { + return j1; + } + +- public void b(int i, int j) { +- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); +- Validate.inclusiveBetween(0L, this.d, (long) j); ++ public final void b(int i, int j) { // Paper - make final for inline ++ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper ++ //Validate.inclusiveBetween(0L, this.d, (long) j); // Paper + int k = this.b(i); + long l = this.b[k]; + int i1 = (i - k * this.f) * this.c; +@@ -0,0 +0,0 @@ public class DataBits { + this.b[k] = l & ~(this.d << i1) | ((long) j & this.d) << i1; + } + +- public int a(int i) { +- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); ++ public final int a(int i) { // Paper - make final for inline ++ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper + int j = this.b(i); + long k = this.b[j]; + int l = (i - j * this.f) * this.c; diff --git a/Spigot-Server-Patches/Remove-Debug-checks-from-DataBits.patch b/Spigot-Server-Patches/Remove-Debug-checks-from-DataBits.patch deleted file mode 100644 index 24214b1ed6..0000000000 --- a/Spigot-Server-Patches/Remove-Debug-checks-from-DataBits.patch +++ /dev/null @@ -1,45 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Aikar -Date: Tue, 5 Apr 2016 21:38:58 -0400 -Subject: [PATCH] Remove Debug checks from DataBits - -These are super hot and causing noticeable hits - -Before: http://i.imgur.com/nQsMzAE.png -After: http://i.imgur.com/nJ46crB.png - -diff --git a/src/main/java/net/minecraft/server/DataBits.java b/src/main/java/net/minecraft/server/DataBits.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/server/DataBits.java -+++ b/src/main/java/net/minecraft/server/DataBits.java -@@ -0,0 +0,0 @@ public class DataBits { - } - - public int a(int i, int j) { -- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); -- Validate.inclusiveBetween(0L, this.d, (long) j); -+ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper -+ //Validate.inclusiveBetween(0L, this.d, (long) j); // Paper - int k = this.b(i); - long l = this.b[k]; - int i1 = (i - k * this.f) * this.c; -@@ -0,0 +0,0 @@ public class DataBits { - } - - public void b(int i, int j) { -- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); -- Validate.inclusiveBetween(0L, this.d, (long) j); -+ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper -+ //Validate.inclusiveBetween(0L, this.d, (long) j); // Paper - int k = this.b(i); - long l = this.b[k]; - int i1 = (i - k * this.f) * this.c; -@@ -0,0 +0,0 @@ public class DataBits { - } - - public int a(int i) { -- Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); -+ //Validate.inclusiveBetween(0L, (long) (this.e - 1), (long) i); // Paper - int j = this.b(i); - long k = this.b[j]; - int l = (i - j * this.f) * this.c;