From 57313a933996b67a6ddbd1b8dbd00409de6a6ce9 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sat, 12 Jan 2013 03:05:45 -0800 Subject: [PATCH] Cache the hash of a SHAMapNode. --- src/cpp/ripple/SHAMap.cpp | 12 +++++++----- src/cpp/ripple/SHAMap.h | 11 ++++++++--- src/cpp/ripple/SHAMapNodes.cpp | 7 ++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/cpp/ripple/SHAMap.cpp b/src/cpp/ripple/SHAMap.cpp index ad536e762..e549b9325 100644 --- a/src/cpp/ripple/SHAMap.cpp +++ b/src/cpp/ripple/SHAMap.cpp @@ -21,13 +21,15 @@ DECLARE_INSTANCE(SHAMap); DECLARE_INSTANCE(SHAMapItem); DECLARE_INSTANCE(SHAMapTreeNode); +void SHAMapNode::setHash() const +{ + std::size_t h = theApp->getNonceST() + mDepth; + mHash = mNodeID.hash_combine(h); +} + std::size_t hash_value(const SHAMapNode& mn) { - std::size_t seed = theApp->getNonceST(); - - boost::hash_combine(seed, mn.getDepth()); - - return mn.getNodeID().hash_combine(seed); + return mn.getHash(); } std::size_t hash_value(const uint256& u) diff --git a/src/cpp/ripple/SHAMap.h b/src/cpp/ripple/SHAMap.h index afd323fdd..a03eb76bd 100644 --- a/src/cpp/ripple/SHAMap.h +++ b/src/cpp/ripple/SHAMap.h @@ -27,23 +27,28 @@ class SHAMap; class SHAMapNode -{ // Identifies a node in a SHA256 hash +{ // Identifies a node in a SHA256 hash map private: static uint256 smMasks[65]; // AND with hash to get node id uint256 mNodeID; - int mDepth; + int mDepth; + mutable size_t mHash; + + void setHash() const; public: static const int rootDepth = 0; - SHAMapNode() : mDepth(0) { ; } + SHAMapNode() : mDepth(0), mHash(0) { ; } SHAMapNode(int depth, const uint256& hash); virtual ~SHAMapNode() { ; } + int getDepth() const { return mDepth; } const uint256& getNodeID() const { return mNodeID; } bool isValid() const { return (mDepth >= 0) && (mDepth < 64); } + size_t getHash() const { if (mHash == 0) setHash(); return mHash; } virtual bool isPopulated() const { return false; } diff --git a/src/cpp/ripple/SHAMapNodes.cpp b/src/cpp/ripple/SHAMapNodes.cpp index 724f70942..0cfa10b53 100644 --- a/src/cpp/ripple/SHAMapNodes.cpp +++ b/src/cpp/ripple/SHAMapNodes.cpp @@ -102,15 +102,16 @@ uint256 SHAMapNode::getNodeID(int depth, const uint256& hash) return hash & smMasks[depth]; } -SHAMapNode::SHAMapNode(int depth, const uint256 &hash) : mDepth(depth) +SHAMapNode::SHAMapNode(int depth, const uint256 &hash) : mDepth(depth), mHash(0) { // canonicalize the hash to a node ID for this depth assert((depth >= 0) && (depth < 65)); mNodeID = getNodeID(depth, hash); } -SHAMapNode::SHAMapNode(const void *ptr, int len) +SHAMapNode::SHAMapNode(const void *ptr, int len) : mHash(0) { - if (len < 33) mDepth = -1; + if (len < 33) + mDepth = -1; else { memcpy(mNodeID.begin(), ptr, 32);