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

upthewaterspout pushed a commit to branch feature/redis-performance-testing
in repository https://gitbox.apache.org/repos/asf/geode.git

commit b947e83fd29f1876a3024db7b016fd4e8e12fcbc
Author: Jens Deppe <[email protected]>
AuthorDate: Thu Mar 4 06:52:11 2021 -0800

    Add fastIntToBytes
---
 .../apache/geode/redis/internal/netty/Coder.java   | 95 +++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)

diff --git 
a/geode-redis/src/main/java/org/apache/geode/redis/internal/netty/Coder.java 
b/geode-redis/src/main/java/org/apache/geode/redis/internal/netty/Coder.java
index e0a0e52..57bc9b6 100644
--- a/geode-redis/src/main/java/org/apache/geode/redis/internal/netty/Coder.java
+++ b/geode-redis/src/main/java/org/apache/geode/redis/internal/netty/Coder.java
@@ -327,7 +327,100 @@ public class Coder {
    */
 
   public static byte[] intToBytes(int i) {
-    return stringToBytes(String.valueOf(i));
+    // return stringToBytes(String.valueOf(i));
+    return fastIntToBytes(i);
+  }
+
+  private static byte[] fastIntToBytes(int i) {
+    if (i == Integer.MIN_VALUE) {
+      return "-2147483648".getBytes();
+    }
+
+    int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
+    byte[] buffer = new byte[size];
+    getChars(i, size, buffer);
+
+    return buffer;
+  }
+
+  static void getChars(int i, int index, byte[] buffer) {
+    int q, r;
+    int charPos = index;
+    byte sign = 0;
+
+    if (i < 0) {
+      sign = '-';
+      i = -i;
+    }
+
+    // Generate two digits per iteration
+    while (i >= 65536) {
+      q = i / 100;
+      // really: r = i - (q * 100);
+      r = i - ((q << 6) + (q << 5) + (q << 2));
+      i = q;
+      buffer[--charPos] = DigitOnes[r];
+      buffer[--charPos] = DigitTens[r];
+    }
+
+    // Fall thru to fast mode for smaller numbers
+    // assert(i <= 65536, i);
+    for (;;) {
+      q = (i * 52429) >>> (16 + 3);
+      r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
+      buffer[--charPos] = digits[r];
+      i = q;
+      if (i == 0)
+        break;
+    }
+    if (sign != 0) {
+      buffer[--charPos] = sign;
+    }
+  }
+
+  static final byte[] digits = {
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
+      0x36, 0x37, 0x38, 0x39, 0x61, 0x62,
+      0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+      0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
+      0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
+      0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
+  };
+
+  static final byte[] DigitOnes = {
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+  };
+
+  static final byte[] DigitTens = {
+      0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+      0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
+      0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
+      0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
+      0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
+      0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35,
+      0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
+      0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
+      0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38,
+      0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
+  };
+
+  static final int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999,
+      99999999, 999999999, Integer.MAX_VALUE};
+
+  // Requires positive x
+  static int stringSize(int x) {
+    for (int i = 0;; i++)
+      if (x <= sizeTable[i])
+        return i + 1;
   }
 
   public static byte[] longToBytes(long l) {

Reply via email to