# std.heap.ArenaAllocator: Get rid of cmpxchg loop in hot path · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/2fa2300ba46f35a9073475bd24a8aa9180e0b228)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 2fa2300ba46f35a9073475bd24a8aa9180e0b228

Requested commit: 2fa2300ba46f35a9073475bd24a8aa9180e0b228

Commit: 2fa2300ba46f35a9073475bd24a8aa9180e0b228

Tree: 7729433688b91aa33e7a800aec27c8285c7f97b4

Author: Justus Klausecker

Committer: Justus Klausecker

## Message

```
std.heap.ArenaAllocator: Get rid of cmpxchg loop in hot path

This is achieved by bumping `end_index` by a large enough amount so that
a suitably aligned region of memory can always be provided. The potential
wasted space this creates is then recovered by a single cmpxchg. This is
always successful for single-threaded arenas which means that this version
still behaves exactly the same as the old single-threaded implementation
when only being accessed by one thread at a time. It can however fail when
another thread bumps `end_index` in the meantime. The observerd failure
rates under extreme load are:

2 Threads: 4-5%
3 Threads: 13-15%
4 Threads: 15-17%
5 Threads: 17-18%
6 Threads: 19-20%
7 Threads: 18-21%

This version offers ~25% faster performance under extreme load from 7 threads,
with diminishing speedups for less threads. The performance for 1 and 2
threads is nearly identical.

```

## Parents

- [56253d9e31c0576f024d95929a8fe26428b35176](https://git.cafe/gitcafe/zig/commit/56253d9e31c0576f024d95929a8fe26428b35176?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/2fa2300ba46f35a9073475bd24a8aa9180e0b228?format=markdown)
