This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/tsfile.git
The following commit(s) were added to refs/heads/develop by this push:
new 4ab90f6e Remove synchronized in LocalTsFileOutput and PublicBAOS
4ab90f6e is described below
commit 4ab90f6e9df35f931e6bbad11ee862989d651ab0
Author: shuwenwei <[email protected]>
AuthorDate: Fri Apr 19 18:51:53 2024 +0800
Remove synchronized in LocalTsFileOutput and PublicBAOS
---
.../java/org/apache/tsfile/utils/PublicBAOS.java | 67 +++++++++++++++++++++-
.../tsfile/write/writer/LocalTsFileOutput.java | 10 ++--
2 files changed, 71 insertions(+), 6 deletions(-)
diff --git a/tsfile/src/main/java/org/apache/tsfile/utils/PublicBAOS.java
b/tsfile/src/main/java/org/apache/tsfile/utils/PublicBAOS.java
index 0c0aa6a1..fc3928b5 100644
--- a/tsfile/src/main/java/org/apache/tsfile/utils/PublicBAOS.java
+++ b/tsfile/src/main/java/org/apache/tsfile/utils/PublicBAOS.java
@@ -22,13 +22,18 @@ package org.apache.tsfile.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
/**
* A subclass extending <code>ByteArrayOutputStream</code>. It's used to
return the byte array
* directly. Note that the size of byte array is large than actual size of
valid contents, thus it's
- * used cooperating with <code>size()</code> or <code>capacity = size</code>
+ * used cooperating with <code>size()</code> or <code>capacity = size</code>
This class extends
+ * ByteArrayOutputStream and intentionally remove the 'synchronized' keyword
in write methods for
+ * better performance. (Not thread safe)
*/
public class PublicBAOS extends ByteArrayOutputStream {
+ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public PublicBAOS() {
super();
@@ -38,6 +43,66 @@ public class PublicBAOS extends ByteArrayOutputStream {
super(size);
}
+ private void ensureCapacity(int minCapacity) {
+ // overflow-conscious code
+ if (minCapacity - buf.length > 0) {
+ grow(minCapacity);
+ }
+ }
+
+ private void grow(int minCapacity) {
+ // overflow-conscious code
+ int oldCapacity = buf.length;
+ int newCapacity = oldCapacity << 1;
+ if (newCapacity - minCapacity < 0) {
+ newCapacity = minCapacity;
+ }
+ if (newCapacity - MAX_ARRAY_SIZE > 0) {
+ newCapacity = hugeCapacity(minCapacity);
+ }
+ buf = Arrays.copyOf(buf, newCapacity);
+ }
+
+ private static int hugeCapacity(int minCapacity) {
+ if (minCapacity < 0) {
+ // overflow
+ throw new OutOfMemoryError();
+ }
+ return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
+ }
+
+ @Override
+ public void write(int b) {
+ ensureCapacity(count + 1);
+ buf[count] = (byte) b;
+ count += 1;
+ }
+
+ @Override
+ public void write(byte b[], int off, int len) {
+ if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) - b.length
> 0)) {
+ throw new IndexOutOfBoundsException();
+ }
+ ensureCapacity(count + len);
+ System.arraycopy(b, off, buf, count, len);
+ count += len;
+ }
+
+ @Override
+ public byte[] toByteArray() {
+ return Arrays.copyOf(buf, count);
+ }
+
+ @Override
+ public String toString() {
+ return new String(buf, 0, count);
+ }
+
+ @Override
+ public String toString(String charsetName) throws
UnsupportedEncodingException {
+ return new String(buf, 0, count, charsetName);
+ }
+
/**
* get current all bytes data
*
diff --git
a/tsfile/src/main/java/org/apache/tsfile/write/writer/LocalTsFileOutput.java
b/tsfile/src/main/java/org/apache/tsfile/write/writer/LocalTsFileOutput.java
index 256d7ee8..9b7a18a0 100644
--- a/tsfile/src/main/java/org/apache/tsfile/write/writer/LocalTsFileOutput.java
+++ b/tsfile/src/main/java/org/apache/tsfile/write/writer/LocalTsFileOutput.java
@@ -41,31 +41,31 @@ public class LocalTsFileOutput extends OutputStream
implements TsFileOutput {
}
@Override
- public synchronized void write(int b) throws IOException {
+ public void write(int b) throws IOException {
bufferedStream.write(b);
position++;
}
@Override
- public synchronized void write(byte[] b) throws IOException {
+ public void write(byte[] b) throws IOException {
bufferedStream.write(b);
position += b.length;
}
@Override
- public synchronized void write(byte b) throws IOException {
+ public void write(byte b) throws IOException {
bufferedStream.write(b);
position++;
}
@Override
- public synchronized void write(byte[] buf, int start, int offset) throws
IOException {
+ public void write(byte[] buf, int start, int offset) throws IOException {
bufferedStream.write(buf, start, offset);
position += offset;
}
@Override
- public synchronized void write(ByteBuffer b) throws IOException {
+ public void write(ByteBuffer b) throws IOException {
bufferedStream.write(b.array());
position += b.array().length;
}