Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
chunked-vector.hh
Go to the documentation of this file.
1#pragma once
3
4#include <cstdint>
5#include <cstdlib>
6#include <vector>
7#include <limits>
8#include <atomic>
9#include <ranges>
10
11#include "nix/util/error.hh"
12
13namespace nix {
14
27template<typename T, size_t ChunkSize, size_t MaxChunks>
28class ChunkedVector
29{
30 static_assert(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__, "ChunkedVector does not support over-aligned types");
31
32private:
33 /* Put on a separate cache line to avoid false sharing. */
34 alignas(std::hardware_destructive_interference_size) std::atomic<uint32_t> size_ = 0;
35
36 using ChunksArray = std::array<std::atomic<T *>, MaxChunks>;
37
38 std::unique_ptr<ChunksArray> chunks = std::make_unique<ChunksArray>();
39
40 [[gnu::noinline]]
41 T * allocChunk()
42 {
43 return static_cast<T *>(::operator new(ChunkSize * sizeof(T)));
44 }
45
49 [[gnu::noinline]]
50 T * ensureChunk(size_t chunkIdx)
51 {
52 if (chunkIdx >= MaxChunks)
53 unreachable();
54
55 /* Want synchronises-with relation with the CAS. */
56 auto * p = (*chunks)[chunkIdx].load(std::memory_order_acquire);
57 if (p) [[likely]]
58 return p;
59
60 auto * newChunk = allocChunk();
61 T * expected = nullptr;
62 /* Try to allocate the chunk ourselves. If CAS fails then somebody beat
63 us to it. Release semantics for the winner and acquire semantics for
64 the loser. */
65 if ((*chunks)[chunkIdx].compare_exchange_strong(
66 expected, newChunk, /*success=*/std::memory_order_release, /*failure=*/std::memory_order_acquire))
67 return newChunk; /* We succeeded. */
68
69 /* Somebody beat us to it. */
70 ::operator delete(newChunk);
71 assert(expected);
72 return expected;
73 }
74
75 std::size_t numActiveChunks() const noexcept
76 {
77 return (size_.load(std::memory_order_relaxed) + ChunkSize - 1) / ChunkSize;
78 }
79
80public:
81 ChunkedVector() = default;
82
83 ChunkedVector(ChunkedVector &&) = delete;
84 ChunkedVector(const ChunkedVector &) = delete;
85 ChunkedVector & operator=(ChunkedVector &&) = delete;
86 ChunkedVector & operator=(const ChunkedVector &) = delete;
87
88 ~ChunkedVector()
89 {
90 auto len = size();
91 for (auto & chunkPtr : std::ranges::views::take(*chunks, numActiveChunks())) {
92 auto * chunk = chunkPtr.load(std::memory_order_relaxed);
93 auto count = std::min<decltype(len)>(len, ChunkSize);
94 if constexpr (!std::is_trivially_destructible_v<T>) {
95 std::destroy_n(chunk, count);
96 }
97 ::operator delete(chunk);
98 len -= count;
99 }
100 }
101
105 uint32_t size() const noexcept
106 {
107 return size_.load(std::memory_order_relaxed);
108 }
109
110 template<typename... Args>
111 std::pair<T &, uint32_t> add(Args &&... args)
112 {
113 /* Get some unique index. Doesn't need any synchronises-with relation. */
114 const auto idx = size_.fetch_add(1, std::memory_order_relaxed);
115 auto * chunk = ensureChunk(idx / ChunkSize);
116 auto * p = new (&chunk[idx % ChunkSize]) T(std::forward<Args>(args)...);
117 return {*p, idx};
118 }
119
125 const T & operator[](uint32_t idx) const & noexcept
126 {
127 /* Synchronises-with relation with add(). */
128 auto * chunk = (*chunks)[idx / ChunkSize].load(std::memory_order_acquire);
129 return chunk[idx % ChunkSize];
130 }
131
135 template<typename Fn>
136 void forEach(Fn fn) const
137 {
138 auto len = size();
139 for (auto & chunkPtr : std::ranges::views::take(*chunks, numActiveChunks())) {
140 auto * chunk = chunkPtr.load(std::memory_order_relaxed);
141 auto count = std::min<decltype(len)>(len, ChunkSize);
142 std::for_each_n(chunk, count, fn);
143 len -= count;
144 }
145 }
146};
147} // namespace nix
Definition args.hh:29
uint32_t size() const noexcept
Definition chunked-vector.hh:105
void forEach(Fn fn) const
Definition chunked-vector.hh:136
const T & operator[](uint32_t idx) const &noexcept
Definition chunked-vector.hh:125
This file defines two main structs/classes used in nix error handling.