Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
attr-set.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nixexpr.hh"
5#include "symbol-table.hh"
6
7#include <algorithm>
8
9namespace nix {
10
11
12class EvalState;
13struct Value;
14
18struct Attr
19{
20 /* the placement of `name` and `pos` in this struct is important.
21 both of them are uint32 wrappers, they are next to each other
22 to make sure that Attr has no padding on 64 bit machines. that
23 way we keep Attr size at two words with no wasted space. */
24 Symbol name;
25 PosIdx pos;
26 Value * value;
27 Attr(Symbol name, Value * value, PosIdx pos = noPos)
28 : name(name), pos(pos), value(value) { };
29 Attr() { };
30 auto operator <=> (const Attr & a) const
31 {
32 return name <=> a.name;
33 }
34};
35
36static_assert(sizeof(Attr) == 2 * sizeof(uint32_t) + sizeof(Value *),
37 "performance of the evaluator is highly sensitive to the size of Attr. "
38 "avoid introducing any padding into Attr if at all possible, and do not "
39 "introduce new fields that need not be present for almost every instance.");
40
47class Bindings
48{
49public:
50 typedef uint32_t size_t;
51 PosIdx pos;
52
53private:
54 size_t size_, capacity_;
55 Attr attrs[0];
56
57 Bindings(size_t capacity) : size_(0), capacity_(capacity) { }
58 Bindings(const Bindings & bindings) = delete;
59
60public:
61 size_t size() const { return size_; }
62
63 bool empty() const { return !size_; }
64
65 typedef Attr * iterator;
66
67 typedef const Attr * const_iterator;
68
69 void push_back(const Attr & attr)
70 {
71 assert(size_ < capacity_);
72 attrs[size_++] = attr;
73 }
74
75 const_iterator find(Symbol name) const
76 {
77 Attr key(name, 0);
78 const_iterator i = std::lower_bound(begin(), end(), key);
79 if (i != end() && i->name == name) return i;
80 return end();
81 }
82
83 const Attr * get(Symbol name) const
84 {
85 Attr key(name, 0);
86 const_iterator i = std::lower_bound(begin(), end(), key);
87 if (i != end() && i->name == name) return &*i;
88 return nullptr;
89 }
90
91 iterator begin() { return &attrs[0]; }
92 iterator end() { return &attrs[size_]; }
93
94 const_iterator begin() const { return &attrs[0]; }
95 const_iterator end() const { return &attrs[size_]; }
96
97 Attr & operator[](size_t pos)
98 {
99 return attrs[pos];
100 }
101
102 const Attr & operator[](size_t pos) const
103 {
104 return attrs[pos];
105 }
106
107 void sort();
108
109 size_t capacity() const { return capacity_; }
110
114 std::vector<const Attr *> lexicographicOrder(const SymbolTable & symbols) const
115 {
116 std::vector<const Attr *> res;
117 res.reserve(size_);
118 for (size_t n = 0; n < size_; n++)
119 res.emplace_back(&attrs[n]);
120 std::sort(res.begin(), res.end(), [&](const Attr * a, const Attr * b) {
121 std::string_view sa = symbols[a->name], sb = symbols[b->name];
122 return sa < sb;
123 });
124 return res;
125 }
126
127 friend class EvalState;
128};
129
135class BindingsBuilder
136{
137 Bindings * bindings;
138
139public:
140 // needed by std::back_inserter
141 using value_type = Attr;
142
143 EvalState & state;
144
145 BindingsBuilder(EvalState & state, Bindings * bindings)
146 : bindings(bindings), state(state)
147 { }
148
149 void insert(Symbol name, Value * value, PosIdx pos = noPos)
150 {
151 insert(Attr(name, value, pos));
152 }
153
154 void insert(const Attr & attr)
155 {
156 push_back(attr);
157 }
158
159 void push_back(const Attr & attr)
160 {
161 bindings->push_back(attr);
162 }
163
164 Value & alloc(Symbol name, PosIdx pos = noPos);
165
166 Value & alloc(std::string_view name, PosIdx pos = noPos);
167
168 Bindings * finish()
169 {
170 bindings->sort();
171 return bindings;
172 }
173
174 Bindings * alreadySorted()
175 {
176 return bindings;
177 }
178
179 size_t capacity()
180 {
181 return bindings->capacity();
182 }
183
184 void grow(Bindings * newBindings)
185 {
186 for (auto & i : *bindings)
187 newBindings->push_back(i);
188 bindings = newBindings;
189 }
190
191 friend struct ExprAttrs;
192};
193
194}
Definition attr-set.hh:48
friend class EvalState
Definition attr-set.hh:127
std::vector< const Attr * > lexicographicOrder(const SymbolTable &symbols) const
Definition attr-set.hh:114
Definition eval.hh:182
Definition pos-idx.hh:9
Definition symbol-table.hh:82
Definition symbol-table.hh:58
const T::key_type & key
Definition lexer.l:2763
auto i
Definition lexer.l:2745
Strings res
Definition lexer.l:2566
const std::string_view & name
Definition lexer.l:1709
const T & value
Definition lexer.l:492
Bindings * bindings
Definition lexer.l:6423
std::unordered_map< std::string_view, std::pair< const std::string *, uint32_t > > symbols
Definition lexer.l:1010
Definition attr-set.hh:19
Definition value.hh:167