Avoid disk I/O when data 'moves' in a SHAMap

This commit is contained in:
David Schwartz
2014-03-05 11:41:32 -08:00
committed by Nik Bougalis
parent 2505a908c5
commit 16d2bbd6e5
3 changed files with 24 additions and 6 deletions

View File

@@ -61,7 +61,7 @@ SHAMap::SHAMap (SHAMapType t, uint256 const& hash, FullBelowCache& fullBelowCach
mTNByID.replace(*root, root); mTNByID.replace(*root, root);
} }
TaggedCache< SHAMap::TNIndex, SHAMapTreeNode> TaggedCache <uint256, SHAMapTreeNode>
SHAMap::treeNodeCache ("TreeNodeCache", 65536, 60, SHAMap::treeNodeCache ("TreeNodeCache", 65536, 60,
get_seconds_clock (), get_seconds_clock (),
LogPartition::getJournal <TaggedCacheLog> ()); LogPartition::getJournal <TaggedCacheLog> ());
@@ -1263,15 +1263,29 @@ void SHAMap::dump (bool hash)
SHAMapTreeNode::pointer SHAMap::getCache (uint256 const& hash, SHAMapNode const& id) SHAMapTreeNode::pointer SHAMap::getCache (uint256 const& hash, SHAMapNode const& id)
{ {
SHAMapTreeNode::pointer ret = treeNodeCache.fetch (TNIndex (hash, id)); SHAMapTreeNode::pointer ret = treeNodeCache.fetch (hash);
assert (!ret || !ret->getSeq()); assert (!ret || !ret->getSeq());
if (ret && (*ret != id))
{
// We have the data, but with a different node ID
WriteLog (lsTRACE, SHAMap) << "ID mismatch: " << id << " != " << *ret;
ret = boost::make_shared <SHAMapTreeNode> (*ret, 0);
ret->set(id);
// Future fetches are likely to use the "new" ID
treeNodeCache.canonicalize (hash, ret, true);
assert (*ret == id);
assert (ret->getNodeHash() == hash);
}
return ret; return ret;
} }
void SHAMap::canonicalize (uint256 const& hash, SHAMapTreeNode::pointer& node) void SHAMap::canonicalize (uint256 const& hash, SHAMapTreeNode::pointer& node)
{ {
assert (node->getSeq() == 0); assert (node->getSeq() == 0);
treeNodeCache.canonicalize (TNIndex (hash, *node), node); treeNodeCache.canonicalize (hash, node);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@@ -270,10 +270,8 @@ public:
treeNodeCache.setTargetAge (age); treeNodeCache.setTargetAge (age);
} }
typedef std::pair<uint256, SHAMapNode> TNIndex;
private: private:
static TaggedCache <TNIndex, SHAMapTreeNode> treeNodeCache; static TaggedCache <uint256, SHAMapTreeNode> treeNodeCache;
void dirtyUp (std::stack<SHAMapTreeNode::pointer>& stack, uint256 const & target, uint256 prevHash); void dirtyUp (std::stack<SHAMapTreeNode::pointer>& stack, uint256 const & target, uint256 prevHash);
std::stack<SHAMapTreeNode::pointer> getStack (uint256 const & id, bool include_nonmatching_leaf); std::stack<SHAMapTreeNode::pointer> getStack (uint256 const & id, bool include_nonmatching_leaf);

View File

@@ -92,6 +92,12 @@ public:
{ {
return n != mNodeID; return n != mNodeID;
} }
void set (SHAMapNode const& from)
{
mNodeID = from.mNodeID;
mDepth = from.mDepth;
mHash = from.mHash;
}
virtual std::string getString () const; virtual std::string getString () const;
void dump () const; void dump () const;