From fd296b4411248b9a2da35a4d54df7c175c776389 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 9 Apr 2013 19:42:57 -0700 Subject: [PATCH] Pathfinding performance improvements. --- src/cpp/ripple/Application.cpp | 21 +++++++++++++-------- src/cpp/ripple/Application.h | 2 +- src/cpp/ripple/HashedObject.cpp | 10 +++++----- src/cpp/ripple/RippleState.cpp | 25 +++++++++++++------------ src/cpp/ripple/RippleState.h | 12 ++++++------ src/cpp/ripple/SHAMapNodes.cpp | 4 ++-- src/cpp/ripple/SerializedObject.cpp | 12 ++++++++---- src/cpp/ripple/SerializedObject.h | 2 +- src/cpp/ripple/SerializedTypes.cpp | 5 ++--- src/cpp/ripple/SerializedTypes.h | 8 +++----- src/cpp/ripple/main.cpp | 2 ++ 11 files changed, 56 insertions(+), 47 deletions(-) diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index c64deb311c..2b91535ca6 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -169,7 +169,11 @@ void Application::setup() { cLog(lsINFO) << "Loading specified Ledger"; - loadOldLedger(theConfig.START_LEDGER); + if (!loadOldLedger(theConfig.START_LEDGER)) + { + theApp->stop(); + exit(-1); + } } else if (theConfig.START_UP == Config::NETWORK) { // This should probably become the default once we have a stable network @@ -389,7 +393,7 @@ void Application::startNewLedger() } } -void Application::loadOldLedger(const std::string& l) +bool Application::loadOldLedger(const std::string& l) { try { @@ -408,7 +412,7 @@ void Application::loadOldLedger(const std::string& l) if (!loadLedger) { cLog(lsFATAL) << "No Ledger found?" << std::endl; - exit(-1); + return false; } loadLedger->setClosed(); @@ -418,19 +422,19 @@ void Application::loadOldLedger(const std::string& l) { cLog(lsFATAL) << "Ledger is empty."; assert(false); - exit(-1); + return false; } if (!loadLedger->walkLedger()) { cLog(lsFATAL) << "Ledger is missing nodes."; - exit(-1); + return false; } if (!loadLedger->assertSane()) { cLog(lsFATAL) << "Ledger is not sane."; - exit(-1); + return false; } mLedgerMaster.setLedgerRangePresent(loadLedger->getLedgerSeq(), loadLedger->getLedgerSeq()); @@ -441,13 +445,14 @@ void Application::loadOldLedger(const std::string& l) catch (SHAMapMissingNode&) { cLog(lsFATAL) << "Data is missing for selected ledger"; - exit(-1); + return false; } catch (boost::bad_lexical_cast&) { cLog(lsFATAL) << "Ledger specified '" << l << "' is not valid"; - exit(-1); + return false; } + return true; } // vim:ts=4 diff --git a/src/cpp/ripple/Application.h b/src/cpp/ripple/Application.h index 6318e4972d..2a848ed7c5 100644 --- a/src/cpp/ripple/Application.h +++ b/src/cpp/ripple/Application.h @@ -92,7 +92,7 @@ class Application void updateTables(); void startNewLedger(); - void loadOldLedger(const std::string&); + bool loadOldLedger(const std::string&); public: Application(); diff --git a/src/cpp/ripple/HashedObject.cpp b/src/cpp/ripple/HashedObject.cpp index 143ad17584..b3f4e76d50 100644 --- a/src/cpp/ripple/HashedObject.cpp +++ b/src/cpp/ripple/HashedObject.cpp @@ -184,17 +184,16 @@ HashedObject::pointer HashedObjectStore::retrieve(const uint256& hash) #ifndef NO_SQLITE3_PREPARE { - std::string sql = "SELECT ObjType,LedgerIndex,Object FROM CommittedObjects WHERE Hash = '"; - sql.append(hash.GetHex()); - sql.append("';"); - ScopedLock sl(theApp->getHashNodeDB()->getDBLock()); + static SqliteStatement pSt(theApp->getHashNodeDB()->getDB()->getSqliteDB(), + "SELECT ObjType,LedgerIndex,Object FROM CommittedObjects WHERE Hash = ?;"); LoadEvent::autoptr event(theApp->getJobQueue().getLoadEventAP(jtDISK, "HOS::retrieve")); - SqliteStatement pSt(theApp->getHashNodeDB()->getDB()->getSqliteDB(), sql.c_str()); + pSt.bind(1, hash.GetHex()); int ret = pSt.step(); if (pSt.isDone(ret)) { + pSt.reset(); mNegativeCache.add(hash); cLog(lsTRACE) << "HOS: " << hash <<" fetch: not in db"; return obj; @@ -203,6 +202,7 @@ HashedObject::pointer HashedObjectStore::retrieve(const uint256& hash) type = pSt.peekString(0); index = pSt.getUInt32(1); pSt.getBlob(2).swap(data); + pSt.reset(); } #else diff --git a/src/cpp/ripple/RippleState.cpp b/src/cpp/ripple/RippleState.cpp index bf542a9ae7..bebaee4504 100644 --- a/src/cpp/ripple/RippleState.cpp +++ b/src/cpp/ripple/RippleState.cpp @@ -3,33 +3,34 @@ AccountItem::pointer RippleState::makeItem(const uint160& accountID, SerializedLedgerEntry::ref ledgerEntry) { - if (!ledgerEntry || ledgerEntry->getType() != ltRIPPLE_STATE) return(AccountItem::pointer()); - RippleState* rs=new RippleState(ledgerEntry); + if (!ledgerEntry || ledgerEntry->getType() != ltRIPPLE_STATE) + return AccountItem::pointer(); + RippleState* rs = new RippleState(ledgerEntry); rs->setViewAccount(accountID); - return(AccountItem::pointer(rs)); + return AccountItem::pointer(rs); } RippleState::RippleState(SerializedLedgerEntry::ref ledgerEntry) : AccountItem(ledgerEntry), mValid(false), - mViewLowest(true) + mViewLowest(true), + + mLowLimit(ledgerEntry->getFieldAmount(sfLowLimit)), + mHighLimit(ledgerEntry->getFieldAmount(sfHighLimit)), + + mLowID(mLowLimit.getIssuer()), + mHighID(mHighLimit.getIssuer()), + + mBalance(ledgerEntry->getFieldAmount(sfBalance)) { mFlags = mLedgerEntry->getFieldU32(sfFlags); - mLowLimit = mLedgerEntry->getFieldAmount(sfLowLimit); - mHighLimit = mLedgerEntry->getFieldAmount(sfHighLimit); - - mLowID = mLowLimit.getIssuer(); - mHighID = mHighLimit.getIssuer(); - mLowQualityIn = mLedgerEntry->getFieldU32(sfLowQualityIn); mLowQualityOut = mLedgerEntry->getFieldU32(sfLowQualityOut); mHighQualityIn = mLedgerEntry->getFieldU32(sfHighQualityIn); mHighQualityOut = mLedgerEntry->getFieldU32(sfHighQualityOut); - mBalance = mLedgerEntry->getFieldAmount(sfBalance); - mValid = true; } diff --git a/src/cpp/ripple/RippleState.h b/src/cpp/ripple/RippleState.h index f38aa45d00..bdc30ce3b5 100644 --- a/src/cpp/ripple/RippleState.h +++ b/src/cpp/ripple/RippleState.h @@ -17,14 +17,17 @@ public: typedef boost::shared_ptr pointer; private: - uint32 mFlags; + bool mValid; + bool mViewLowest; - uint160 mLowID; - uint160 mHighID; + uint32 mFlags; STAmount mLowLimit; STAmount mHighLimit; + uint160 mLowID; + uint160 mHighID; + uint64 mLowQualityIn; uint64 mLowQualityOut; uint64 mHighQualityIn; @@ -32,9 +35,6 @@ private: STAmount mBalance; - bool mValid; - bool mViewLowest; - RippleState(SerializedLedgerEntry::ref ledgerEntry); // For accounts in a ledger public: diff --git a/src/cpp/ripple/SHAMapNodes.cpp b/src/cpp/ripple/SHAMapNodes.cpp index c1ef24948d..f171b7a098 100644 --- a/src/cpp/ripple/SHAMapNodes.cpp +++ b/src/cpp/ripple/SHAMapNodes.cpp @@ -124,8 +124,8 @@ SHAMapNode SHAMapNode::getChildNodeID(int m) const int SHAMapNode::selectBranch(const uint256& hash) const { // Which branch would contain the specified hash -#ifdef DEBUG - if (mDepth == 64) +#ifdef PARANOID + if (mDepth >= 64) { assert(false); return -1; diff --git a/src/cpp/ripple/SerializedObject.cpp b/src/cpp/ripple/SerializedObject.cpp index 497b2fb7db..2179d5d642 100644 --- a/src/cpp/ripple/SerializedObject.cpp +++ b/src/cpp/ripple/SerializedObject.cpp @@ -639,14 +639,18 @@ std::vector STObject::getFieldVL(SField::ref field) const return cf->getValue(); } -STAmount STObject::getFieldAmount(SField::ref field) const +static const STAmount defaultAmount; +const STAmount& STObject::getFieldAmount(SField::ref field) const { const SerializedType* rf = peekAtPField(field); - if (!rf) throw std::runtime_error("Field not found"); + if (!rf) + throw std::runtime_error("Field not found"); SerializedTypeID id = rf->getSType(); - if (id == STI_NOTPRESENT) return STAmount(); // optional field not present + if (id == STI_NOTPRESENT) + return defaultAmount; // optional field not present const STAmount *cf = dynamic_cast(rf); - if (!cf) throw std::runtime_error("Wrong field type"); + if (!cf) + throw std::runtime_error("Wrong field type"); return *cf; } diff --git a/src/cpp/ripple/SerializedObject.h b/src/cpp/ripple/SerializedObject.h index 67d76fabdf..97ab764f3a 100644 --- a/src/cpp/ripple/SerializedObject.h +++ b/src/cpp/ripple/SerializedObject.h @@ -130,7 +130,7 @@ public: RippleAddress getFieldAccount(SField::ref field) const; uint160 getFieldAccount160(SField::ref field) const; std::vector getFieldVL(SField::ref field) const; - STAmount getFieldAmount(SField::ref field) const; + const STAmount& getFieldAmount(SField::ref field) const; STPathSet getFieldPathSet(SField::ref field) const; STVector256 getFieldV256(SField::ref field) const; diff --git a/src/cpp/ripple/SerializedTypes.cpp b/src/cpp/ripple/SerializedTypes.cpp index 2e6cf1d472..e883a4a453 100644 --- a/src/cpp/ripple/SerializedTypes.cpp +++ b/src/cpp/ripple/SerializedTypes.cpp @@ -14,10 +14,9 @@ #include "TransactionErr.h" SETUP_LOG(); -DECLARE_INSTANCE(SerializedValue); -STAmount saZero(CURRENCY_ONE, ACCOUNT_ONE, 0); -STAmount saOne(CURRENCY_ONE, ACCOUNT_ONE, 1); +const STAmount saZero(CURRENCY_ONE, ACCOUNT_ONE, 0); +const STAmount saOne(CURRENCY_ONE, ACCOUNT_ONE, 1); SerializedType& SerializedType::operator=(const SerializedType& t) { diff --git a/src/cpp/ripple/SerializedTypes.h b/src/cpp/ripple/SerializedTypes.h index 4025274f3c..3a2e619bc0 100644 --- a/src/cpp/ripple/SerializedTypes.h +++ b/src/cpp/ripple/SerializedTypes.h @@ -44,9 +44,7 @@ static inline const uint160& get_u160_one() { return u160_one; } #define ACCOUNT_XRP get_u160_zero() #define ACCOUNT_ONE get_u160_one() // Used as a place holder. -DEFINE_INSTANCE(SerializedValue); - -class SerializedType : private IS_INSTANCE(SerializedValue) +class SerializedType { protected: SField::ptr fName; @@ -475,8 +473,8 @@ public: void roundSelf(); }; -extern STAmount saZero; -extern STAmount saOne; +extern const STAmount saZero; +extern const STAmount saOne; class STHash128 : public SerializedType { diff --git a/src/cpp/ripple/main.cpp b/src/cpp/ripple/main.cpp index 239d8301ac..284c7f5d11 100644 --- a/src/cpp/ripple/main.cpp +++ b/src/cpp/ripple/main.cpp @@ -221,6 +221,7 @@ int main(int argc, char* argv[]) { unit_test_main(init_unit_test, argc, argv); + InstanceType::shutdown(); return 0; } @@ -279,6 +280,7 @@ int main(int argc, char* argv[]) if (1 == iResult && !vm.count("quiet")) printHelp(desc); + InstanceType::shutdown(); return iResult; } // vim:ts=4