SPIGOT-1388: Branchless NibbleArray

This commit is contained in:
md_5 2016-07-30 11:33:13 +10:00
parent af8198d44a
commit 90f61bc448
2 changed files with 137 additions and 1 deletions

View file

@ -1,4 +1,4 @@
From c2c4378d7d78148502d76b9ba476ff4db2499310 Mon Sep 17 00:00:00 2001
From 9cc906c7892b928cb1b7895fab0aacad410070e8 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Sun, 1 Dec 2013 15:10:48 +1100
Subject: [PATCH] mc-dev imports
@ -750,6 +750,70 @@ index 0000000..4aa2d89
+ return this.c();
+ }
+}
diff --git a/src/main/java/net/minecraft/server/NibbleArray.java b/src/main/java/net/minecraft/server/NibbleArray.java
new file mode 100644
index 0000000..1509c77
--- /dev/null
+++ b/src/main/java/net/minecraft/server/NibbleArray.java
@@ -0,0 +1,58 @@
+package net.minecraft.server;
+
+public class NibbleArray {
+
+ private final byte[] a;
+
+ public NibbleArray() {
+ this.a = new byte[2048];
+ }
+
+ public NibbleArray(byte[] abyte) {
+ this.a = abyte;
+ if (abyte.length != 2048) {
+ throw new IllegalArgumentException("ChunkNibbleArrays should be 2048 bytes not: " + abyte.length);
+ }
+ }
+
+ public int a(int i, int j, int k) {
+ return this.a(this.b(i, j, k));
+ }
+
+ public void a(int i, int j, int k, int l) {
+ this.a(this.b(i, j, k), l);
+ }
+
+ private int b(int i, int j, int k) {
+ return j << 8 | k << 4 | i;
+ }
+
+ public int a(int i) {
+ int j = this.c(i);
+
+ return this.b(i) ? this.a[j] & 15 : this.a[j] >> 4 & 15;
+ }
+
+ public void a(int i, int j) {
+ int k = this.c(i);
+
+ if (this.b(i)) {
+ this.a[k] = (byte) (this.a[k] & 240 | j & 15);
+ } else {
+ this.a[k] = (byte) (this.a[k] & 15 | (j & 15) << 4);
+ }
+
+ }
+
+ private boolean b(int i) {
+ return (i & 1) == 0;
+ }
+
+ private int c(int i) {
+ return i >> 1;
+ }
+
+ public byte[] asBytes() {
+ return this.a;
+ }
+}
diff --git a/src/main/java/net/minecraft/server/PacketHandshakingInSetProtocol.java b/src/main/java/net/minecraft/server/PacketHandshakingInSetProtocol.java
new file mode 100644
index 0000000..1cb9d60

View file

@ -0,0 +1,72 @@
From bf28b198c7543a5370be312b54d1823f2adcac39 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Sat, 30 Jul 2016 11:29:14 +1000
Subject: [PATCH] Branchless NibbleArray
diff --git a/src/main/java/net/minecraft/server/NibbleArray.java b/src/main/java/net/minecraft/server/NibbleArray.java
index 1509c77..79c5bc4 100644
--- a/src/main/java/net/minecraft/server/NibbleArray.java
+++ b/src/main/java/net/minecraft/server/NibbleArray.java
@@ -30,18 +30,16 @@ public class NibbleArray {
public int a(int i) {
int j = this.c(i);
- return this.b(i) ? this.a[j] & 15 : this.a[j] >> 4 & 15;
+ return this.a[j] >> ((i & 1) << 2) & 15; // Spigot
}
public void a(int i, int j) {
int k = this.c(i);
- if (this.b(i)) {
- this.a[k] = (byte) (this.a[k] & 240 | j & 15);
- } else {
- this.a[k] = (byte) (this.a[k] & 15 | (j & 15) << 4);
- }
-
+ // Spigot start
+ int shift = (i & 1) << 2;
+ this.a[k] = (byte) (this.a[k] & ~(15 << shift) | (j & 15) << shift);
+ // Spigot end
}
private boolean b(int i) {
diff --git a/src/test/java/org/bukkit/NibbleArrayTest.java b/src/test/java/org/bukkit/NibbleArrayTest.java
new file mode 100644
index 0000000..0131ff0
--- /dev/null
+++ b/src/test/java/org/bukkit/NibbleArrayTest.java
@@ -0,0 +1,29 @@
+package org.bukkit;
+
+import java.util.Random;
+import net.minecraft.server.NibbleArray;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NibbleArrayTest {
+
+ private static final int NIBBLE_SIZE = 4096;
+
+ @Test
+ public void testNibble() {
+ Random random = new Random();
+ byte[] classic = new byte[NIBBLE_SIZE];
+ NibbleArray nibble = new NibbleArray();
+
+ for (int i = 0; i < classic.length; i++) {
+ byte number = (byte) (random.nextInt() & 0xF);
+
+ classic[i] = number;
+ nibble.a(i, number);
+ }
+
+ for (int i = 0; i < classic.length; i++) {
+ Assert.assertEquals("Nibble array mismatch", classic[i], nibble.a(i));
+ }
+ }
+}
--
2.7.4