Skip to content

Commit

Permalink
Remove bitwise thing+ Small optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabwhy committed Jul 21, 2024
1 parent b73dd38 commit fe42d87
Showing 1 changed file with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,10 @@ Subject: [PATCH] Reduce work done in CraftMapCanvas.drawImage by limiting size


diff --git a/src/main/java/org/bukkit/craftbukkit/map/CraftMapCanvas.java b/src/main/java/org/bukkit/craftbukkit/map/CraftMapCanvas.java
index ff59f759669620795ef355c988b664bdcda39f52..54a3c1cea840f45385f1102988f8d2c8c415127b 100644
index ff59f759669620795ef355c988b664bdcda39f52..a5e98571d6d83390761c11e28a0bc3c4415799cd 100644
--- a/src/main/java/org/bukkit/craftbukkit/map/CraftMapCanvas.java
+++ b/src/main/java/org/bukkit/craftbukkit/map/CraftMapCanvas.java
@@ -59,8 +59,10 @@ public class CraftMapCanvas implements MapCanvas {

@Override
public void setPixel(int x, int y, byte color) {
- if (x < 0 || y < 0 || x >= 128 || y >= 128)
+ // Paper start - Optimise bounds check using bitwise operations
+ if ((x & -128) != 0 || (y & -128) != 0)
return;
+ // Paper end
if (this.buffer[y * 128 + x] != color) {
this.buffer[y * 128 + x] = color;
this.mapView.worldMap.setColorsDirty(x, y);
@@ -69,15 +71,19 @@ public class CraftMapCanvas implements MapCanvas {

@Override
public byte getPixel(int x, int y) {
- if (x < 0 || y < 0 || x >= 128 || y >= 128)
+ // Paper start - Optimise bounds check using bitwise operations
+ if ((x & -128) != 0 || (y & -128) != 0)
return 0;
+ // Paper end
return this.buffer[y * 128 + x];
}

@Override
public byte getBasePixel(int x, int y) {
- if (x < 0 || y < 0 || x >= 128 || y >= 128)
+ // Paper start - Optimise bounds check using bitwise operations
+ if ((x & -128) != 0 || (y & -128) != 0)
return 0;
+ // Paper end
return this.base[y * 128 + x];
}

@@ -91,12 +97,41 @@ public class CraftMapCanvas implements MapCanvas {
@@ -91,12 +91,41 @@ public class CraftMapCanvas implements MapCanvas {

@Override
public void drawImage(int x, int y, Image image) {
Expand Down Expand Up @@ -90,3 +56,20 @@ index ff59f759669620795ef355c988b664bdcda39f52..54a3c1cea840f45385f1102988f8d2c8
}

@Override
diff --git a/src/main/java/org/bukkit/craftbukkit/map/CraftMapRenderer.java b/src/main/java/org/bukkit/craftbukkit/map/CraftMapRenderer.java
index 0cbbd915631904fe8c6effefb92895422b33eff6..cf0920e5f84b35647882fb963e9972af4e8427e0 100644
--- a/src/main/java/org/bukkit/craftbukkit/map/CraftMapRenderer.java
+++ b/src/main/java/org/bukkit/craftbukkit/map/CraftMapRenderer.java
@@ -23,8 +23,10 @@ public class CraftMapRenderer extends MapRenderer {
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
// Map
- for (int x = 0; x < 128; ++x) {
- for (int y = 0; y < 128; ++y) {
+ // Paper start - Swap inner and outer loops here to (theoretically) improve cache locality
+ for (int y = 0; y < 128; ++y) {
+ for (int x = 0; x < 128; ++x) {
+ // Paper end
canvas.setPixel(x, y, this.worldMap.colors[y * 128 + x]);
}
}

0 comments on commit fe42d87

Please sign in to comment.