|
4 | 4 | import java.util.TreeMap; |
5 | 5 | import java.util.concurrent.ThreadLocalRandom; |
6 | 6 | import java.util.function.Supplier; |
7 | | - |
8 | 7 | import org.openjdk.jmh.annotations.Benchmark; |
9 | 8 | import org.openjdk.jmh.annotations.Fork; |
10 | 9 | import org.openjdk.jmh.annotations.Measurement; |
|
13 | 12 | import org.openjdk.jmh.infra.Blackhole; |
14 | 13 |
|
15 | 14 | /** |
16 | | - * <ul>Benchmark to illustrate the trade-offs around case-insensitive Map look-ups - using either... |
17 | | - * <li>(RECOMMENDED) TreeMap with Comparator of String::compareToIgnoreCase |
18 | | - * <li>HashMap with look-ups using String::to<X>Case |
| 15 | + * |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * Benchmark to illustrate the trade-offs around case-insensitive Map look-ups - using either... |
| 19 | + * <li>(RECOMMENDED) TreeMap with Comparator of String::compareToIgnoreCase |
| 20 | + * <li>HashMap with look-ups using String::to<X>Case |
19 | 21 | * </ul> |
20 | | - * |
21 | | - * <p>For case-insensitive lookups, TreeMap map creation is consistently faster because it |
22 | | - * avoids String::to<X>Case calls. |
23 | | - * |
24 | | - * <p>Despite calls to String::to<X>Case, HashMap lookups are faster in single threaded |
| 22 | + * |
| 23 | + * <p>For case-insensitive lookups, TreeMap map creation is consistently faster because it avoids |
| 24 | + * String::to<X>Case calls. |
| 25 | + * |
| 26 | + * <p>Despite calls to String::to<X>Case, HashMap lookups are faster in single threaded |
25 | 27 | * microbenchmark by 50% but are worse when frequently called in a multi-threaded system. |
26 | | - * |
| 28 | + * |
27 | 29 | * <p>With many threads, the extra allocation from calling String::to<X>Case leads to frequent GCs |
28 | | - * which has adverse impacts on the whole system. |
29 | | - * |
30 | | - * <code> |
| 30 | + * which has adverse impacts on the whole system. <code> |
31 | 31 | * MacBook M1 with 1 thread (Java 21) |
32 | | - * |
| 32 | + * |
33 | 33 | * Benchmark Mode Cnt Score Error Units |
34 | 34 | * CaseInsensitiveMapBenchmark.create_hashMap thrpt 6 994213.041 ± 15718.903 ops/s |
35 | 35 | * CaseInsensitiveMapBenchmark.create_treeMap thrpt 6 1522900.015 ± 21646.688 ops/s |
36 | | - * |
| 36 | + * |
37 | 37 | * CaseInsensitiveMapBenchmark.get_hashMap thrpt 6 69149862.293 ± 9168648.566 ops/s |
38 | 38 | * CaseInsensitiveMapBenchmark.get_treeMap thrpt 6 42796699.230 ± 9029447.805 ops/s |
39 | | - * </code> |
40 | | - * |
41 | | - * <code> |
| 39 | + * </code> <code> |
42 | 40 | * MacBook M1 with 8 threads (Java 21) |
43 | | - * |
| 41 | + * |
44 | 42 | * Benchmark Mode Cnt Score Error Units |
45 | 43 | * CaseInsensitiveMapBenchmark.create_hashMap thrpt 6 6641003.483 ± 543210.409 ops/s |
46 | 44 | * CaseInsensitiveMapBenchmark.create_treeMap thrpt 6 10030191.764 ± 1308865.113 ops/s |
47 | | - * |
| 45 | + * |
48 | 46 | * CaseInsensitiveMapBenchmark.get_hashMap thrpt 6 38748031.837 ± 9012072.804 ops/s |
49 | 47 | * CaseInsensitiveMapBenchmark.get_treeMap thrpt 6 173495470.789 ± 27824904.999 ops/s |
50 | 48 | * </code> |
51 | 49 | */ |
52 | 50 | @Fork(2) |
53 | | -@Warmup(iterations=2) |
54 | | -@Measurement(iterations=3) |
| 51 | +@Warmup(iterations = 2) |
| 52 | +@Measurement(iterations = 3) |
55 | 53 | @Threads(8) |
56 | 54 | public class CaseInsensitiveMapBenchmark { |
57 | | - static final String[] PREFIXES = { |
58 | | - "foo", |
59 | | - "bar", |
60 | | - "baz", |
61 | | - "quux" |
62 | | - }; |
63 | | - |
| 55 | + static final String[] PREFIXES = {"foo", "bar", "baz", "quux"}; |
| 56 | + |
64 | 57 | static final int NUM_SUFFIXES = 4; |
65 | | - |
| 58 | + |
66 | 59 | static <T> T init(Supplier<T> supplier) { |
67 | | - return supplier.get(); |
| 60 | + return supplier.get(); |
68 | 61 | } |
69 | 62 |
|
70 | | - static final String[] UPPER_PREFIXES = init(() -> { |
71 | | - String[] upperPrefixes = new String[PREFIXES.length]; |
72 | | - for ( int i = 0; i < PREFIXES.length; ++i ) { |
73 | | - upperPrefixes[i] = PREFIXES[i].toUpperCase(); |
74 | | - } |
75 | | - return upperPrefixes; |
76 | | - }); |
77 | | - |
78 | | - static final String[] LOOKUP_KEYS = init(() -> { |
79 | | - ThreadLocalRandom curRandom = ThreadLocalRandom.current(); |
80 | | - |
81 | | - String[] keys = new String[32]; |
82 | | - for ( int i = 0; i < keys.length; ++i ) { |
83 | | - int prefixIndex = curRandom.nextInt(PREFIXES.length); |
84 | | - boolean toUpper = curRandom.nextBoolean(); |
85 | | - int suffixIndex = curRandom.nextInt(NUM_SUFFIXES + 1); |
86 | | - |
87 | | - String key = PREFIXES[prefixIndex] + "-" + suffixIndex; |
88 | | - keys[i] = toUpper ? key.toUpperCase() : key.toLowerCase(); |
89 | | - } |
90 | | - return keys; |
91 | | - }); |
92 | | - |
| 63 | + static final String[] UPPER_PREFIXES = |
| 64 | + init( |
| 65 | + () -> { |
| 66 | + String[] upperPrefixes = new String[PREFIXES.length]; |
| 67 | + for (int i = 0; i < PREFIXES.length; ++i) { |
| 68 | + upperPrefixes[i] = PREFIXES[i].toUpperCase(); |
| 69 | + } |
| 70 | + return upperPrefixes; |
| 71 | + }); |
| 72 | + |
| 73 | + static final String[] LOOKUP_KEYS = |
| 74 | + init( |
| 75 | + () -> { |
| 76 | + ThreadLocalRandom curRandom = ThreadLocalRandom.current(); |
| 77 | + |
| 78 | + String[] keys = new String[32]; |
| 79 | + for (int i = 0; i < keys.length; ++i) { |
| 80 | + int prefixIndex = curRandom.nextInt(PREFIXES.length); |
| 81 | + boolean toUpper = curRandom.nextBoolean(); |
| 82 | + int suffixIndex = curRandom.nextInt(NUM_SUFFIXES + 1); |
| 83 | + |
| 84 | + String key = PREFIXES[prefixIndex] + "-" + suffixIndex; |
| 85 | + keys[i] = toUpper ? key.toUpperCase() : key.toLowerCase(); |
| 86 | + } |
| 87 | + return keys; |
| 88 | + }); |
| 89 | + |
93 | 90 | static int sharedLookupIndex = 0; |
94 | | - |
| 91 | + |
95 | 92 | static String nextLookupKey() { |
96 | | - int localIndex = ++sharedLookupIndex; |
97 | | - if ( localIndex >= LOOKUP_KEYS.length ) { |
98 | | - sharedLookupIndex = localIndex = 0; |
99 | | - } |
100 | | - return LOOKUP_KEYS[localIndex]; |
| 93 | + int localIndex = ++sharedLookupIndex; |
| 94 | + if (localIndex >= LOOKUP_KEYS.length) { |
| 95 | + sharedLookupIndex = localIndex = 0; |
| 96 | + } |
| 97 | + return LOOKUP_KEYS[localIndex]; |
101 | 98 | } |
102 | | - |
| 99 | + |
103 | 100 | @Benchmark |
104 | 101 | public void create_baseline(Blackhole blackhole) { |
105 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; ++suffix ) { |
106 | | - for ( String prefix: PREFIXES ) { |
107 | | - blackhole.consume(prefix + "-" + suffix); |
108 | | - blackhole.consume(Integer.valueOf(suffix)); |
| 102 | + for (int suffix = 0; suffix < NUM_SUFFIXES; ++suffix) { |
| 103 | + for (String prefix : PREFIXES) { |
| 104 | + blackhole.consume(prefix + "-" + suffix); |
| 105 | + blackhole.consume(Integer.valueOf(suffix)); |
109 | 106 | } |
110 | | - } |
111 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; suffix +=2 ) { |
112 | | - for ( String prefix: UPPER_PREFIXES ) { |
113 | | - blackhole.consume(prefix + "-" + suffix); |
114 | | - blackhole.consume(Integer.valueOf(suffix + 1)); |
| 107 | + } |
| 108 | + for (int suffix = 0; suffix < NUM_SUFFIXES; suffix += 2) { |
| 109 | + for (String prefix : UPPER_PREFIXES) { |
| 110 | + blackhole.consume(prefix + "-" + suffix); |
| 111 | + blackhole.consume(Integer.valueOf(suffix + 1)); |
115 | 112 | } |
116 | | - } |
| 113 | + } |
117 | 114 | } |
118 | | - |
| 115 | + |
119 | 116 | @Benchmark |
120 | 117 | public void lookup_baseline(Blackhole blackhole) { |
121 | | - blackhole.consume(nextLookupKey()); |
| 118 | + blackhole.consume(nextLookupKey()); |
122 | 119 | } |
123 | | - |
| 120 | + |
124 | 121 | @Benchmark |
125 | 122 | public HashMap<String, Integer> create_hashMap() { |
126 | 123 | return _create_hashMap(); |
127 | 124 | } |
128 | | - |
| 125 | + |
129 | 126 | static HashMap<String, Integer> _create_hashMap() { |
130 | | - HashMap<String, Integer> map = new HashMap<>(); |
131 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; ++suffix ) { |
132 | | - for ( String prefix: PREFIXES ) { |
133 | | - map.put((prefix + "-" + suffix).toLowerCase(), suffix); // arguable, but real caller probably doesn't know the case ahead-of-time |
| 127 | + HashMap<String, Integer> map = new HashMap<>(); |
| 128 | + for (int suffix = 0; suffix < NUM_SUFFIXES; ++suffix) { |
| 129 | + for (String prefix : PREFIXES) { |
| 130 | + map.put( |
| 131 | + (prefix + "-" + suffix).toLowerCase(), |
| 132 | + suffix); // arguable, but real caller probably doesn't know the case ahead-of-time |
134 | 133 | } |
135 | | - } |
136 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; suffix +=2 ) { |
137 | | - for ( String prefix: UPPER_PREFIXES ) { |
138 | | - map.put((prefix + "-" + suffix).toLowerCase(), suffix + 1); |
| 134 | + } |
| 135 | + for (int suffix = 0; suffix < NUM_SUFFIXES; suffix += 2) { |
| 136 | + for (String prefix : UPPER_PREFIXES) { |
| 137 | + map.put((prefix + "-" + suffix).toLowerCase(), suffix + 1); |
139 | 138 | } |
140 | | - } |
| 139 | + } |
141 | 140 | return map; |
142 | 141 | } |
143 | | - |
| 142 | + |
144 | 143 | static final HashMap<String, Integer> HASH_MAP = _create_hashMap(); |
145 | 144 |
|
146 | 145 | @Benchmark |
147 | 146 | public Integer lookup_hashMap() { |
148 | | - // This benchmark is still "correct" in multi-threaded context, |
149 | | - // Map is populated under the class initialization lock and not changed thereafter |
150 | | - return HASH_MAP.get(nextLookupKey().toLowerCase()); |
| 147 | + // This benchmark is still "correct" in multi-threaded context, |
| 148 | + // Map is populated under the class initialization lock and not changed thereafter |
| 149 | + return HASH_MAP.get(nextLookupKey().toLowerCase()); |
151 | 150 | } |
152 | | - |
| 151 | + |
153 | 152 | @Benchmark |
154 | 153 | public TreeMap<String, Integer> create_treeMap() { |
155 | | - return _create_treeMap(); |
| 154 | + return _create_treeMap(); |
156 | 155 | } |
157 | | - |
| 156 | + |
158 | 157 | static TreeMap<String, Integer> _create_treeMap() { |
159 | | - TreeMap<String, Integer> map = new TreeMap<>(String::compareToIgnoreCase); |
160 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; ++suffix ) { |
161 | | - for ( String prefix: PREFIXES ) { |
162 | | - map.put(prefix + "-" + suffix, suffix); |
| 158 | + TreeMap<String, Integer> map = new TreeMap<>(String::compareToIgnoreCase); |
| 159 | + for (int suffix = 0; suffix < NUM_SUFFIXES; ++suffix) { |
| 160 | + for (String prefix : PREFIXES) { |
| 161 | + map.put(prefix + "-" + suffix, suffix); |
163 | 162 | } |
164 | | - } |
165 | | - for ( int suffix = 0; suffix < NUM_SUFFIXES; suffix +=2 ) { |
166 | | - for ( String prefix: UPPER_PREFIXES ) { |
167 | | - map.put(prefix + "-" + suffix, suffix + 1); |
| 163 | + } |
| 164 | + for (int suffix = 0; suffix < NUM_SUFFIXES; suffix += 2) { |
| 165 | + for (String prefix : UPPER_PREFIXES) { |
| 166 | + map.put(prefix + "-" + suffix, suffix + 1); |
168 | 167 | } |
169 | | - } |
| 168 | + } |
170 | 169 | return map; |
171 | 170 | } |
172 | | - |
| 171 | + |
173 | 172 | static final TreeMap<String, Integer> TREE_MAP = _create_treeMap(); |
174 | | - |
| 173 | + |
175 | 174 | @Benchmark |
176 | 175 | public Integer lookup_treeMap() { |
177 | | - // This benchmark is still "correct" in multi-threaded context, |
178 | | - // Map is populated under the initial class initialization lock and not changed thereafter |
179 | | - return TREE_MAP.get(nextLookupKey()); |
| 176 | + // This benchmark is still "correct" in multi-threaded context, |
| 177 | + // Map is populated under the initial class initialization lock and not changed thereafter |
| 178 | + return TREE_MAP.get(nextLookupKey()); |
180 | 179 | } |
181 | | - |
| 180 | + |
182 | 181 | // TODO: Add ConcurrentSkipListMap & synchronized HashMap & TreeMap |
183 | 182 | } |
0 commit comments