Use unordered_map instead of map for cache

This commit is contained in:
Sebastian Messmer 2015-04-17 12:56:21 +02:00
parent b41853cd00
commit 4a5416dbec
3 changed files with 19 additions and 10 deletions

View File

@ -3,7 +3,7 @@
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING2_MAP_H_ #define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING2_MAP_H_
#include <memory> #include <memory>
#include <map> #include <unordered_map>
#include <cassert> #include <cassert>
namespace blockstore { namespace blockstore {
@ -60,7 +60,7 @@ private:
} }
//TODO Double indirection unique_ptr<Entry> and Entry has unique_ptr<Value>. Necessary? //TODO Double indirection unique_ptr<Entry> and Entry has unique_ptr<Value>. Necessary?
std::map<Key, std::unique_ptr<Entry>> _entries; std::unordered_map<Key, std::unique_ptr<Entry>> _entries;
Entry _sentinel; Entry _sentinel;
}; };

View File

@ -38,9 +38,6 @@ private:
template<int SIZE> bool operator==(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs); template<int SIZE> bool operator==(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs);
template<int SIZE> bool operator!=(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs); template<int SIZE> bool operator!=(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs);
//operator< is defined, so that FixedSizeData objects can be used in std::map and std::set
template<int SIZE> bool operator<(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs);
// ----- Implementation ----- // ----- Implementation -----
template<int SIZE> constexpr unsigned int FixedSizeData<SIZE>::BINARY_LENGTH; template<int SIZE> constexpr unsigned int FixedSizeData<SIZE>::BINARY_LENGTH;
@ -110,11 +107,6 @@ bool operator!=(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs)
return !operator==(lhs, rhs); return !operator==(lhs, rhs);
} }
template<int SIZE>
bool operator<(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs) {
return 0 > std::memcmp(lhs.data(), rhs.data(), FixedSizeData<SIZE>::BINARY_LENGTH);
}
} }
#endif #endif

View File

@ -12,4 +12,21 @@ using Key = FixedSizeData<16>;
} }
namespace std {
//Allow using blockstore::Key in std::unordered_map / std::unordered_set
template <> struct hash<blockstore::Key> {
size_t operator()(const blockstore::Key &key) const {
//Keys are random, so it is enough to use the first few bytes as a hash
return *(size_t*)(key.data());
}
};
//Allow using blockstore::Key in std::map / std::set
template <> struct less<blockstore::Key> {
bool operator()(const blockstore::Key &lhs, const blockstore::Key &rhs) const {
return 0 > std::memcmp(lhs.data(), rhs.data(), blockstore::Key::BINARY_LENGTH);
}
};
}
#endif #endif