[WIP][Driver/C] Driver should use weaker incremements#1985
Draft
pveentjer wants to merge 1 commit into
Draft
Conversation
vyazelenko
marked this pull request as draft
April 7, 2026 19:42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the C driver, many counter increments are done using:
aeron_counter_increment
On Linux with GCC this translates to an xadd.
I'm assuming all of the above counter usages, are written by a single thread, so there is no need for an increment that properly handles concurrent writers. lock xaddq is worse than a Java style volatile write (sequential consistent write) because it not only waits for the store buffer to be drained, inside the core there is some serialization of instructions to make sure that only 1 atomic instruction travels through the CPU instruction pipeline.
I added the aeron_counter_increment_opaque:
Although I'm not crazy about this Java name in the C code, it does exactly what is needed and it matches the Java semantics. The addr is volatile so the write will not be optimized out and GCC will ensure that value is properly aligned, so you don't end up with torn reads/writes (so the read and the write is atomic).
A slightly more expensive alternative would be to use the aeron_counter_increment_release:
which you get for free on the X86 since every store is a release store. But you still need to pay the price for this ordering behavior on e.g. ARM even if it isn't needed and this slows down code for no good reason. It think it is best to go for the weakest ordering that still is correct so that we express intent and do not obstruct performance.
Currently there already exists aeron_counter_increment_plain.
But if you want to match Java semantics, the volatile type qualifier should be dropped. I think the method should be dropped because counters are written to shared memory and there could be 1 or more readers, so we should make sure that writes are not optimized out.
Counters that increment by more than 1 make use of the aeron_counter_get_and_add_release which translate to a much cheaper release store
Although they could also be relaxed, they are already cheaper than the aeron_counter_increment
The Java driver has similar problems. Lot of calls to increment() and excessive synchronization.