BewareMyPower commented on issue #23215: URL: https://github.com/apache/pulsar/issues/23215#issuecomment-2308196578
I added some benchmarks with different threads (by modifying the `BenchmarkState.numThreads` field) ## TL; DR There is no reason to use `ConcurrentOpenHashMap` for the sake of performance. ### computeIfAbsent https://gist.github.com/BewareMyPower/1083937e30cb0f5a63be74c4ee9c5559 1. Generate 10000 keys in range [0, 100) in advance 2. Call `computeIfAbsent` in N threads on all keys #### 2 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapTest.testConcurrentOpenHashMap thrpt 5 2875.969 ± 189.894 ops/s ConcurrentHashMapTest.testOfficialConcurrentHashMap thrpt 5 5252.836 ± 1312.977 ops/s ConcurrentHashMapTest.testSynchronizedHashMap thrpt 5 996.854 ± 21.998 ops/s ``` `ConcurrentHashMap` is 1.8x faster than the `ConcurrentOpenHashMap`. #### 4 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapTest.testConcurrentOpenHashMap thrpt 5 1460.855 ± 152.361 ops/s ConcurrentHashMapTest.testOfficialConcurrentHashMap thrpt 5 3750.708 ± 1324.286 ops/s ConcurrentHashMapTest.testSynchronizedHashMap thrpt 5 270.024 ± 11.736 ops/s ``` `ConcurrentHashMap` is 2.5x faster than the `ConcurrentOpenHashMap`. #### 8 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapTest.testConcurrentOpenHashMap thrpt 5 648.256 ± 278.038 ops/s ConcurrentHashMapTest.testOfficialConcurrentHashMap thrpt 5 2383.684 ± 663.103 ops/s ConcurrentHashMapTest.testSynchronizedHashMap thrpt 5 130.503 ± 4.253 ops/s ``` `ConcurrentHashMap` is 3.6x faster than `ConcurrentOpenHashMap`. #### 16 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapTest.testConcurrentOpenHashMap thrpt 5 259.008 ± 204.781 ops/s ConcurrentHashMapTest.testOfficialConcurrentHashMap thrpt 5 1443.469 ± 368.230 ops/s ConcurrentHashMapTest.testSynchronizedHashMap thrpt 5 59.875 ± 2.984 ops/s ``` `ConcurrentHashMap` is 5.5x faster than `ConcurrentOpenHashMap` #### Conclusion `ConcurrentHashMap` performs definitely much better. With the number of threads increasing, the gap becomes much larger. ### get https://gist.github.com/BewareMyPower/b6254ec64932c3cf86b6ec7433a631da 1. Generate 5000 random keys (might be duplicated) in range [0, 10000) and insert them 2. Call `get` on key from 0 to 10000 (inclusive) in N threads #### 2 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapGetTest.testConcurrentOpenHashMap thrpt 5 3741.910 ± 206.337 ops/s ConcurrentHashMapGetTest.testOfficialConcurrentHashMap thrpt 5 5807.285 ± 1896.396 ops/s ConcurrentHashMapGetTest.testSynchronizedHashMap thrpt 5 870.238 ± 66.370 ops/s ``` #### 4 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapGetTest.testConcurrentOpenHashMap thrpt 5 3485.099 ± 269.270 ops/s ConcurrentHashMapGetTest.testOfficialConcurrentHashMap thrpt 5 3777.292 ± 1120.207 ops/s ConcurrentHashMapGetTest.testSynchronizedHashMap thrpt 5 256.690 ± 16.778 ops/s ``` #### 8 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapGetTest.testConcurrentOpenHashMap thrpt 5 2318.738 ± 135.056 ops/s ConcurrentHashMapGetTest.testOfficialConcurrentHashMap thrpt 5 2568.236 ± 828.029 ops/s ConcurrentHashMapGetTest.testSynchronizedHashMap thrpt 5 119.749 ± 2.051 ops/s ``` #### 16 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapGetTest.testConcurrentOpenHashMap thrpt 5 1711.945 ± 128.223 ops/s ConcurrentHashMapGetTest.testOfficialConcurrentHashMap thrpt 5 1459.255 ± 431.960 ops/s ConcurrentHashMapGetTest.testSynchronizedHashMap thrpt 5 59.398 ± 2.085 ops/s ``` #### 32 threads ``` Benchmark Mode Cnt Score Error Units ConcurrentHashMapGetTest.testConcurrentOpenHashMap thrpt 5 863.625 ± 36.957 ops/s ConcurrentHashMapGetTest.testOfficialConcurrentHashMap thrpt 5 1001.633 ± 222.766 ops/s ConcurrentHashMapGetTest.testSynchronizedHashMap thrpt 5 28.798 ± 4.943 ops/s ``` #### Conclusion When the number of threads is small, `ConcurrentHashMap` has better performance though they are similar. It should be noted that when the number of threads is 16, `ConcurrentOpenHashMap` performs better. It might also because the default concurrency level is 16 for `ConcurrentOpenHashMap`. With 32 threads, `ConcurrentHashMap` performs better again. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
