<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/146141>146141</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc++] Look into merging the atomics in __libcpp_contention_table_entry
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
huixie90
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ldionne
</td>
</tr>
</table>
<pre>
While discussing with @huixie90 and Apple folks about https://github.com/llvm/llvm-project/issues/109290, it was noticed that `__libcpp_contention_table_entry` defined [here](https://github.com/llvm/llvm-project/blob/52040b44f6060540a0b9d56fdd2e0eb5a540e84c/libcxx/src/atomic.cpp#L121C54-L121C85) contained two atomics when in reality we could merge both into a single atomic:
```
struct alignas(64) /* aim to avoid false sharing */ __libcpp_contention_table_entry {
__cxx_atomic_contention_t __contention_state;
__cxx_atomic_contention_t __platform_state;
inline constexpr __libcpp_contention_table_entry() : __contention_state(0), __platform_state(0) {}
};
```
On Linux, `__cxx_atomic_contention_t` is 32 bits, so we could merge both into a single 64 bits value and perform atomic operations on that directly.
On Apple arm64, `__cxx_atomic_contention_t` is 64 bits, so we could merge both into a single 128 bits value and perform 128-bit atomic operations on that. On Apple x86_64, I don't think we can use this trick (?).
This can potentially simplify the implementation of the contention table algorithm and might be faster.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyklFuL6zYQxz-N8jKcIMuX2A9-yJ4QKCz0pdDHINvjeLqyZKTxJvn2RUr27Dm0y24pCHzRjPT7z02HQGeL2IrySSg1rXQlbKRQSpSHjV55cr41AzlrcdO54db-OZFBGCj0awhkz3AhnkAU8s0XtB1gvywGYXTmJYDu3MowMS9B5HuhjkIdz8TT2m17Nwt1NOb17fFt8e4v7FmoI4WwYhDqmMlGRaTvQAwXHcA6ph4H4EkziEqeToa6fllOvbOMlsnZE-vO4Akt-5uoJAw4ksUBRPk0oUdRHoSq_zNSZ1wn1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