Replace ConcurrentServiceBinding by ConcurrentHashMap

Description

While looking through the source code of Hibernate I found the class ConcurrentServiceBinding. My first thought was: cool idea, maybe I can use something similar too. And the second thought was: is it really faster than IdentityHashMap or ConcurrentHashMap. To answer this question I have created small JMH tests. Here are the results from the execution of this tests on Intel Core i7 2 GHz:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # JMH 1.12 (released 78 days ago) # VM version: JDK 1.8.0_92, VM 25.92-b14 # VM options: <none> # Warmup: 10 iterations, 3 s each # Measurement: 10 iterations, 3 s each # Timeout: 10 min per iteration # Threads: 1 thread, will synchronize iterations # Benchmark mode: Average time, time/op Benchmark Mode Cnt Score Error Units ConcurrentServiceBindingBenchmark.baseConcurrentServiceBindingGetEventListenerRegistry avgt 100 11.197 ± 0.534 ns/op ConcurrentServiceBindingBenchmark.baseConcurrentServiceBindingGetJdbcServices avgt 100 8.126 ± 1.197 ns/op ConcurrentServiceBindingBenchmark.chmConcurrentServiceBindingGetEventListenerRegistry avgt 100 6.594 ± 0.023 ns/op ConcurrentServiceBindingBenchmark.chmConcurrentServiceBindingGetJdbcServices avgt 100 6.589 ± 0.025 ns/op ConcurrentServiceBindingBenchmark.ihmConcurrentServiceBindingGetEventListenerRegistry avgt 100 6.206 ± 0.005 ns/op ConcurrentServiceBindingBenchmark.ihmConcurrentServiceBindingGetJdbcServices avgt 100 6.325 ± 0.108 ns/op

Same as above but with 10 threads:

1 2 3 4 5 6 7 8 9 # Threads: 10 threads, will synchronize iterations Benchmark Mode Cnt Score Error Units ConcurrentServiceBindingBenchmark.baseConcurrentServiceBindingGetEventListenerRegistry avgt 100 27.125 ± 0.967 ns/op ConcurrentServiceBindingBenchmark.baseConcurrentServiceBindingGetJdbcServices avgt 100 27.019 ± 2.590 ns/op ConcurrentServiceBindingBenchmark.chmConcurrentServiceBindingGetEventListenerRegistry avgt 100 18.367 ± 0.507 ns/op ConcurrentServiceBindingBenchmark.chmConcurrentServiceBindingGetJdbcServices avgt 100 17.919 ± 0.403 ns/op ConcurrentServiceBindingBenchmark.ihmConcurrentServiceBindingGetEventListenerRegistry avgt 100 17.049 ± 0.315 ns/op ConcurrentServiceBindingBenchmark.ihmConcurrentServiceBindingGetJdbcServices avgt 100 17.871 ± 0.520 ns/op

baseConcurrentServiceBindingXXX uses the current implementation of ConcurrentServiceBinding in Hibernate. chmConcurrentServiceBindingXXX uses ConcurrentHashMap and ihmConcurrentServiceBinding uses IdentityHashMap.

As you can see CHM based implementation is faster than the current implementation of ConcurrentServiceBinding. The IdentityHashMap based implementation is only slightly faster than the CHM based. Therefore I think it is not worth to maintain an extra class when CHM is good enough and suggest to replace ConcurrentServiceBinding by ConcurrentHashMap.

Environment

None

Status

Assignee

Andrej Golovnin

Reporter

Andrej Golovnin

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

backportDecision

None

Components

Affects versions

5.2.1

Priority

Major
Configure