1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-11 04:56:01 +01:00

store Symbols in a table as well, like positions

this slightly increases the amount of memory used for any given symbol, but this
increase is more than made up for if the symbol is referenced more than once in
the EvalState that holds it. on average every symbol should be referenced at
least twice (once to introduce a binding, once to use it), so we expect no
increase in memory on average.

symbol tables are limited to 2³² entries like position tables, and similar
arguments apply to why overflow is not likely: 2³² symbols would require as many
string instances (at 24 bytes each) and map entries (at 24 bytes or more each,
assuming that the map holds on average at most one item per bucket as the docs
say). a full symbol table would require at least 192GB of memory just for
symbols, which is well out of reach. (an ofborg eval of nixpks today creates
less than a million symbols!)
This commit is contained in:
pennae 2022-03-05 14:40:24 +01:00
parent 00a3280232
commit 8775be3393
32 changed files with 525 additions and 448 deletions

View file

@ -15,10 +15,10 @@ struct Value;
/* Map one attribute name to its value. */
struct Attr
{
Symbol name;
SymbolIdx name;
Value * value;
PosIdx pos;
Attr(Symbol name, Value * value, PosIdx pos = noPos)
Attr(SymbolIdx name, Value * value, PosIdx pos = noPos)
: name(name), value(value), pos(pos) { };
Attr() { };
bool operator < (const Attr & a) const
@ -57,7 +57,7 @@ public:
attrs[size_++] = attr;
}
iterator find(const Symbol & name)
iterator find(const SymbolIdx & name)
{
Attr key(name, 0);
iterator i = std::lower_bound(begin(), end(), key);
@ -65,7 +65,7 @@ public:
return end();
}
Attr * get(const Symbol & name)
Attr * get(const SymbolIdx & name)
{
Attr key(name, 0);
iterator i = std::lower_bound(begin(), end(), key);
@ -86,14 +86,15 @@ public:
size_t capacity() { return capacity_; }
/* Returns the attributes in lexicographically sorted order. */
std::vector<const Attr *> lexicographicOrder() const
std::vector<const Attr *> lexicographicOrder(const SymbolTable & symbols) const
{
std::vector<const Attr *> res;
res.reserve(size_);
for (size_t n = 0; n < size_; n++)
res.emplace_back(&attrs[n]);
std::sort(res.begin(), res.end(), [](const Attr * a, const Attr * b) {
return (const std::string &) a->name < (const std::string &) b->name;
std::sort(res.begin(), res.end(), [&](const Attr * a, const Attr * b) {
std::string_view sa = symbols[a->name], sb = symbols[b->name];
return sa < sb;
});
return res;
}
@ -118,7 +119,7 @@ public:
: bindings(bindings), state(state)
{ }
void insert(Symbol name, Value * value, PosIdx pos = noPos)
void insert(SymbolIdx name, Value * value, PosIdx pos = noPos)
{
insert(Attr(name, value, pos));
}
@ -133,7 +134,7 @@ public:
bindings->push_back(attr);
}
Value & alloc(const Symbol & name, PosIdx pos = noPos);
Value & alloc(const SymbolIdx & name, PosIdx pos = noPos);
Value & alloc(std::string_view name, PosIdx pos = noPos);