mirror of
https://github.com/NixOS/nix
synced 2025-06-25 02:21:16 +02:00
Merge pull request #13130 from xokdvium/symbol-table-chore
libexpr: Remove unused field from SymbolTable::symbols and emplace in…
This commit is contained in:
commit
bd643359a2
2 changed files with 20 additions and 11 deletions
|
@ -81,26 +81,29 @@ public:
|
|||
class SymbolTable
|
||||
{
|
||||
private:
|
||||
std::unordered_map<std::string_view, std::pair<const std::string *, uint32_t>> symbols;
|
||||
/**
|
||||
* Map from string view (backed by ChunkedVector) -> offset into the store.
|
||||
* ChunkedVector references are never invalidated.
|
||||
*/
|
||||
std::unordered_map<std::string_view, uint32_t> symbols;
|
||||
ChunkedVector<std::string, 8192> store{16};
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* converts a string into a symbol.
|
||||
* Converts a string into a symbol.
|
||||
*/
|
||||
Symbol create(std::string_view s)
|
||||
{
|
||||
// Most symbols are looked up more than once, so we trade off insertion performance
|
||||
// for lookup performance.
|
||||
// TODO: could probably be done more efficiently with transparent Hash and Equals
|
||||
// on the original implementation using unordered_set
|
||||
// FIXME: make this thread-safe.
|
||||
auto it = symbols.find(s);
|
||||
if (it != symbols.end()) return Symbol(it->second.second + 1);
|
||||
if (it != symbols.end())
|
||||
return Symbol(it->second + 1);
|
||||
|
||||
const auto & [rawSym, idx] = store.add(std::string(s));
|
||||
symbols.emplace(rawSym, std::make_pair(&rawSym, idx));
|
||||
const auto & [rawSym, idx] = store.add(s);
|
||||
symbols.emplace(rawSym, idx);
|
||||
return Symbol(idx + 1);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,9 +45,10 @@ public:
|
|||
addChunk();
|
||||
}
|
||||
|
||||
uint32_t size() const { return size_; }
|
||||
uint32_t size() const noexcept { return size_; }
|
||||
|
||||
std::pair<T &, uint32_t> add(T value)
|
||||
template <typename... Args>
|
||||
std::pair<T &, uint32_t> add(Args &&... args)
|
||||
{
|
||||
const auto idx = size_++;
|
||||
auto & chunk = [&] () -> auto & {
|
||||
|
@ -55,11 +56,16 @@ public:
|
|||
return back;
|
||||
return addChunk();
|
||||
}();
|
||||
auto & result = chunk.emplace_back(std::move(value));
|
||||
auto & result = chunk.emplace_back(std::forward<Args>(args)...);
|
||||
return {result, idx};
|
||||
}
|
||||
|
||||
const T & operator[](uint32_t idx) const
|
||||
/**
|
||||
* Unchecked subscript operator.
|
||||
* @pre add must have been called at least idx + 1 times.
|
||||
* @throws nothing
|
||||
*/
|
||||
const T & operator[](uint32_t idx) const noexcept
|
||||
{
|
||||
return chunks[idx / ChunkSize][idx % ChunkSize];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue