Use a crc32 of all the blocks in the chunk as our hash key. This is much more unique.

This commit is contained in:
md_5 2014-01-29 12:51:20 +11:00
parent 1bf13129a5
commit a780739071

View file

@ -1,4 +1,4 @@
From e752e877dd0a7fffdcbd8a375ee35f47db3ca983 Mon Sep 17 00:00:00 2001 From 8d913a3ada460ef4eb29dd7be5c09aa6d440d349 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net> From: md_5 <git@md-5.net>
Date: Tue, 28 Jan 2014 20:32:07 +1100 Date: Tue, 28 Jan 2014 20:32:07 +1100
Subject: [PATCH] Implement Threaded Bulk Chunk Compression and Caching Subject: [PATCH] Implement Threaded Bulk Chunk Compression and Caching
@ -17,23 +17,18 @@ index 9b853a9..a4c8843 100644
Iterator iterator2 = arraylist1.iterator(); Iterator iterator2 = arraylist1.iterator();
diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
index 30bf8a7..e05c870 100644 index 30bf8a7..c27b2e3 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java --- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java +++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
@@ -8,13 +8,13 @@ import java.util.zip.Inflater; @@ -12,9 +12,9 @@ public class PacketPlayOutMapChunkBulk extends Packet {
private int[] b;
public class PacketPlayOutMapChunkBulk extends Packet {
- private int[] a;
- private int[] b;
+ public int[] a; // Spigot
+ public int[] b; // Spigot
private int[] c; private int[] c;
private int[] d; private int[] d;
- private byte[] buffer; - private byte[] buffer;
+ public byte[] buffer; // Spigot - private byte[][] inflatedBuffers;
private byte[][] inflatedBuffers;
- private int size; - private int size;
+ public byte[] buffer; // Spigot
+ public byte[][] inflatedBuffers; // Spigot
+ public int size; // Spigot + public int size; // Spigot
private boolean h; private boolean h;
private byte[] buildBuffer = new byte[0]; // CraftBukkit - remove static private byte[] buildBuffer = new byte[0]; // CraftBukkit - remove static
@ -124,16 +119,16 @@ index fb95be4..2875c94 100644
ServerConnection.a(this.a).add(networkmanager); ServerConnection.a(this.a).add(networkmanager);
diff --git a/src/main/java/org/spigotmc/ChunkCompressor.java b/src/main/java/org/spigotmc/ChunkCompressor.java diff --git a/src/main/java/org/spigotmc/ChunkCompressor.java b/src/main/java/org/spigotmc/ChunkCompressor.java
new file mode 100644 new file mode 100644
index 0000000..3fd45dc index 0000000..90e03cb
--- /dev/null --- /dev/null
+++ b/src/main/java/org/spigotmc/ChunkCompressor.java +++ b/src/main/java/org/spigotmc/ChunkCompressor.java
@@ -0,0 +1,64 @@ @@ -0,0 +1,62 @@
+package org.spigotmc; +package org.spigotmc;
+ +
+import java.util.Arrays; +import java.util.Arrays;
+import java.util.Iterator; +import java.util.Iterator;
+import java.util.LinkedHashMap; +import java.util.LinkedHashMap;
+import java.util.Map; +import java.util.zip.CRC32;
+import net.minecraft.server.PacketPlayOutMapChunkBulk; +import net.minecraft.server.PacketPlayOutMapChunkBulk;
+import net.minecraft.util.io.netty.channel.ChannelHandler; +import net.minecraft.util.io.netty.channel.ChannelHandler;
+import net.minecraft.util.io.netty.channel.ChannelHandlerContext; +import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
@ -144,7 +139,7 @@ index 0000000..3fd45dc
+public class ChunkCompressor extends ChannelOutboundHandlerAdapter +public class ChunkCompressor extends ChannelOutboundHandlerAdapter
+{ +{
+ +
+ private final LinkedHashMap<Integer, byte[]> cache = new LinkedHashMap<Integer, byte[]>( 16, 0.75f, true ); // Defaults, order by access + private final LinkedHashMap<Long, byte[]> cache = new LinkedHashMap<Long, byte[]>( 16, 0.75f, true ); // Defaults, order by access
+ private volatile int cacheSize; + private volatile int cacheSize;
+ +
+ @Override + @Override
@ -155,15 +150,13 @@ index 0000000..3fd45dc
+ if ( msg instanceof PacketPlayOutMapChunkBulk ) + if ( msg instanceof PacketPlayOutMapChunkBulk )
+ { + {
+ PacketPlayOutMapChunkBulk chunk = (PacketPlayOutMapChunkBulk) msg; + PacketPlayOutMapChunkBulk chunk = (PacketPlayOutMapChunkBulk) msg;
+ // Here we assign a hash to the chunk based on the array of its coordinates: x1, z1, x2, z2, x3, z3 etc etc + // Here we assign a hash to the chunk based on its contents. CRC32 is fast and sufficient for use here.
+ int[] series = new int[ chunk.a.length * 2 ]; + CRC32 crc = new CRC32();
+ int pos = 0; // TODO: Can this be determined mathematically? + for ( byte[] c : chunk.inflatedBuffers )
+ for ( int i = 0; i < chunk.a.length; i++ )
+ { + {
+ series[pos++] = chunk.a[i]; + crc.update( c );
+ series[pos++] = chunk.b[i];
+ } + }
+ int hash = Arrays.hashCode( series ); + long hash = crc.getValue();
+ +
+ byte[] deflated = cache.get( hash ); + byte[] deflated = cache.get( hash );
+ if ( deflated != null ) + if ( deflated != null )