cache on depth

This commit is contained in:
Brannon King 2019-09-09 11:28:53 -06:00
parent fbf7de9fb2
commit c309f811c1
3 changed files with 21 additions and 16 deletions

View file

@ -547,26 +547,34 @@ std::vector<std::string> extractChildren(CClaimPrefixTrie::const_iterator it)
return children; return children;
} }
static std::size_t cacheHits = 0;
static std::size_t cacheMisses = 0;
bool CClaimTrie::find(const std::string& key, CClaimTrieDataNode& node) const bool CClaimTrie::find(const std::string& key, CClaimTrieDataNode& node) const
{ {
if (auto it = cacheNodes.find(key)) { if (auto it = cacheNodes.find(key)) {
if (it.hasChildren()) { if (it.hasChildren()) {
cacheHits++;
node.hash = it->hash; node.hash = it->hash;
node.children = extractChildren(it); node.children = extractChildren(it);
return true; return true;
} }
} }
return db->Read(std::make_pair(TRIE_NODE_CHILDREN, key), node); auto ret = db->Read(std::make_pair(TRIE_NODE_CHILDREN, key), node);
if (ret) ++cacheMisses;
return ret;
} }
bool CClaimTrie::find(const std::string& key, CClaimTrieData& data) const bool CClaimTrie::find(const std::string& key, CClaimTrieData& data) const
{ {
if (auto it = cacheNodes.find(key)) { if (auto it = cacheNodes.find(key)) {
cacheHits++;
data = it.data(); data = it.data();
data.flags |= CAME_FROM_NODE_CACHE; data.flags |= CAME_FROM_NODE_CACHE;
return true; return true;
} }
return db->Read(std::make_pair(TRIE_NODE_CLAIMS, key), data); auto ret = db->Read(std::make_pair(TRIE_NODE_CLAIMS, key), data);
if (ret) ++cacheMisses;
return ret;
} }
template <typename K, typename T> template <typename K, typename T>
@ -604,18 +612,10 @@ bool CClaimTrieCacheBase::flush()
getMerkleHash(); getMerkleHash();
if (!nodesToDelete.empty() || !nodesToAddOrUpdate.empty()) {
base->cacheNodes.clear();
if (auto it = nodesToAddOrUpdate.begin()) {
if (it->flags & HASH_DIRTY) {
for (auto& child : it.children())
base->cacheNodes.copy(child);
base->cacheNodes.copy(it);
}
}
}
for (auto it = nodesToAddOrUpdate.begin(); it != nodesToAddOrUpdate.end(); ++it) { for (auto it = nodesToAddOrUpdate.begin(); it != nodesToAddOrUpdate.end(); ++it) {
if (it.depth() < 4) {
base->cacheNodes.copy(it, it.depth() == 3 || (it->flags & HASH_DIRTY && !it.hasChildren()));
}
bool removed = forDeleteFromBase.erase(it.key()); bool removed = forDeleteFromBase.erase(it.key());
if (it->flags & HASH_DIRTY) { if (it->flags & HASH_DIRTY) {
CClaimTrieDataNode node; CClaimTrieDataNode node;
@ -636,6 +636,9 @@ bool CClaimTrieCacheBase::flush()
batch.Erase(std::make_pair(TRIE_NODE_CLAIMS, name)); batch.Erase(std::make_pair(TRIE_NODE_CLAIMS, name));
} }
fprintf(stderr, "Height: %d, Hits: %zu, Misses: %zu, Cache Size: %zu\n", nNextHeight, cacheHits, cacheMisses, base->cacheNodes.height());
cacheHits = cacheMisses = 0;
BatchWriteQueue(batch, SUPPORT, supportCache); BatchWriteQueue(batch, SUPPORT, supportCache);
BatchWriteQueue(batch, CLAIM_QUEUE_ROW, claimQueueCache); BatchWriteQueue(batch, CLAIM_QUEUE_ROW, claimQueueCache);

View file

@ -320,12 +320,14 @@ typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(con
} }
template <typename TKey, typename TData> template <typename TKey, typename TData>
typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::copy(CPrefixTrie<TKey, TData>::const_iterator it) typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::copy(CPrefixTrie<TKey, TData>::const_iterator it, bool wipeChildren)
{ {
auto& key = it.key(); auto& key = it.key();
auto& node = key.empty() ? root : insert(key, root); auto& node = key.empty() ? root : insert(key, root);
node->data = it.node.lock()->data; node->data = it.node.lock()->data;
return key.empty() ? begin() : iterator{key, node}; if (wipeChildren)
node->children.clear();
return iterator{key, node};
} }
template <typename TKey, typename TData> template <typename TKey, typename TData>

View file

@ -133,7 +133,7 @@ public:
template <typename TDataUni> template <typename TDataUni>
iterator insert(iterator& it, const TKey& key, TDataUni&& data); iterator insert(iterator& it, const TKey& key, TDataUni&& data);
iterator copy(const_iterator it); iterator copy(const_iterator it, bool wipeChildren);
iterator find(const TKey& key); iterator find(const TKey& key);
const_iterator find(const TKey& key) const; const_iterator find(const TKey& key) const;