You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2022/05/26 06:14:34 UTC

[iotdb] branch improvedAlign updated: improve plainEncoder

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

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


The following commit(s) were added to refs/heads/improvedAlign by this push:
     new 387fcacfe3 improve plainEncoder
387fcacfe3 is described below

commit 387fcacfe323c6c1239d03c19fe2764021fefe90
Author: JackieTien97 <ja...@gmail.com>
AuthorDate: Thu May 26 14:14:25 2022 +0800

    improve plainEncoder
---
 .../tsfile/encoding/encoder/PlainEncoder.java      | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/encoding/encoder/PlainEncoder.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/encoding/encoder/PlainEncoder.java
index 1a2271836b..4d41214e72 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/encoding/encoder/PlainEncoder.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/encoding/encoder/PlainEncoder.java
@@ -36,13 +36,17 @@ import java.math.BigDecimal;
 public class PlainEncoder extends Encoder {
 
   private static final Logger logger = LoggerFactory.getLogger(PlainEncoder.class);
+  private static final int BUFFER_SIZE = 64;
+
   private TSDataType dataType;
   private int maxStringLength;
+  private final byte[] writeBuffer;
 
   public PlainEncoder(TSDataType dataType, int maxStringLength) {
     super(TSEncoding.PLAIN);
     this.dataType = dataType;
     this.maxStringLength = maxStringLength;
+    this.writeBuffer = new byte[BUFFER_SIZE];
   }
 
   @Override
@@ -67,9 +71,21 @@ public class PlainEncoder extends Encoder {
 
   @Override
   public void encode(long value, ByteArrayOutputStream out) {
-    for (int i = 7; i >= 0; i--) {
-      out.write((byte) (((value) >> (i * 8)) & 0xFF));
-    }
+    final long bits = Double.doubleToLongBits(value);
+    final int first = (int) (bits);
+    final int second = (int) ((bits >>> 32));
+    // Implementation taken from Apache Avro (org.apache.avro.io.BinaryData)
+    // the compiler seems to execute this order the best, likely due to
+    // register allocation -- the lifetime of constants is minimized.
+    writeBuffer[0] = (byte) (first);
+    writeBuffer[4] = (byte) (second);
+    writeBuffer[5] = (byte) (second >>> 8);
+    writeBuffer[1] = (byte) (first >>> 8);
+    writeBuffer[2] = (byte) (first >>> 16);
+    writeBuffer[6] = (byte) (second >>> 16);
+    writeBuffer[7] = (byte) (second >>> 24);
+    writeBuffer[3] = (byte) (first >>> 24);
+    out.write(writeBuffer, 0, 8);
   }
 
   @Override