View Javadoc
1   /*
2    * Copyright 2019 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http;
17  
18  import io.netty5.microbench.util.AbstractMicrobenchmark;
19  import io.netty5.util.internal.PlatformDependent;
20  import io.netty5.util.internal.StringUtil;
21  import org.openjdk.jmh.annotations.Benchmark;
22  import org.openjdk.jmh.annotations.CompilerControl;
23  import org.openjdk.jmh.annotations.CompilerControl.Mode;
24  import org.openjdk.jmh.annotations.Measurement;
25  import org.openjdk.jmh.annotations.OutputTimeUnit;
26  import org.openjdk.jmh.annotations.Param;
27  import org.openjdk.jmh.annotations.Scope;
28  import org.openjdk.jmh.annotations.Setup;
29  import org.openjdk.jmh.annotations.State;
30  import org.openjdk.jmh.annotations.Warmup;
31  
32  import java.util.Arrays;
33  import java.util.Random;
34  import java.util.concurrent.TimeUnit;
35  
36  @State(Scope.Benchmark)
37  @Warmup(iterations = 5, time = 1)
38  @Measurement(iterations = 5, time = 1)
39  @OutputTimeUnit(TimeUnit.MICROSECONDS)
40  public class DecodeHexBenchmark extends AbstractMicrobenchmark {
41  
42      @Param({
43              //with HEX chars
44              "135aBa9BBCEA030b947d79fCcaf48Bde",
45              //with HEX chars + 'g'
46              "4DDeA5gDD1C6fE567E1b6gf0C40FEcDg",
47      })
48      private String hex;
49      // Needs to specify a high number of inputs to allow the current strategy
50      // on nextHexDigits to produce enough branch-misses
51      @Param({ "2048" })
52      private int inputs;
53      private char[][] hexDigits;
54      private static final long SEED = 1578675524L;
55      private long next;
56  
57      @Setup
58      public void init() {
59          final char[] hexCh = hex.toCharArray();
60          next = 0;
61          inputs = PlatformDependent.roundToPowerOfTwo(inputs);
62          hexDigits = new char[inputs][];
63          hexDigits[0] = hexCh;
64          if (inputs > 1) {
65              final Random rnd = new Random(SEED);
66              for (int i = 1; i < inputs; i++) {
67                  hexDigits[i] = shuffle(Arrays.copyOf(hexCh, hexCh.length), rnd);
68              }
69          }
70      }
71  
72      // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
73      private static char[] shuffle(char[] chars, Random rnd) {
74          int index;
75          char tmp;
76          for (int i = chars.length - 1; i > 0; i--) {
77              index = rnd.nextInt(i + 1);
78              tmp = chars[index];
79              chars[index] = chars[i];
80              chars[i] = tmp;
81          }
82          return chars;
83      }
84  
85      private int nextHexDigits() {
86          final int idx = (int) (next & (inputs - 1));
87          next++;
88          return idx;
89      }
90  
91      @Benchmark
92      @CompilerControl(Mode.DONT_INLINE)
93      public long hexDigits() {
94          long v = 0;
95          final char[] hexDigits = this.hexDigits[nextHexDigits()];
96          for (int i = 0, size = hexDigits.length; i < size; i++) {
97              v += StringUtil.decodeHexNibble(hexDigits[i]);
98          }
99          return v;
100     }
101 
102     @Benchmark
103     @CompilerControl(Mode.DONT_INLINE)
104     public long hexDigitsWithChecks() {
105         long v = 0;
106         final char[] hexDigits = this.hexDigits[nextHexDigits()];
107         for (int i = 0, size = hexDigits.length; i < size; i++) {
108             v += decodeHexNibbleWithCheck(hexDigits[i]);
109         }
110         return v;
111     }
112 
113     @Benchmark
114     @CompilerControl(Mode.DONT_INLINE)
115     public long hexDigitsOriginal() {
116         long v = 0;
117         final char[] hexDigits = this.hexDigits[nextHexDigits()];
118         for (int i = 0, size = hexDigits.length; i < size; i++) {
119             v += decodeHexNibble(hexDigits[i]);
120         }
121         return v;
122     }
123 
124     private static int decodeHexNibble(final char c) {
125         if (c >= '0' && c <= '9') {
126             return c - '0';
127         }
128         if (c >= 'A' && c <= 'F') {
129             return c - ('A' - 0xA);
130         }
131         if (c >= 'a' && c <= 'f') {
132             return c - ('a' - 0xA);
133         }
134         return -1;
135     }
136 
137     private static final byte[] HEX2B;
138 
139     static {
140         HEX2B = new byte['f' + 1];
141         Arrays.fill(HEX2B, (byte) -1);
142         HEX2B['0'] = (byte) 0;
143         HEX2B['1'] = (byte) 1;
144         HEX2B['2'] = (byte) 2;
145         HEX2B['3'] = (byte) 3;
146         HEX2B['4'] = (byte) 4;
147         HEX2B['5'] = (byte) 5;
148         HEX2B['6'] = (byte) 6;
149         HEX2B['7'] = (byte) 7;
150         HEX2B['8'] = (byte) 8;
151         HEX2B['9'] = (byte) 9;
152         HEX2B['A'] = (byte) 10;
153         HEX2B['B'] = (byte) 11;
154         HEX2B['C'] = (byte) 12;
155         HEX2B['D'] = (byte) 13;
156         HEX2B['E'] = (byte) 14;
157         HEX2B['F'] = (byte) 15;
158         HEX2B['a'] = (byte) 10;
159         HEX2B['b'] = (byte) 11;
160         HEX2B['c'] = (byte) 12;
161         HEX2B['d'] = (byte) 13;
162         HEX2B['e'] = (byte) 14;
163         HEX2B['f'] = (byte) 15;
164     }
165 
166     private static int decodeHexNibbleWithCheck(final char c) {
167         if ((int) c >= HEX2B.length) {
168             return -1;
169         }
170         if (PlatformDependent.hasUnsafe()) {
171             return PlatformDependent.getByte(HEX2B, c);
172         }
173         return HEX2B[c];
174     }
175 
176 }