From 8a3052683e9e92b763746eea1f654416973d438d Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 6 Mar 2023 13:54:35 +0100 Subject: [PATCH] Use unsynchronized buffered OutputStream for history (#2127) * Use unsychronized buffered OutputStream * Add note * Avoid synchronization on DataOutputStream#write --- .../core/internal/io/FaweOutputStream.java | 7 +++++++ .../java/com/fastasyncworldedit/core/util/MainUtil.java | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/internal/io/FaweOutputStream.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/internal/io/FaweOutputStream.java index 9bee3afe9..130c63952 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/internal/io/FaweOutputStream.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/internal/io/FaweOutputStream.java @@ -20,6 +20,13 @@ public class FaweOutputStream extends DataOutputStream { return parent; } + // overwritten to un-synchronized + @Override + public void write(final int b) throws IOException { + out.write(b); + written++; + } + public void write(int b, int amount) throws IOException { for (int i = 0; i < amount; i++) { write(b); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MainUtil.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MainUtil.java index 86370747d..8deb59d13 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MainUtil.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MainUtil.java @@ -27,6 +27,7 @@ import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.formatting.text.Component; import it.unimi.dsi.fastutil.io.FastBufferedInputStream; +import it.unimi.dsi.fastutil.io.FastBufferedOutputStream; import net.jpountz.lz4.LZ4BlockInputStream; import net.jpountz.lz4.LZ4BlockOutputStream; import net.jpountz.lz4.LZ4Compressor; @@ -40,7 +41,6 @@ import javax.imageio.ImageIO; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -273,9 +273,12 @@ public class MainUtil { return buffer; } + /** + * Note: The returned stream is not thread safe. + */ public static FaweOutputStream getCompressedOS(OutputStream os, int amount, int buffer) throws IOException { os.write((byte) 10 + amount); - os = new BufferedOutputStream(os, buffer); + os = new FastBufferedOutputStream(os, buffer); if (amount == 0) { return new FaweOutputStream(os); } @@ -296,7 +299,7 @@ public class MainUtil { os = new LZ4BlockOutputStream(os, buffer, factory.highCompressor()); } } - os = new BufferedOutputStream(os, buffer); + os = new FastBufferedOutputStream(os, buffer); return new FaweOutputStream(os); }