Skip to content

AtomicLongArray.LazySet — Spurious Full Fence #143

@leecampbell-codeagent

Description

@leecampbell-codeagent

File: Utilities/AtomicLongArray.cs

The LazySet implementation (used in ClearCounts) does a plain write then calls Interlocked.MemoryBarrier() — which emits a full fence, defeating the purpose of a lazy/relaxed write. The comment in the code even acknowledges uncertainty about this. On modern x86 this is largely harmless due to TSO, but on ARM64 (e.g. AWS Graviton, Apple Silicon) a full fence is expensive and wrong for this use case. The correct modern equivalent is:

Volatile.Write(ref _counts[index], value);

Volatile.Write emits a store-release fence (half-fence) rather than a full fence, which is precisely what Java's lazySet maps to on ARM64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions