Optimize Chunk Saving Memory Allocation and Compression

Minecraft ineffeciently uses OutputStreams by calling .write(int) on the stream.
For Chunks, this is a DeflaterOutputStream, which allocates a single byte EVERY write.

This is causing the server to allocate tons of new byte[1] objects.
Additionally, this is very ineffecient for the Deflate process.

By Buffering Writes the same way it already is Buffering Reads, we will
write to the stream much more effeciently.

Also a more effecient RegionFile zero'ing for new chunks to speed up
new chunk generation.
This commit is contained in:
Aikar 2015-09-09 20:48:46 -04:00 committed by Zach Brown
parent 1f41387610
commit c95273f973
5 changed files with 77 additions and 3 deletions

View File

@ -1,4 +1,4 @@
From 0eee1e3cd7129692baef76a063c7a4e62100cf86 Mon Sep 17 00:00:00 2001 From 6e6193c0e175991dcb0e464d39c193c9aa46f27e Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au> From: md_5 <md_5@live.com.au>
Date: Sun, 2 Jun 2013 15:10:56 +1000 Date: Sun, 2 Jun 2013 15:10:56 +1000
Subject: [PATCH] Skeleton API Implementations Subject: [PATCH] Skeleton API Implementations

View File

@ -1,4 +1,4 @@
From 1b3a4837aebc2072d4229c6e2d34938342620063 Mon Sep 17 00:00:00 2001 From af0d91f09d43929fbb2cc2e49892dd1065decb39 Mon Sep 17 00:00:00 2001
From: DerFlash <bte@freenet.de> From: DerFlash <bte@freenet.de>
Date: Sat, 3 Aug 2013 19:53:48 +1000 Date: Sat, 3 Aug 2013 19:53:48 +1000
Subject: [PATCH] Add Getter for Entity Invulnerability Subject: [PATCH] Add Getter for Entity Invulnerability

View File

@ -1,4 +1,4 @@
From c84e8b213d787358de30c456003e4abc570e9a32 Mon Sep 17 00:00:00 2001 From 879e94cd8a3d63afc8defe693f08018b9a19f874 Mon Sep 17 00:00:00 2001
From: Andrew Krieger <Slizyboy@hotmail.com> From: Andrew Krieger <Slizyboy@hotmail.com>
Date: Tue, 24 Dec 2013 07:55:23 -0800 Date: Tue, 24 Dec 2013 07:55:23 -0800
Subject: [PATCH] Cross World Entity Teleportation Subject: [PATCH] Cross World Entity Teleportation

View File

@ -0,0 +1,33 @@
From bec646b094570475d33dac9381e03b8ce6ba4286 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 29 Aug 2015 13:59:25 -0400
Subject: [PATCH] Optimize Chunk Saving Memory Allocation and Compression
Minecraft ineffeciently uses OutputStreams by calling .write(int) on the stream.
For Chunks, this is a DeflaterOutputStream, which allocates a single byte EVERY write.
This is causing the server to allocate tons of new byte[1] objects.
Additionally, this is very ineffecient for the Deflate process.
By Buffering Writes the same way it already is Buffering Reads, we will
write to the stream much more effeciently.
diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java
index 3e31f4b..fe3fbb0 100644
--- a/src/main/java/net/minecraft/server/RegionFile.java
+++ b/src/main/java/net/minecraft/server/RegionFile.java
@@ -176,8 +176,9 @@ public class RegionFile {
}
}
- public DataOutputStream b(int i, int j) {
- return this.d(i, j) ? null : new DataOutputStream(new DeflaterOutputStream(new RegionFile.ChunkBuffer(i, j)));
+ public DataOutputStream b(int i, int j) { // PAIL: getChunkOutputStream
+ // PAIL: isInvalidRegion
+ return this.d(i, j) ? null : new DataOutputStream(new java.io.BufferedOutputStream(new DeflaterOutputStream(new RegionFile.ChunkBuffer(i, j)))); // Spigot - use a BufferedOutputStream to greatly improve file write performance
}
protected synchronized void a(int i, int j, byte[] abyte, int k) {
--
1.9.1

View File

@ -0,0 +1,41 @@
From 0fe6a5f1ec7f039ee04368f5d974286b08ddb53f Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 29 Aug 2015 17:48:20 -0400
Subject: [PATCH] More effecient RegionFile zero'ing
Speeds up new Chunk Generation by using 2 4k block writes vs 2048 4 byte writes
more effecient than going in and out of native calls repeatedly.
diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java
index fe3fbb0..348706f 100644
--- a/src/main/java/net/minecraft/server/RegionFile.java
+++ b/src/main/java/net/minecraft/server/RegionFile.java
@@ -16,7 +16,7 @@ import java.util.zip.InflaterInputStream;
public class RegionFile {
- private static final byte[] a = new byte[4096];
+ private static final byte[] a = new byte[4096]; // Spigot - note: if this ever changes to not be 4096 bytes, update constructor! // PAIL: empty 4k block
private final File b;
private RandomAccessFile c;
private final int[] d = new int[1024];
@@ -38,13 +38,9 @@ public class RegionFile {
int i;
if (this.c.length() < 4096L) {
- for (i = 0; i < 1024; ++i) {
- this.c.writeInt(0);
- }
-
- for (i = 0; i < 1024; ++i) {
- this.c.writeInt(0);
- }
+ // Spigot - more effecient chunk zero'ing
+ this.c.write(RegionFile.a); // Spigot
+ this.c.write(RegionFile.a); // Spigot
this.g += 8192;
}
--
1.9.1