More progress

we've got it to render using binarySearch but its not ideal and it has the same speed as without it....
This commit is contained in:
Ivan Pekov 2020-12-03 10:15:07 +02:00
parent 1bfece82be
commit b6b0e23f1a
No known key found for this signature in database
GPG Key ID: BC975C392D9CA3A3
3 changed files with 9 additions and 76 deletions

View File

@ -55,7 +55,6 @@ # Patches
| server | Despawn rate config options per projectile type | jmp | |
| api | Disable reload command | Ivan Pekov | |
| server | Disable the Snooper | Sotr | |
| server | Don't compare colors in MapTest | Ivan Pekov | |
| server | Don't load chunk with seed based feature search | Phoenix616 | |
| server | Don't trigger Lootable Refresh for non player interaction | Aikar | |
| server | Don't wake up entities when damage event is cancelled | Phoenix616 | |

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Optimise Bukkit's MapPalette
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..c7ebce72a97791da9b61dd04b8b7bb26c3ec7bce 100644
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..418fde209a2c4ce71dbb8e41af32cd87cfc71ed4 100644
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
@@ -23,6 +23,8 @@ public final class MapPalette {
@ -17,7 +17,7 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..c7ebce72a97791da9b61dd04b8b7bb26
double rmean = (c1.getRed() + c2.getRed()) / 2.0;
double r = c1.getRed() - c2.getRed();
double g = c1.getGreen() - c2.getGreen();
@@ -31,10 +33,20 @@ public final class MapPalette {
@@ -31,6 +33,16 @@ public final class MapPalette {
double weightG = 4.0;
double weightB = 2 + (255 - rmean) / 256.0;
return weightR * r * r + weightG * g * g + weightB * b * b;
@ -34,40 +34,17 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..c7ebce72a97791da9b61dd04b8b7bb26
}
@NotNull
- static final Color[] colors = {
+ static Color[] colors = { // Yatopia
c(0, 0, 0), c(0, 0, 0), c(0, 0, 0), c(0, 0, 0),
c(89, 125, 39), c(109, 153, 48), c(127, 178, 56), c(67, 94, 29),
c(174, 164, 115), c(213, 201, 140), c(247, 233, 163), c(130, 123, 86),
@@ -96,6 +108,21 @@ public final class MapPalette {
c(14, 127, 93), c(17, 155, 114), c(20, 180, 133), c(10, 95, 70)
};
+ // Yatopia start - all colors as rgb integers
+ @NotNull
+ static final int[] rgbColors = new int[colors.length];
+ static {
+ for (int i = 0; i < colors.length; i++) {
+ rgbColors[i] = colors[i].getRGB();
+ }
+ java.util.Arrays.sort(rgbColors);
+ Color[] sortedColorsArray = java.util.Arrays.stream(colors)
+ .sorted(java.util.Comparator.comparingInt(Color::getRGB))
+ .toArray(Color[]::new);
+ colors = sortedColorsArray;
+ }
+ // Yatopia end
+
// Interface
/**
* @deprecated Magic value
@@ -235,19 +262,43 @@ public final class MapPalette {
@@ -235,19 +247,31 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
if (color.getAlpha() < 128) return 0;
- int index = 0;
+ // Yatopia start - binary search first
+ int binaryIndex = java.util.Arrays.binarySearch(rgbColors, color.getRGB());
+ int binaryIndex = java.util.Arrays.binarySearch(
+ colors,
+ color,
+ java.util.Comparator.comparingDouble(o -> getDistance(color, o))
+ );
+ if (binaryIndex != -1) {
+ return (byte) (binaryIndex < 128 ? binaryIndex : binaryIndex - 256);
+ }
@ -79,27 +56,11 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..c7ebce72a97791da9b61dd04b8b7bb26
- for (int i = 4; i < colors.length; i++) {
+ for (short i = 4; i < colors.length; i++) { // Yatopia
double distance = getDistance(color, colors[i]);
+ // Yatopia start - optimise this
+ /*
if (distance < best || best == -1) {
best = distance;
index = i;
+ if (distance == 0) { break; } // Yatopia
}
+ */
+ if (i == 4) {
+ if (distance == 0) {
+ return (byte) i;
+ }
+ best = distance;
+ index = i;
+ } else if (distance < best) {
+ if (distance == 0) {
+ return (byte) (i < 128 ? i : i - 256);
+ }
+ best = distance;
+ index = i;
+ }
+ // Yatopia end
}
// Minecraft has 143 colors, some of which have negative byte representations

View File

@ -1,27 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Wed, 2 Dec 2020 21:34:27 +0200
Subject: [PATCH] Don't compare colors in MapTest
It breaks our strategy of speeding up MapPalette.
diff --git a/src/test/java/org/bukkit/map/MapTest.java b/src/test/java/org/bukkit/map/MapTest.java
index 2dde26d3fd4a51828a0aa6e40831ba33aaebeccc..413d089dc8e496a97c83961882fb3eb24bd1efd4 100644
--- a/src/test/java/org/bukkit/map/MapTest.java
+++ b/src/test/java/org/bukkit/map/MapTest.java
@@ -39,6 +39,7 @@ public class MapTest {
}
fail = true;
} else {
+ /* // Yatopia - NO!!! We have different strategy of doing colors in MapPalette
for (int j = 0; j < modifiers.length; j++) {
int modi = modifiers[j];
Color bukkit = bukkitColors[i * 4 + j];
@@ -55,6 +56,7 @@ public class MapTest {
fail = true;
}
}
+ */ // Yatopia
}
}
Assert.assertFalse(fail);