Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
symbol-table.hh
Go to the documentation of this file.
1
#pragma once
3
4
#include <memory_resource>
5
#include "
nix/expr/value.hh
"
6
#include "
nix/expr/static-string-data.hh
"
7
#include "
nix/util/chunked-vector.hh
"
8
#include "
nix/util/bump-memory-resource.hh
"
9
#include "
nix/util/error.hh
"
10
11
#include <boost/version.hpp>
12
#include <boost/unordered/concurrent_flat_set.hpp>
13
14
namespace
nix {
15
16
class
StaticSymbolTable
;
17
23
class
Symbol
24
{
25
friend
class
SymbolTable;
26
friend
class
SymbolStr;
27
friend
class
StaticSymbolTable;
28
friend
struct
Value;
29
30
public
:
31
using
value_type = uint32_t;
32
33
private
:
34
value_type id;
35
36
static_assert
(
sizeof
(detail::ValueBase::Symbol::symbol) >=
sizeof
(id));
37
38
explicit
constexpr
Symbol(value_type
id
) noexcept
39
: id(
id
)
40
{
41
}
42
43
public
:
44
constexpr
Symbol() noexcept
45
: id(0)
46
{
47
}
48
49
[[gnu::always_inline]]
50
constexpr
explicit
operator
bool()
const
noexcept
51
{
52
return
id
> 0;
53
}
54
58
[[gnu::always_inline]]
59
constexpr
value_type
getId
() const noexcept
60
{
61
return
id;
62
}
63
64
constexpr
auto
operator<=>(
const
Symbol
& other)
const
noexcept
=
default
;
65
66
friend
class
std::hash<
Symbol
>;
67
};
68
69
inline
Symbol
Value::symbol
() const noexcept
70
{
71
if
(!isa<tSymbol>())
72
return
{};
73
detail::ValueBase::Symbol
payload;
74
ValueStorage::getStorage(payload);
75
return
nix::Symbol
(
static_cast<
nix::Symbol::value_type
>
(payload.symbol));
76
}
77
83
class
SymbolStr
84
{
85
friend
class
SymbolTable;
86
87
constexpr
static
size_t
chunkSize{131072};
93
using
SymbolValueStore =
ChunkedVector
<
94
Value
,
95
chunkSize,
96
/*MaxChunks=*/
std::numeric_limits<Symbol::value_type>::max() / chunkSize>;
97
98
const
Value
* s;
99
100
struct
Key
101
{
102
using
HashType = boost::hash<std::string_view>;
103
104
SymbolValueStore & store;
105
std::string_view s;
106
std::size_t hash;
107
std::pmr::memory_resource & resource;
108
109
Key(SymbolValueStore & store, std::string_view s, std::pmr::memory_resource & stringMemory)
110
: store(store)
111
, s(s)
112
, hash(HashType{}(s))
113
, resource(stringMemory)
114
{
115
}
116
};
117
118
public
:
119
SymbolStr(
const
Value
& s) noexcept
120
: s(&s)
121
{
122
}
123
124
SymbolStr(
const
Key & key)
125
{
126
const
auto
& [v, idx] = key.store.add(
Value
{});
127
detail::ValueBase::Symbol
symbol;
128
symbol.symbol = idx + 1;
129
if
(key.s.empty()) {
130
symbol.str = &
""
_sds;
131
}
else
{
132
symbol.str = &
StringData::make
(key.resource, key.s);
133
}
134
v.setStorage(symbol);
135
this->s = &v;
136
}
137
138
bool
operator==(std::string_view s2)
const
noexcept
139
{
140
return
s->string_view() == s2;
141
}
142
143
[[gnu::always_inline]]
144
const
StringData
& string_data()
const
noexcept
145
{
146
return
s->string_data();
147
}
148
149
[[gnu::always_inline]]
150
const
char
* c_str()
const
noexcept
151
{
152
return
s->c_str();
153
}
154
155
[[gnu::always_inline]]
operator
std::string_view()
const
noexcept
156
{
157
return
s->string_view();
158
}
159
160
friend
std::ostream & operator<<(std::ostream & os,
const
SymbolStr & symbol);
161
162
[[gnu::always_inline]]
163
bool
empty()
const
noexcept
164
{
165
auto
* p = &s->string_data();
166
// Save a dereference in the sentinel value case
167
if
(p == &
""
_sds)
168
return
true
;
169
return
p->size() == 0;
170
}
171
172
[[gnu::always_inline]]
173
size_t
size()
const
noexcept
174
{
175
return
s->string_data().size();
176
}
177
178
[[gnu::always_inline]]
179
const
Value
* valuePtr()
const
noexcept
180
{
181
return
s;
182
}
183
184
explicit
operator
Symbol
()
const
noexcept
185
{
186
return
s->symbol();
187
}
188
189
struct
Hash
190
{
191
using
is_transparent = void;
192
using
is_avalanching = std::true_type;
193
194
std::size_t operator()(SymbolStr str)
const
195
{
196
return
Key::HashType{}(str.s->string_view());
197
}
198
199
std::size_t operator()(
const
Key & key)
const
noexcept
200
{
201
return
key.hash;
202
}
203
};
204
205
struct
Equal
206
{
207
using
is_transparent = void;
208
209
bool
operator()(SymbolStr a, SymbolStr b)
const
noexcept
210
{
211
// strings are unique, so that a pointer comparison is OK
212
return
a.s == b.s;
213
}
214
215
bool
operator()(SymbolStr a,
const
Key & b)
const
noexcept
216
{
217
return
a == b.s;
218
}
219
220
[[gnu::always_inline]]
221
bool
operator()(
const
Key & a, SymbolStr b)
const
noexcept
222
{
223
return
operator()(b, a);
224
}
225
};
226
};
227
228
class
SymbolTable
;
229
233
class
StaticSymbolTable
234
{
235
static
constexpr
std::size_t maxSize = 1024;
236
237
struct
StaticSymbolInfo
238
{
239
std::string_view str;
240
Symbol
sym;
241
};
242
243
std::array<StaticSymbolInfo, maxSize> symbols;
244
std::size_t size = 0;
245
246
public
:
247
constexpr
StaticSymbolTable() =
default
;
248
249
constexpr
Symbol
create(std::string_view str)
250
{
251
/* No need to check bounds because out of bounds access is
252
a compilation error. */
253
auto
sym =
Symbol
(size + 1);
//< +1 because Symbol with id = 0 is reserved
254
symbols[size++] = {str, sym};
255
return
sym;
256
}
257
258
void
copyIntoSymbolTable(
SymbolTable
& symtab)
const
;
259
};
260
265
class
SymbolTable
266
{
267
private
:
268
/* Thread-safe fallback resource, which might be a bit slower. */
269
std::pmr::synchronized_pool_resource fallbackResource{};
274
BumpMemoryResource
buffer{BumpMemoryResource::defaultReserveSize, &fallbackResource};
275
SymbolStr::SymbolValueStore store;
276
281
boost::concurrent_flat_set<SymbolStr, SymbolStr::Hash, SymbolStr::Equal> symbols{SymbolStr::chunkSize};
282
283
public
:
284
SymbolTable(
const
StaticSymbolTable
& staticSymtab)
285
{
286
staticSymtab.copyIntoSymbolTable(*
this
);
287
}
288
292
Symbol
create
(std::string_view s)
293
{
294
Symbol
res;
295
symbols.insert_and_cvisit(
296
SymbolStr::Key{store, s, buffer},
297
[&](
const
auto
& key) { res =
Symbol
(key); },
298
[&](
const
auto
& key) { res =
Symbol
(key); });
299
return
res;
300
}
301
307
Symbol
create
(
const
Value
& v)
308
{
309
if
(
auto
symbol = v.
symbol
())
310
return
symbol;
311
return
create
(v.string_view());
312
}
313
314
std::vector<SymbolStr> resolve(
const
std::span<const Symbol> & symbols)
const
315
{
316
std::vector<SymbolStr> result;
317
result.reserve(symbols.size());
318
for
(
auto
sym : symbols)
319
result.push_back((*
this
)[sym]);
320
return
result;
321
}
322
323
SymbolStr operator[](Symbol s)
const
324
{
325
auto
idx = s.id;
326
if
(--idx >= store.
size
())
327
unreachable();
328
329
/* Since ChunkedVector itself doesn't provide happens-before guarantees
330
about the object construction, we rely on other synchronisation
331
primitives to ensure happens-before relation between insertion
332
into the ChunkedVector and the load here. If another thread interns
333
the symbol via the symbols map, then the happens-before guarantee
334
should be provided by the concurrent_map itself. If thread B gets
335
Symbol from thread A through other means then it should also ensure
336
happens-before relation. */
337
338
return
store[idx];
339
}
340
341
[[gnu::always_inline]]
342
size_t
size() const noexcept
343
{
344
return
store.
size
();
345
}
346
347
size_t
totalSize()
const
;
348
349
template
<
typename
T>
350
void
dump(T callback)
const
351
{
352
store.forEach([&](
const
Value & v) { callback(SymbolStr(v)); });
353
}
354
};
355
356
inline
void
StaticSymbolTable::copyIntoSymbolTable(
SymbolTable
& symtab)
const
357
{
358
for
(std::size_t i = 0; i < size; ++i) {
359
auto
[str, staticSym] = symbols[i];
360
auto
sym = symtab.create(str);
361
if
(sym != staticSym) [[unlikely]]
362
unreachable();
363
}
364
}
365
366
}
// namespace nix
367
368
template
<>
369
struct
std::hash<nix::Symbol>
370
{
371
std::size_t operator()(
const
nix::Symbol
& s)
const
noexcept
372
{
373
return
std::hash<
decltype
(s.id)>{}(s.id);
374
}
375
};
bump-memory-resource.hh
chunked-vector.hh
nix::BumpMemoryResource
Definition
bump-memory-resource.hh:15
nix::ChunkedVector
Definition
chunked-vector.hh:29
nix::ChunkedVector::size
uint32_t size() const noexcept
Definition
chunked-vector.hh:105
nix::StaticSymbolTable
Definition
symbol-table.hh:234
nix::StringData
Definition
value.hh:213
nix::StringData::make
static const StringData & make(EvalMemory &mem, std::string_view s)
Definition
eval.cc:98
nix::SymbolTable
Definition
symbol-table.hh:266
nix::SymbolTable::create
Symbol create(std::string_view s)
Definition
symbol-table.hh:292
nix::SymbolTable::create
Symbol create(const Value &v)
Definition
symbol-table.hh:307
nix::Symbol
Definition
symbol-table.hh:24
nix::Symbol::getId
constexpr value_type getId() const noexcept
Definition
symbol-table.hh:59
error.hh
This file defines two main structs/classes used in nix error handling.
static-string-data.hh
nix::SymbolStr::Equal
Definition
symbol-table.hh:206
nix::SymbolStr::Hash
Definition
symbol-table.hh:190
nix::Value
Definition
value.hh:1166
nix::Value::symbol
nix::Symbol symbol() const noexcept
Definition
symbol-table.hh:69
nix::detail::ValueBase::Symbol
Definition
value.hh:307
value.hh
libexpr
include
nix
expr
symbol-table.hh
Generated by
1.18.0