value_store.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
  5. #define CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
  6. #include <memory>
  7. #include <type_traits>
  8. #include "common/check.h"
  9. #include "common/hashtable_key_context.h"
  10. #include "common/ostream.h"
  11. #include "common/set.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/Sequence.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "toolchain/base/mem_usage.h"
  17. #include "toolchain/base/yaml.h"
  18. namespace Carbon {
  19. namespace Internal {
  20. // Used as a parent class for non-printable types. This is just for
  21. // std::conditional, not as an API.
  22. class ValueStoreNotPrintable {};
  23. } // namespace Internal
  24. // A simple wrapper for accumulating values, providing IDs to later retrieve the
  25. // value. This does not do deduplication.
  26. //
  27. // IdT::ValueType must represent the type being indexed.
  28. template <typename IdT>
  29. class ValueStore
  30. : public std::conditional<
  31. std::is_base_of_v<Printable<typename IdT::ValueType>,
  32. typename IdT::ValueType>,
  33. Yaml::Printable<ValueStore<IdT>>, Internal::ValueStoreNotPrintable> {
  34. public:
  35. using ValueType = typename IdT::ValueType;
  36. // Typically we want to use `ValueType&` and `const ValueType& to avoid
  37. // copies, but when the value type is a `StringRef`, we assume external
  38. // storage for the string data and both our value type and ref type will be
  39. // `StringRef`. This will preclude mutation of the string data.
  40. using RefType = std::conditional_t<std::same_as<llvm::StringRef, ValueType>,
  41. llvm::StringRef, ValueType&>;
  42. using ConstRefType =
  43. std::conditional_t<std::same_as<llvm::StringRef, ValueType>,
  44. llvm::StringRef, const ValueType&>;
  45. // Stores the value and returns an ID to reference it.
  46. auto Add(ValueType value) -> IdT {
  47. IdT id(values_.size());
  48. // This routine is especially hot and the check here relatively expensive
  49. // for the value provided, so only do this in debug builds to make tracking
  50. // down issues easier.
  51. CARBON_DCHECK(id.index >= 0, "Id overflow");
  52. values_.push_back(std::move(value));
  53. return id;
  54. }
  55. // Adds a default constructed value and returns an ID to reference it.
  56. auto AddDefaultValue() -> IdT {
  57. IdT id(values_.size());
  58. values_.resize(id.index + 1);
  59. return id;
  60. }
  61. // Returns a mutable value for an ID.
  62. auto Get(IdT id) -> RefType {
  63. CARBON_DCHECK(id.index >= 0, "{0}", id);
  64. return values_[id.index];
  65. }
  66. // Returns the value for an ID.
  67. auto Get(IdT id) const -> ConstRefType {
  68. CARBON_DCHECK(id.index >= 0, "{0}", id);
  69. return values_[id.index];
  70. }
  71. // Reserves space.
  72. auto Reserve(size_t size) -> void { values_.reserve(size); }
  73. // These are to support printable structures, and are not guaranteed.
  74. auto OutputYaml() const -> Yaml::OutputMapping {
  75. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  76. for (auto [id, value] : enumerate()) {
  77. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  78. }
  79. });
  80. }
  81. // Collects memory usage of the values.
  82. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  83. -> void {
  84. mem_usage.Collect(label.str(), values_);
  85. }
  86. auto array_ref() const -> llvm::ArrayRef<ValueType> { return values_; }
  87. auto size() const -> size_t { return values_.size(); }
  88. // Makes an iterable range over pairs of the index and a reference to the
  89. // value for each value in the store.
  90. //
  91. // The range is over references to the values in the store, even if used with
  92. // `auto` to destructure the pair. In this example, the `value` is a
  93. // `ConstRefType`:
  94. // ```
  95. // for (auto [id, value] : store.enumerate()) { ... }
  96. // ```
  97. auto enumerate() const -> auto {
  98. auto index_to_id = [](auto pair) -> std::pair<IdT, ConstRefType> {
  99. auto [index, value] = pair;
  100. return std::pair<IdT, ConstRefType>(IdT(index), value);
  101. };
  102. return llvm::map_range(llvm::enumerate(values_), index_to_id);
  103. }
  104. private:
  105. // Set inline size to 0 because these will typically be too large for the
  106. // stack, while this does make File smaller.
  107. llvm::SmallVector<std::decay_t<ValueType>, 0> values_;
  108. };
  109. // A wrapper for accumulating immutable values with deduplication, providing IDs
  110. // to later retrieve the value.
  111. //
  112. // IdT::ValueType must represent the type being indexed.
  113. template <typename IdT>
  114. class CanonicalValueStore {
  115. public:
  116. using ValueType = typename IdT::ValueType;
  117. using RefType = typename ValueStore<IdT>::RefType;
  118. using ConstRefType = typename ValueStore<IdT>::ConstRefType;
  119. // Stores a canonical copy of the value and returns an ID to reference it.
  120. auto Add(ValueType value) -> IdT;
  121. // Returns the value for an ID.
  122. auto Get(IdT id) const -> ConstRefType { return values_.Get(id); }
  123. // Looks up the canonical ID for a value, or returns `None` if not in the
  124. // store.
  125. auto Lookup(ValueType value) const -> IdT;
  126. // Reserves space.
  127. auto Reserve(size_t size) -> void;
  128. // These are to support printable structures, and are not guaranteed.
  129. auto OutputYaml() const -> Yaml::OutputMapping {
  130. return values_.OutputYaml();
  131. }
  132. auto array_ref() const -> llvm::ArrayRef<ValueType> {
  133. return values_.array_ref();
  134. }
  135. auto size() const -> size_t { return values_.size(); }
  136. // Collects memory usage of the values and deduplication set.
  137. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  138. -> void {
  139. mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
  140. auto bytes =
  141. set_.ComputeMetrics(KeyContext(values_.array_ref())).storage_bytes;
  142. mem_usage.Add(MemUsage::ConcatLabel(label, "set_"), bytes, bytes);
  143. }
  144. private:
  145. class KeyContext;
  146. ValueStore<IdT> values_;
  147. Set<IdT, /*SmallSize=*/0, KeyContext> set_;
  148. };
  149. template <typename IdT>
  150. class CanonicalValueStore<IdT>::KeyContext
  151. : public TranslatingKeyContext<KeyContext> {
  152. public:
  153. explicit KeyContext(llvm::ArrayRef<ValueType> values) : values_(values) {}
  154. // Note that it is safe to return a `const` reference here as the underlying
  155. // object's lifetime is provided by the `store_`.
  156. auto TranslateKey(IdT id) const -> const ValueType& {
  157. return values_[id.index];
  158. }
  159. private:
  160. llvm::ArrayRef<ValueType> values_;
  161. };
  162. template <typename IdT>
  163. auto CanonicalValueStore<IdT>::Add(ValueType value) -> IdT {
  164. auto make_key = [&] { return IdT(values_.Add(std::move(value))); };
  165. return set_.Insert(value, make_key, KeyContext(values_.array_ref())).key();
  166. }
  167. template <typename IdT>
  168. auto CanonicalValueStore<IdT>::Lookup(ValueType value) const -> IdT {
  169. if (auto result = set_.Lookup(value, KeyContext(values_.array_ref()))) {
  170. return result.key();
  171. }
  172. return IdT::None;
  173. }
  174. template <typename IdT>
  175. auto CanonicalValueStore<IdT>::Reserve(size_t size) -> void {
  176. // Compute the resulting new insert count using the size of values -- the
  177. // set doesn't have a fast to compute current size.
  178. if (size > values_.size()) {
  179. set_.GrowForInsertCount(size - values_.size(),
  180. KeyContext(values_.array_ref()));
  181. }
  182. values_.Reserve(size);
  183. }
  184. // A ValueStore that builds a 1:1 relationship between two IDs.
  185. // * `RelatedIdT` represents a related ID that can be used to find values in the
  186. // store.
  187. // * `IdT` is the actual ID of values in this store, and `IdT::ValueType` is the
  188. // value type being stored.
  189. //
  190. // The value store builds a mapping so that either ID can be used later to find
  191. // a value. And the user can query if a related `RelatedIdT` has been used to
  192. // add a value to the store or not.
  193. //
  194. // When adding to the store, the user provides the related `RelatedIdT` along
  195. // with the value being stored, and gets back the ID of the value in the store.
  196. //
  197. // This store requires more storage space than normal ValueStore does, as it
  198. // requires storing a bit for presence of each `RelatedIdT`. And it allocates
  199. // memory for values for all IDs up largest ID present in the store, even if
  200. // they are not yet used.
  201. template <typename RelatedIdT, typename IdT>
  202. class RelationalValueStore {
  203. public:
  204. using ValueType = IdT::ValueType;
  205. using ConstRefType = ValueStore<IdT>::ConstRefType;
  206. // Given the related ID and a value, stores the value and returns a mapped ID
  207. // to reference it in the store.
  208. auto Add(RelatedIdT related_id, ValueType value) -> IdT {
  209. CARBON_DCHECK(related_id.index >= 0, "{0}", related_id);
  210. IdT id(related_id.index);
  211. if (static_cast<size_t>(id.index) >= values_.size()) {
  212. values_.resize(id.index + 1);
  213. }
  214. auto& opt = values_[id.index];
  215. CARBON_CHECK(!opt.has_value(),
  216. "Add with `related_id` that was already added to the store");
  217. opt.emplace(std::move(value));
  218. return id;
  219. }
  220. // Returns the ID of a value in the store if the `related_id` was previously
  221. // used to add a value to the store, or None.
  222. auto TryGetId(RelatedIdT related_id) const -> IdT {
  223. CARBON_DCHECK(related_id.index >= 0, "{0}", related_id);
  224. if (static_cast<size_t>(related_id.index) >= values_.size()) {
  225. return IdT::None;
  226. }
  227. auto& opt = values_[related_id.index];
  228. if (!opt.has_value()) {
  229. return IdT::None;
  230. }
  231. return IdT(related_id.index);
  232. }
  233. // Returns a value for an ID.
  234. auto Get(IdT id) const -> ConstRefType {
  235. CARBON_DCHECK(id.index >= 0, "{0}", id);
  236. return *values_[id.index];
  237. }
  238. private:
  239. // Set inline size to 0 because these will typically be too large for the
  240. // stack, while this does make File smaller.
  241. llvm::SmallVector<std::optional<std::decay_t<ValueType>>, 0> values_;
  242. };
  243. } // namespace Carbon
  244. #endif // CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_