2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								From: Aikar <aikar@aikar.co>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								diff --git a/src/main/java/net/minecraft/util/SimpleBitStorage.java b/src/main/java/net/minecraft/util/SimpleBitStorage.java
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-16 12:44:59 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								index dea4f322f750a0a09407fdb48d5d6e809dfe8ed4..9f438d9c6eb05e43d24e4af68188a3d4c46a938c 100644
 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								--- a/src/main/java/net/minecraft/util/SimpleBitStorage.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+++ b/src/main/java/net/minecraft/util/SimpleBitStorage.java
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -204,8 +204,8 @@ public class SimpleBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     private final long mask;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     private final int size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     private final int valuesPerLong;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-    private final int divideMul;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-    private final int divideAdd;
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    private final int divideMul; private final long divideMulUnsigned; // Paper - Perf: Optimize SimpleBitStorage; referenced in b(int) with 2 Integer.toUnsignedLong calls
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+    private final int divideAdd; private final long divideAddUnsigned; // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     private final int divideShift;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2022-06-07 21:55:39 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     public SimpleBitStorage(int elementBits, int size, int[] data) {
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -248,8 +248,8 @@ public class SimpleBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         this.mask = (1L << elementBits) - 1L;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         this.valuesPerLong = (char)(64 / elementBits);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         int i = 3 * (this.valuesPerLong - 1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        this.divideMul = MAGIC[i + 0];
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        this.divideAdd = MAGIC[i + 1];
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        this.divideMul = MAGIC[i + 0]; this.divideMulUnsigned = Integer.toUnsignedLong(this.divideMul); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        this.divideAdd = MAGIC[i + 1]; this.divideAddUnsigned = Integer.toUnsignedLong(this.divideAdd); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         this.divideShift = MAGIC[i + 2];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         int j = (size + this.valuesPerLong - 1) / this.valuesPerLong;
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         if (data != null) {
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -264,15 +264,15 @@ public class SimpleBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     private int cellIndex(int index) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        long l = Integer.toUnsignedLong(this.divideMul);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        long m = Integer.toUnsignedLong(this.divideAdd);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-16 12:44:59 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-        return (int)((long)index * l + m >> 32 >> this.divideShift);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        //long l = Integer.toUnsignedLong(this.divideMul); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //long m = Integer.toUnsignedLong(this.divideAdd); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        return (int) (index * this.divideMulUnsigned + this.divideAddUnsigned >> 32 >> this.divideShift); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								-    public int getAndSet(int index, int value) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, this.mask, (long)value);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final int getAndSet(int index, int value) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, this.mask, (long)value); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         int i = this.cellIndex(index);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         long l = this.data[i];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         int j = (index - i * this.valuesPerLong) * this.bits;
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -282,9 +282,9 @@ public class SimpleBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								-    public void set(int index, int value) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, this.mask, (long)value);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final void set(int index, int value) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, this.mask, (long)value); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         int i = this.cellIndex(index);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         long l = this.data[i];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         int j = (index - i * this.valuesPerLong) * this.bits;
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -292,8 +292,8 @@ public class SimpleBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 14:22:49 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								-    public int get(int index) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final int get(int index) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-26 19:41:41 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 20:07:57 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         int i = this.cellIndex(index);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         long l = this.data[i];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         int j = (index - i * this.valuesPerLong) * this.bits;
							 
						 
					
						
							
								
									
										
										
										
											2021-12-04 23:41:44 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								diff --git a/src/main/java/net/minecraft/util/ZeroBitStorage.java b/src/main/java/net/minecraft/util/ZeroBitStorage.java
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								index 8dd5a5899e9b5d8b3f1a6064dd7c1580313da69e..50040c497a819cd1229042ab3cb057d34a32cacc 100644
 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-04 23:41:44 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								--- a/src/main/java/net/minecraft/util/ZeroBitStorage.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+++ b/src/main/java/net/minecraft/util/ZeroBitStorage.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@@ -13,21 +13,21 @@ public class ZeroBitStorage implements BitStorage {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-    public int getAndSet(int index, int value) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, 0L, (long)value);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final int getAndSet(int index, int value) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, 0L, (long)value); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-04 23:41:44 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-    public void set(int index, int value) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, 0L, (long)value);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final void set(int index, int value) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, 0L, (long)value); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-04 23:41:44 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     @Override
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-    public int get(int index) {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-        Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-23 14:34:17 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    public final int get(int index) { // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        //Validate.inclusiveBetween(0L, (long)(this.size - 1), (long)index); // Paper - Perf: Optimize SimpleBitStorage
 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-04 23:41:44 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     }