cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xe...@apache.org
Subject git commit: Minimize byte array allocation by AbstractData{Input,Output} patch by Pavel Yaskevich; reviewed by Jonathan Ellis for CASSANDRA-5090
Date Wed, 26 Dec 2012 03:55:54 GMT
Updated Branches:
  refs/heads/cassandra-1.2 f4700edb4 -> 38757c4b2


Minimize byte array allocation by AbstractData{Input,Output}
patch by Pavel Yaskevich; reviewed by Jonathan Ellis for CASSANDRA-5090


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/38757c4b
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/38757c4b
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/38757c4b

Branch: refs/heads/cassandra-1.2
Commit: 38757c4b2782bbfc837faee00199d373b645cb76
Parents: f4700ed
Author: Pavel Yaskevich <xedin@apache.org>
Authored: Tue Dec 25 19:23:38 2012 -0800
Committer: Pavel Yaskevich <xedin@apache.org>
Committed: Tue Dec 25 19:54:19 2012 -0800

----------------------------------------------------------------------
 CHANGES.txt                                        |    1 +
 .../cassandra/io/util/AbstractDataInput.java       |   47 ++++++---------
 .../cassandra/io/util/AbstractDataOutput.java      |   35 ++++-------
 3 files changed, 34 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/38757c4b/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 401a9af..404cdbe 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -11,6 +11,7 @@
    method (CASSANDRA-4939)
  * Expose black-listed directories via JMX (CASSANDRA-4848)
  * Log compaction merge counts (CASSANDRA-4894)
+ * Minimize byte array allocation by AbstractData{Input,Output} (CASSANDRA-5090)
 
 
 1.2.0

http://git-wip-us.apache.org/repos/asf/cassandra/blob/38757c4b/src/java/org/apache/cassandra/io/util/AbstractDataInput.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/AbstractDataInput.java b/src/java/org/apache/cassandra/io/util/AbstractDataInput.java
index b0766a4..ff8b6b2 100644
--- a/src/java/org/apache/cassandra/io/util/AbstractDataInput.java
+++ b/src/java/org/apache/cassandra/io/util/AbstractDataInput.java
@@ -78,11 +78,11 @@ public abstract class AbstractDataInput extends InputStream implements
DataInput
      *             if this file is closed or another I/O error occurs.
      */
     public final char readChar() throws IOException {
-        byte[] buffer = new byte[2];
-        if (read(buffer, 0, buffer.length) != buffer.length) {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        if ((ch1 | ch2) < 0)
             throw new EOFException();
-        }
-        return (char) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff));
+        return (char)((ch1 << 8) + (ch2 << 0));
     }
 
     /**
@@ -188,12 +188,13 @@ public abstract class AbstractDataInput extends InputStream implements
DataInput
      *             if this file is closed or another I/O error occurs.
      */
     public int readInt() throws IOException {
-        byte[] buffer = new byte[4];
-        if (read(buffer, 0, buffer.length) != buffer.length) {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        int ch3 = this.read();
+        int ch4 = this.read();
+        if ((ch1 | ch2 | ch3 | ch4) < 0)
             throw new EOFException();
-        }
-        return ((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16)
-                + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff);
+        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 <<
0));
     }
 
     /**
@@ -252,17 +253,7 @@ public abstract class AbstractDataInput extends InputStream implements
DataInput
      *             if this file is closed or another I/O error occurs.
      */
     public long readLong() throws IOException {
-        byte[] buffer = new byte[8];
-        int n = read(buffer, 0, buffer.length);
-        if (n != buffer.length) {
-            throw new EOFException("expected 8 bytes; read " + n + " at final position "
+ getPosition());
-        }
-        return ((long) (((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) <<
16)
-                + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff)) <<
32)
-                + ((long) (buffer[4] & 0xff) << 24)
-                + ((buffer[5] & 0xff) << 16)
-                + ((buffer[6] & 0xff) << 8)
-                + (buffer[7] & 0xff);
+        return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
     }
 
     /**
@@ -277,11 +268,11 @@ public abstract class AbstractDataInput extends InputStream implements
DataInput
      *             if this file is closed or another I/O error occurs.
      */
     public short readShort() throws IOException {
-        byte[] buffer = new byte[2];
-        if (read(buffer, 0, buffer.length) != buffer.length) {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        if ((ch1 | ch2) < 0)
             throw new EOFException();
-        }
-        return (short) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff));
+        return (short)((ch1 << 8) + (ch2 << 0));
     }
 
     /**
@@ -315,11 +306,11 @@ public abstract class AbstractDataInput extends InputStream implements
DataInput
      *             if this file is closed or another I/O error occurs.
      */
     public int readUnsignedShort() throws IOException {
-        byte[] buffer = new byte[2];
-        if (read(buffer, 0, buffer.length) != buffer.length) {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        if ((ch1 | ch2) < 0)
             throw new EOFException();
-        }
-        return ((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff);
+        return (ch1 << 8) + (ch2 << 0);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/cassandra/blob/38757c4b/src/java/org/apache/cassandra/io/util/AbstractDataOutput.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/AbstractDataOutput.java b/src/java/org/apache/cassandra/io/util/AbstractDataOutput.java
index be26094..149d82a 100644
--- a/src/java/org/apache/cassandra/io/util/AbstractDataOutput.java
+++ b/src/java/org/apache/cassandra/io/util/AbstractDataOutput.java
@@ -135,10 +135,8 @@ public abstract class AbstractDataOutput extends OutputStream implements
DataOut
      *             DataOutputStream.
      */
     public final void writeChar(int val) throws IOException {
-        byte[] buffer = new byte[2];
-        buffer[0] = (byte) (val >> 8);
-        buffer[1] = (byte) val;
-        write(buffer, 0, buffer.length);
+        write((val >>> 8) & 0xFF);
+        write((val >>> 0) & 0xFF);
     }
 
     /**
@@ -206,12 +204,10 @@ public abstract class AbstractDataOutput extends OutputStream implements
DataOut
      *             DataOutputStream.
      */
     public void writeInt(int val) throws IOException {
-        byte[] buffer = new byte[4];
-        buffer[0] = (byte) (val >> 24);
-        buffer[1] = (byte) (val >> 16);
-        buffer[2] = (byte) (val >> 8);
-        buffer[3] = (byte) val;
-        write(buffer, 0, buffer.length);
+        write((val >>> 24) & 0xFF);
+        write((val >>> 16) & 0xFF);
+        write((val >>>  8) & 0xFF);
+        write((val >>>  0) & 0xFF);
     }
 
     /**
@@ -226,17 +222,14 @@ public abstract class AbstractDataOutput extends OutputStream implements
DataOut
      *             DataOutputStream.
      */
     public void writeLong(long val) throws IOException {
-        byte[] buffer = new byte[8];
-        int t = (int) (val >> 32);
-        buffer[0] = (byte) (t >> 24);
-        buffer[1] = (byte) (t >> 16);
-        buffer[2] = (byte) (t >> 8);
-        buffer[3] = (byte) t;
-        buffer[4] = (byte) (val >> 24);
-        buffer[5] = (byte) (val >> 16);
-        buffer[6] = (byte) (val >> 8);
-        buffer[7] = (byte) val;
-        write(buffer, 0, buffer.length);
+        write((int)(val >>> 56) & 0xFF);
+        write((int)(val >>> 48) & 0xFF);
+        write((int)(val >>> 40) & 0xFF);
+        write((int)(val >>> 32) & 0xFF);
+        write((int)(val >>> 24) & 0xFF);
+        write((int)(val >>> 16) & 0xFF);
+        write((int)(val >>>  8) & 0xFF);
+        write((int)(val >>>  0) & 0xFF);
     }
 
     /**


Mime
View raw message