canonical_value_store.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_CANONICAL_VALUE_STORE_H_
  5. #define CARBON_TOOLCHAIN_BASE_CANONICAL_VALUE_STORE_H_
  6. #include "common/hashtable_key_context.h"
  7. #include "common/set.h"
  8. #include "toolchain/base/mem_usage.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/base/value_store_types.h"
  11. #include "toolchain/base/yaml.h"
  12. namespace Carbon {
  13. // A wrapper for accumulating immutable values with deduplication, providing IDs
  14. // to later retrieve the value.
  15. //
  16. // `ValueT` represents the type being stored.
  17. //
  18. // `KeyT` can optionally be different from `ValueT`, and if so is used for the
  19. // argument to `Lookup`. In this case, `ValueT` must provide a `GetAsKey` member
  20. // function that returns the corresponding key.
  21. template <typename IdT, typename KeyT, typename ValueT = KeyT>
  22. class CanonicalValueStore {
  23. public:
  24. using IdType = IdT;
  25. using KeyType = std::remove_cvref_t<KeyT>;
  26. using ValueType = ValueStoreTypes<ValueT>::ValueType;
  27. using RefType = ValueStoreTypes<ValueT>::RefType;
  28. using ConstRefType = ValueStoreTypes<ValueT>::ConstRefType;
  29. CanonicalValueStore() = default;
  30. template <typename Id>
  31. explicit CanonicalValueStore(Id id, int32_t initial_reserved_ids = 0)
  32. : values_(id, initial_reserved_ids) {}
  33. // Stores a canonical copy of the value and returns an ID to reference it. If
  34. // the value is already in the store, returns the ID of the existing value.
  35. auto Add(ValueType value) -> IdT;
  36. // Returns the value for an ID.
  37. auto Get(IdT id) const -> ConstRefType { return values_.Get(id); }
  38. // Looks up the canonical ID for a value, or returns `None` if not in the
  39. // store.
  40. auto Lookup(KeyType key) const -> IdT;
  41. // Reserves space.
  42. auto Reserve(size_t size) -> void;
  43. // These are to support printable structures, and are not guaranteed.
  44. auto OutputYaml() const -> Yaml::OutputMapping {
  45. return values_.OutputYaml();
  46. }
  47. auto values() const [[clang::lifetimebound]]
  48. -> ValueStore<IdT, ValueType>::Range {
  49. return values_.values();
  50. }
  51. auto size() const -> size_t { return values_.size(); }
  52. // Collects memory usage of the values and deduplication set.
  53. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  54. -> void {
  55. mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
  56. auto bytes = set_.ComputeMetrics(KeyContext(&values_)).storage_bytes;
  57. mem_usage.Add(MemUsage::ConcatLabel(label, "set_"), bytes, bytes);
  58. }
  59. auto GetRawIndex(IdT id) const -> int32_t { return values_.GetRawIndex(id); }
  60. private:
  61. class KeyContext;
  62. static auto GetAsKey(ConstRefType value) -> ConstRefType
  63. requires std::same_as<KeyT, ValueT>
  64. {
  65. return value;
  66. }
  67. template <typename T>
  68. static auto GetAsKey(T&& value) -> decltype(value.GetAsKey()) {
  69. return value.GetAsKey();
  70. }
  71. ValueStore<IdT, ValueType> values_;
  72. Set<IdT, /*SmallSize=*/0, KeyContext> set_;
  73. };
  74. template <typename IdT, typename KeyT, typename ValueT>
  75. class CanonicalValueStore<IdT, KeyT, ValueT>::KeyContext
  76. : public TranslatingKeyContext<KeyContext> {
  77. public:
  78. explicit KeyContext(const ValueStore<IdT, ValueType>* values)
  79. : values_(values) {}
  80. // Note that it is safe to return a reference here as the underlying object's
  81. // lifetime is provided by the `ValueStore`.
  82. auto TranslateKey(IdT id) const
  83. -> decltype(GetAsKey(std::declval<ConstRefType>())) {
  84. return GetAsKey(values_->Get(id));
  85. }
  86. private:
  87. const ValueStore<IdT, ValueType>* values_;
  88. };
  89. template <typename IdT, typename KeyT, typename ValueT>
  90. auto CanonicalValueStore<IdT, KeyT, ValueT>::Add(ValueType value) -> IdT {
  91. auto make_key = [&] { return IdT(values_.Add(std::move(value))); };
  92. return set_.Insert(GetAsKey(value), make_key, KeyContext(&values_)).key();
  93. }
  94. template <typename IdT, typename KeyT, typename ValueT>
  95. auto CanonicalValueStore<IdT, KeyT, ValueT>::Lookup(KeyType key) const -> IdT {
  96. if (auto result = set_.Lookup(key, KeyContext(&values_))) {
  97. return result.key();
  98. }
  99. return IdT::None;
  100. }
  101. template <typename IdT, typename KeyT, typename ValueT>
  102. auto CanonicalValueStore<IdT, KeyT, ValueT>::Reserve(size_t size) -> void {
  103. // Compute the resulting new insert count using the size of values -- the
  104. // set doesn't have a fast to compute current size.
  105. if (size > values_.size()) {
  106. set_.GrowForInsertCount(size - values_.size(), KeyContext(&values_));
  107. }
  108. values_.Reserve(size);
  109. }
  110. } // namespace Carbon
  111. #endif // CARBON_TOOLCHAIN_BASE_CANONICAL_VALUE_STORE_H_