From 97e85f94e5ee7e6c8340011757a71222b8091cda Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 21 May 2020 04:53:09 +0100 Subject: refactor(3p/nix/libexpr): Use absl::node_hash_set in SymbolTable This replaces the previous use of std::unordered_set with absl::node_hash_set. This type was chosen because the current implementation requires pointer stability. This does not yet touch the 'Attr' struct. As a bonus, the implementation of the SymbolTable struct is now consolidated into a single header/implementation file pair. --- third_party/nix/src/libexpr/symbol-table.hh | 37 ++++++++++++----------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'third_party/nix/src/libexpr/symbol-table.hh') diff --git a/third_party/nix/src/libexpr/symbol-table.hh b/third_party/nix/src/libexpr/symbol-table.hh index 8c27cf8ec723..62c0b48a173b 100644 --- a/third_party/nix/src/libexpr/symbol-table.hh +++ b/third_party/nix/src/libexpr/symbol-table.hh @@ -1,11 +1,11 @@ #pragma once -#include -#include +#include +#include #include "types.hh" -namespace nix { +namespace nix { // TODO(tazjin): ::expr /* Symbol table used by the parser and evaluator to represent and look up identifiers and attributes efficiently. SymbolTable::create() @@ -15,8 +15,8 @@ namespace nix { class Symbol { private: - const string* s; // pointer into SymbolTable - Symbol(const string* s) : s(s){}; + const std::string* s; // pointer into SymbolTable + Symbol(const std::string* s) : s(s){}; friend class SymbolTable; public: @@ -28,7 +28,7 @@ class Symbol { bool operator<(const Symbol& s2) const { return s < s2.s; } - operator const string&() const { return *s; } + operator const std::string&() const { return *s; } bool set() const { return s; } @@ -38,26 +38,19 @@ class Symbol { }; class SymbolTable { - private: - typedef std::unordered_set Symbols; - Symbols symbols; - public: - Symbol create(const string& s) { - std::pair res = symbols.insert(s); - return Symbol(&*res.first); - } + Symbol Create(absl::string_view sym); - size_t size() const { return symbols.size(); } + // TODO(tazjin): two of these? + size_t Size() const; - size_t totalSize() const; + // Return the total size (in bytes) + size_t TotalSize() const; - template - void dump(T callback) { - for (auto& s : symbols) { - callback(s); - } - } + private: + // flat_hash_set does not retain pointer stability on rehashing, + // hence "interned" strings/symbols are stored on the heap. + absl::node_hash_set symbols_; }; } // namespace nix -- cgit 1.4.1