mxsm commented on code in PR #5008:
URL: https://github.com/apache/rocketmq/pull/5008#discussion_r964369381
##########
common/src/main/java/org/apache/rocketmq/common/utils/ConcurrentHashMapUtils.java:
##########
@@ -39,10 +37,11 @@ private ConcurrentHashMapUtil() {
*/
public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key,
Function<? super K, ? extends V> func) {
if (IS_JDK8) {
- V v, newValue;
- return ((v = map.get(key)) == null &&
- (newValue = func.apply(key)) != null &&
- (v = map.putIfAbsent(key, newValue)) == null) ? newValue : v;
+ V v = map.get(key);
+ if (null == v) {
+ v = map.computeIfAbsent(key, func);
+ }
+ return v;
Review Comment:
Hi @caigy @RongtongJin , as JMH test result as following
**JMH test code:**
```
@Warmup(iterations = 2, time = 5)
@Measurement(iterations = 3, time = 5)
@State(Scope.Benchmark)
@Fork(2)
public class ConcurrentHashMapBenchmark {
private static final String KEY = "mxsm";
private final ConcurrentHashMap<String, Object> concurrentMap = new
ConcurrentHashMap<>();
@Setup(Level.Iteration)
public void setup() {
concurrentMap.clear();
}
@Benchmark
@Threads(16)
public Object benchmarkGetBeforeComputeIfAbsent() {
return ConcurrentHashMapUtils.computeIfAbsent(concurrentMap, KEY,
key -> 1);
}
@Benchmark
@Threads(16)
public Object benchmarkComputeIfAbsent() {
return concurrentMap.computeIfAbsent(KEY, key -> 1);
}
}
```
**JDK1.8 JMH result**
```
root@GZYZFA00088591:/mnt/d/develop/github/benchmark/dledger-benchmark# java
-jar target/dledger-benchmarks.jar
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark
# JMH version: 1.35
# VM version: JDK 1.8.0_312, OpenJDK 64-Bit Server VM, 25.312-b07
# VM invoker: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
# VM options: <none>
# Blackhole mode: full + dont-inline hint (auto-detected, use
-Djmh.blackhole.autoDetect=false to disable)
# Warmup: 2 iterations, 5 s each
# Measurement: 3 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark:
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent
# Run progress: 0.00% complete, ETA 00:01:40
# Fork: 1 of 2
# Warmup Iteration 1: 42797877.446 ops/s
# Warmup Iteration 2: 41435241.953 ops/s
Iteration 1: 43157844.990 ops/s
Iteration 2: 43277492.813 ops/s
Iteration 3: 43040444.420 ops/s
# Run progress: 25.00% complete, ETA 00:01:15
# Fork: 2 of 2
# Warmup Iteration 1: 42515980.381 ops/s
# Warmup Iteration 2: 40242904.312 ops/s
Iteration 1: 39257833.798 ops/s
Iteration 2: 41280737.696 ops/s
Iteration 3: 41058950.020 ops/s
Result
"com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent":
41845550.623 ±(99.9%) 4492719.982 ops/s [Average]
(min, avg, max) = (39257833.798, 41845550.623, 43277492.813), stdev =
1602147.224
CI (99.9%): [37352830.640, 46338270.605] (assumes normal distribution)
# JMH version: 1.35
# VM version: JDK 1.8.0_312, OpenJDK 64-Bit Server VM, 25.312-b07
# VM invoker: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
# VM options: <none>
# Blackhole mode: full + dont-inline hint (auto-detected, use
-Djmh.blackhole.autoDetect=false to disable)
# Warmup: 2 iterations, 5 s each
# Measurement: 3 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark:
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent
# Run progress: 50.00% complete, ETA 00:00:50
# Fork: 1 of 2
# Warmup Iteration 1: 887931039.090 ops/s
# Warmup Iteration 2: 905677143.718 ops/s
Iteration 1: 996253036.363 ops/s
Iteration 2: 980599363.488 ops/s
Iteration 3: 929195073.587 ops/s
# Run progress: 75.00% complete, ETA 00:00:25
# Fork: 2 of 2
# Warmup Iteration 1: 810142530.110 ops/s
# Warmup Iteration 2: 872475730.400 ops/s
Iteration 1: 982104949.118 ops/s
Iteration 2: 1017002024.722 ops/s
Iteration 3: 1009650323.584 ops/s
Result
"com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent":
985800795.144 ±(99.9%) 87753464.794 ops/s [Average]
(min, avg, max) = (929195073.587, 985800795.144, 1017002024.722), stdev =
31293730.882
CI (99.9%): [898047330.350, 1073554259.937] (assumes normal distribution)
# Run complete. Total time: 00:01:41
REMEMBER: The numbers below are just data. To gain reusable insights, you
need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof),
design factorial
experiments, perform baseline and negative tests that provide experimental
control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews
from the domain experts.
Do not assume the numbers tell you what you want them to tell.
Benchmark Mode Cnt
Score Error Units
ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent thrpt 6
41845550.623 ± 4492719.982 ops/s
ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent thrpt 6
985800795.144 ± 87753464.794 ops/s
```
**JDK11 JMH result**
```
root@GZYZFA00088591:/mnt/d/develop/github/benchmark/dledger-benchmark# java
-jar target/dledger-benchmarks.jar
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.openjdk.jmh.util.Utils
(file:/mnt/d/develop/github/benchmark/dledger-benchmark/target/dledger-benchmarks.jar)
to method java.io.Console.encoding()
WARNING: Please consider reporting this to the maintainers of
org.openjdk.jmh.util.Utils
WARNING: Use --illegal-access=warn to enable warnings of further illegal
reflective access operations
WARNING: All illegal access operations will be denied in a future release
# JMH version: 1.35
# VM version: JDK 11.0.14.1, OpenJDK 64-Bit Server VM,
11.0.14.1+1-Ubuntu-0ubuntu1.18.04
# VM invoker: /usr/lib/jvm/java-11-openjdk-amd64/bin/java
# VM options: <none>
# Blackhole mode: full + dont-inline hint (auto-detected, use
-Djmh.blackhole.autoDetect=false to disable)
# Warmup: 2 iterations, 5 s each
# Measurement: 3 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark:
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent
# Run progress: 0.00% complete, ETA 00:01:40
# Fork: 1 of 2
# Warmup Iteration 1: 752472650.077 ops/s
# Warmup Iteration 2: 758867890.461 ops/s
Iteration 1: 752371972.156 ops/s
Iteration 2: 728130288.110 ops/s
Iteration 3: 704975310.856 ops/s
# Run progress: 25.00% complete, ETA 00:01:16
# Fork: 2 of 2
# Warmup Iteration 1: 719451190.060 ops/s
# Warmup Iteration 2: 623980466.013 ops/s
Iteration 1: 696479838.494 ops/s
Iteration 2: 684992522.981 ops/s
Iteration 3: 705366498.201 ops/s
Result
"com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent":
712052738.466 ±(99.9%) 68156097.026 ops/s [Average]
(min, avg, max) = (684992522.981, 712052738.466, 752371972.156), stdev =
24305120.753
CI (99.9%): [643896641.441, 780208835.492] (assumes normal distribution)
# JMH version: 1.35
# VM version: JDK 11.0.14.1, OpenJDK 64-Bit Server VM,
11.0.14.1+1-Ubuntu-0ubuntu1.18.04
# VM invoker: /usr/lib/jvm/java-11-openjdk-amd64/bin/java
# VM options: <none>
# Blackhole mode: full + dont-inline hint (auto-detected, use
-Djmh.blackhole.autoDetect=false to disable)
# Warmup: 2 iterations, 5 s each
# Measurement: 3 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark:
com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent
# Run progress: 50.00% complete, ETA 00:00:50
# Fork: 1 of 2
# Warmup Iteration 1: 700639928.397 ops/s
# Warmup Iteration 2: 591844187.371 ops/s
Iteration 1: 702190522.442 ops/s
Iteration 2: 714275280.316 ops/s
Iteration 3: 718153588.415 ops/s
# Run progress: 75.00% complete, ETA 00:00:25
# Fork: 2 of 2
# Warmup Iteration 1: 701145785.263 ops/s
# Warmup Iteration 2: 639685526.697 ops/s
Iteration 1: 703939208.535 ops/s
Iteration 2: 689861279.979 ops/s
Iteration 3: 645455868.007 ops/s
Result
"com.github.mxsm.dledger.benchmark.ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent":
695645957.949 ±(99.9%) 74379825.684 ops/s [Average]
(min, avg, max) = (645455868.007, 695645957.949, 718153588.415), stdev =
26524562.346
CI (99.9%): [621266132.265, 770025783.633] (assumes normal distribution)
# Run complete. Total time: 00:01:41
REMEMBER: The numbers below are just data. To gain reusable insights, you
need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof),
design factorial
experiments, perform baseline and negative tests that provide experimental
control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews
from the domain experts.
Do not assume the numbers tell you what you want them to tell.
Benchmark Mode Cnt
Score Error Units
ConcurrentHashMapBenchmark.benchmarkComputeIfAbsent thrpt 6
712052738.466 ± 68156097.026 ops/s
ConcurrentHashMapBenchmark.benchmarkGetBeforeComputeIfAbsent thrpt 6
695645957.949 ± 74379825.684 ops/s
```
on jdk11, ConcurrentHashMapBenchmark#benchmarkComputeIfAbsent and
ConcurrentHashMapBenchmark#benchmarkGetBeforeComputeIfAbsent run the same code.
--
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]