This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new b3689c45a9a Reduce synchronized cost in compaction (#12279)
b3689c45a9a is described below

commit b3689c45a9a0671d4185de701e5b1803ca9d9c71
Author: shuwenwei <[email protected]>
AuthorDate: Fri Apr 19 17:27:25 2024 +0800

    Reduce synchronized cost in compaction (#12279)
    
    * add UnsynchronizedPublicBAOS
    
    * remove synchronized in LocalTsFileOutput
    
    * remove UnsynchronizedPublicBAOS
    
    * modify code style
---
 .../org/apache/iotdb/tsfile/utils/PublicBAOS.java  | 67 +++++++++++++++++++++-
 .../tsfile/write/writer/LocalTsFileOutput.java     | 10 ++--
 2 files changed, 71 insertions(+), 6 deletions(-)

diff --git 
a/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/PublicBAOS.java 
b/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/PublicBAOS.java
index 18a459a6ee0..62834a684ed 100644
--- 
a/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/PublicBAOS.java
+++ 
b/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/PublicBAOS.java
@@ -22,13 +22,18 @@ package org.apache.iotdb.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/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
 
b/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
index af0b19bb9aa..d64d7964afd 100644
--- 
a/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
+++ 
b/iotdb-core/tsfile/src/main/java/org/apache/iotdb/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;
   }

Reply via email to