Skip to content

Commit

Permalink
Fix a test failure that occurs when we add new validation in swisstable.
Browse files Browse the repository at this point in the history
We can't use swisstable here because this code requires an exception-safe hashtable so switch to std::unordered_map.

PiperOrigin-RevId: 631524388
  • Loading branch information
tensorflower-gardener committed May 7, 2024
1 parent 4d3287a commit 9b74d43
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions third_party/xla/xla/pjrt/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#define XLA_PJRT_LRU_CACHE_H_

#include <optional>
#include <unordered_map>

#include "absl/container/node_hash_map.h"
#include "tsl/platform/logging.h"
Expand Down Expand Up @@ -101,16 +102,17 @@ class LRUCache {
struct Entry : public LRUListEntry {
Entry() = default;

// Pointer to the key in `entries_`. absl::node_hash_map<> promises
// Pointer to the key in `entries_`. std::unordered_map<> promises
// pointer stability for keys.
const Key* key;
LRUCache* container;
std::optional<Value> value;
};

// We use `node_hash_map` because we want to guarantee pointer stability for
// keys and values.
absl::node_hash_map<Key, Entry, Hash, Eq> entries_;
// We use `unordered_map` because (a) we want to guarantee pointer stability
// for keys and values, and (b) we need exception safety so we can't use
// absl hashtables.
std::unordered_map<Key, Entry, Hash, Eq> entries_;
};

template <typename Key, typename Value, typename Hash, typename Eq>
Expand Down Expand Up @@ -140,9 +142,7 @@ LRUCache<Key, Value, Hash, Eq>::~LRUCache() {
template <typename Key, typename Value, typename Hash, typename Eq>
Value LRUCache<Key, Value, Hash, Eq>::GetOrCreateIfAbsent(
const Key& key, const std::function<Value(const Key&)>& factory) {
typename absl::node_hash_map<Key, Entry, Hash, Eq>::iterator it;
bool inserted;
std::tie(it, inserted) = entries_.try_emplace(key);
auto [it, inserted] = entries_.try_emplace(key);
Entry& entry = it->second;
if (inserted) {
entry.key = &it->first;
Expand Down

0 comments on commit 9b74d43

Please sign in to comment.