# Adventures with AtomicLong

DevFeed: [Adventures with AtomicLong](<https://devfeed.tech/articles/adventures-with-atomiclong-13615.md>)

Original publisher: [Read original article](<https://mechanical-sympathy.blogspot.com/2011/09/adventures-with-atomiclong.html>)

Author: Martin Thompson (noreply@blogger.com)

Published: 2011-09-11T11:46:00Z

Content type: article

Language: en

Sources: [Mechanical Sympathy](<https://devfeed.tech/sources/mechanical-sympathy.md>)

Topics: [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [Java](<https://devfeed.tech/topics/java.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [intel](<https://devfeed.tech/topics/intel.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [atomic](<https://devfeed.tech/tags/atomic.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cas](<https://devfeed.tech/tags/cas.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [disruptor](<https://devfeed.tech/tags/disruptor.md>), [intel](<https://devfeed.tech/tags/intel.md>), [java](<https://devfeed.tech/tags/java.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article examines AtomicLong-based event sequencing in Java's Disruptor and reports unexpectedly worse performance after removing megamorphic method calls. The investigation attributes the result to increased contention exposing a performance issue with atomic instructions on Intel Sandy Bridge processors, also observed in ArrayBlockingQueue.

## Source excerpt

Sequencing events between threads is a common operation for many multi-threaded algorithms. These sequences could be used for assigning identity to orders, trades, transactions, messages, events, etc. Within the Disruptor we use a monotonic sequence for all events which is implemented as AtomicLong incrementAndGet for the multi-threaded publishing scenario. While working on the latest version of the Disruptor I made some changes which I was convinced would improve performance, however the results surprised me. I had removed some potentially megamorphic method calls and the performance got worse rather than better. After a lot of investigation, I discovered that the megamorphic method calls were hiding a performance issue with the latest Intel Sandybridge processors. With the megamorphic calls out of the way, the contention on the atomic sequence generation increased exposing the issue. I've also observed this performance issue with other Java concurrent structures such as ArrayBlockingQueue. I've been running various benchmarks on Sandybridge and have so far been impressed with performance improvements over Nehalem, especially for memory intensive applications due to the changes in its front-end. However with this sequencing benchmark, I discovered that Sandybridge has taken a major step backward in performance with regard to atomic instructions. Atomic instructions enable read-modify-write actions to be combined into an atomic operation. A good example is incrementing a counter. To complete the increment operation a thread must read the current value, increment it, and then write back the results. In a multi-threaded environment these distinct operations could interleave with other threads doing the same with corrupt results as a consequence. The normal way to avoid this interleaving is to take out a lock for mutual exclusion while performing the steps. Locks are very expensive and often require kernel arbitration between threads. Modern CPUs provide a number of at