Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8141640

CompareAndExchange idioms may be slower than CompareAndSet

XMLWordPrintable

      If we compare two idiomatic constructions for CompareAndSet vs CompareAndExchange, then CAE would perform worse than CAS. Looking at -prof perfasm, it seems to happen because CAE loop omits reading the hot line back, *but* it also widens the window where the actual value may change. If we put the Unsafe.getIntVolatile in CAE loop, then a "fresher" value is available for mutation, and performance is similar to CAS. This has implications for performance testing, and maybe the codegen.

      Benchmark:
        http://cr.openjdk.java.net/~shade/8141640/CAEvsCAS.java

      CAEvsCAS.exchange: 986.721 ± 11.816 ns/op
      CAEvsCAS.exchange_withReread: 850.282 ± 11.068 ns/op
      CAEvsCAS.swap: 854.341 ± 18.451 ns/op

      Hot loops:
        http://cr.openjdk.java.net/~shade/8141640/perf.txt

            shade Aleksey Shipilev
            shade Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: