Use unsynchronized buffered OutputStream for history (#2127)

* Use unsychronized buffered OutputStream

* Add note

* Avoid synchronization on DataOutputStream#write
This commit is contained in:
Hannes Greule 2023-03-06 13:54:35 +01:00 committed by GitHub
parent 211e8034ff
commit 8a3052683e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -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);

View File

@ -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);
}