From e3b45718314b9dc87dd06fd9a67d33b6c32622a2 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 21 Feb 2013 10:29:06 -0800 Subject: [PATCH] Cleanup of STObject::setType. Avoid some excess allocations. --- src/cpp/ripple/SerializedObject.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cpp/ripple/SerializedObject.cpp b/src/cpp/ripple/SerializedObject.cpp index fc5e710c2..cff7a3272 100644 --- a/src/cpp/ripple/SerializedObject.cpp +++ b/src/cpp/ripple/SerializedObject.cpp @@ -147,16 +147,18 @@ void STObject::set(const std::vector& type) bool STObject::setType(const std::vector &type) { - boost::ptr_vector newData; + boost::ptr_vector newData(type.size()); bool valid = true; mType.clear(); + mType.reserve(type.size()); + BOOST_FOREACH(SOElement::ref elem, type) { bool match = false; for (boost::ptr_vector::iterator it = mData.begin(); it != mData.end(); ++it) if (it->getFName() == elem->e_field) - { + { // matching entry, move to new vector match = true; newData.push_back(mData.release(it).release()); if ((elem->flags == SOE_DEFAULT) && it->isDefault()) @@ -169,7 +171,7 @@ bool STObject::setType(const std::vector &type) } if (!match) - { + { // no match found if (elem->flags == SOE_REQUIRED) { cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid missing " @@ -181,18 +183,17 @@ bool STObject::setType(const std::vector &type) mType.push_back(elem); } - if (mData.size() != 0) - { - BOOST_FOREACH(const SerializedType& t, mData) + + BOOST_FOREACH(const SerializedType& t, mData) + { // Anything left over must be discardable + if (!t.getFName().isDiscardable()) { - if (!t.getFName().isDiscardable()) - { - cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid leftover " - << t.getFName().getName(); - valid = false; - } + cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid leftover " + << t.getFName().getName(); + valid = false; } } + mData.swap(newData); return valid; }