commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [1/2] commons-compress git commit: ByteBuffer looks convenient but is slow (manual method is 50% faster)
Date Sat, 25 Feb 2017 15:41:30 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master 7b5816d96 -> 1cdc1622c


ByteBuffer looks convenient but is slow (manual method is 50% faster)


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/2f56fb5c
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/2f56fb5c
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/2f56fb5c

Branch: refs/heads/master
Commit: 2f56fb5c10e6b8c8e01033ee1c50669028f28340
Parents: 7b5816d
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sat Feb 25 16:27:55 2017 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sat Feb 25 16:27:55 2017 +0100

----------------------------------------------------------------------
 .../compress/compressors/lz4/XXHash32.java      | 37 +++++++++-----------
 1 file changed, 17 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2f56fb5c/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java b/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java
index b3acd82..1d4c945 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java
@@ -20,10 +20,10 @@ package org.apache.commons.compress.compressors.lz4;
 
 import static java.lang.Integer.rotateLeft;
 
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
 import java.util.zip.Checksum;
 
+import static org.apache.commons.compress.utils.ByteUtils.fromLittleEndian;
+
 /**
  * Implementation of the xxhash32 hash algorithm.
  *
@@ -44,7 +44,7 @@ public class XXHash32 implements Checksum {
 
     private final byte[] oneByte = new byte[1];
     private final int[] state = new int[4];
-    private final ByteBuffer buffer = ByteBuffer.allocate(BUF_SIZE).order(ByteOrder.LITTLE_ENDIAN);
+    private final byte[] buffer = new byte[BUF_SIZE];
     private final int seed;
 
     private int totalLen;
@@ -69,7 +69,6 @@ public class XXHash32 implements Checksum {
     @Override
     public void reset() {
         initializeState();
-        buffer.clear();
         totalLen = 0;
         pos = 0;
     }
@@ -90,28 +89,28 @@ public class XXHash32 implements Checksum {
         final int end = off + len;
 
         if (pos + len < BUF_SIZE) {
-            buffer.put(b, off, len);
+            System.arraycopy(b, off, buffer, pos, len);
             pos += len;
             return;
         }
 
         if (pos > 0) {
             final int size = BUF_SIZE - pos;
-            buffer.put(b, off, size);
+            System.arraycopy(b, off, buffer, pos, size);
             process();
             off += size;
         }
 
         final int limit = end - BUF_SIZE;
         while (off <= limit) {
-            buffer.put(b, off, BUF_SIZE);
+            System.arraycopy(b, off, buffer, 0, BUF_SIZE);
             process();
             off += BUF_SIZE;
         }
 
         if (off < end) {
             pos = end - off;
-            buffer.put(b, off, pos);
+            System.arraycopy(b, off, buffer, 0, pos);
         }
     }
 
@@ -129,16 +128,13 @@ public class XXHash32 implements Checksum {
         }
         hash += totalLen;
 
-        buffer.flip();
-
         int idx = 0;
         final int limit = pos - 4;
         for (; idx <= limit; idx += 4) {
-            hash = rotateLeft(hash + buffer.getInt() * PRIME3, 17) * PRIME4;
+            hash = rotateLeft(hash + getInt(idx) * PRIME3, 17) * PRIME4;
         }
         while (idx < pos) {
-            hash = rotateLeft(hash + (buffer.get() & 0xff) * PRIME5, 11) * PRIME1;
-            idx++;
+            hash = rotateLeft(hash + (buffer[idx++] & 0xff) * PRIME5, 11) * PRIME1;
         }
 
         hash ^= hash >>> 15;
@@ -149,6 +145,10 @@ public class XXHash32 implements Checksum {
         return hash & 0xffffffffl;
     }
 
+    private int getInt(int idx) {
+        return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffl);
+    }
+
     private void initializeState() {
         state[0] = seed + PRIME1 + PRIME2;
         state[1] = seed + PRIME2;
@@ -157,25 +157,22 @@ public class XXHash32 implements Checksum {
     }
 
     private void process() {
-        buffer.flip();
-
         // local shadows for performance
         int s0 = state[0];
         int s1 = state[1];
         int s2 = state[2];
         int s3 = state[3];
 
-        s0 = rotateLeft(s0 + buffer.getInt() * PRIME2, ROTATE_BITS) * PRIME1;
-        s1 = rotateLeft(s1 + buffer.getInt() * PRIME2, ROTATE_BITS) * PRIME1;
-        s2 = rotateLeft(s2 + buffer.getInt() * PRIME2, ROTATE_BITS) * PRIME1;
-        s3 = rotateLeft(s3 + buffer.getInt() * PRIME2, ROTATE_BITS) * PRIME1;
+        s0 = rotateLeft(s0 + getInt(0) * PRIME2, ROTATE_BITS) * PRIME1;
+        s1 = rotateLeft(s1 + getInt(4) * PRIME2, ROTATE_BITS) * PRIME1;
+        s2 = rotateLeft(s2 + getInt(8) * PRIME2, ROTATE_BITS) * PRIME1;
+        s3 = rotateLeft(s3 + getInt(12) * PRIME2, ROTATE_BITS) * PRIME1;
 
         state[0] = s0;
         state[1] = s1;
         state[2] = s2;
         state[3] = s3;
 
-        buffer.clear();
         pos = 0;
     }
 }


Mime
View raw message