Avoid a whole lot of extraneous allocate/copy/free cycles when

serialized objects are constructed.
This commit is contained in:
JoelKatz
2012-05-18 21:18:49 -07:00
parent 5118f23956
commit 34e933cad8
3 changed files with 32 additions and 5 deletions

View File

@@ -22,7 +22,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256&
if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type"); if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mType = mFormat->t_type; mType = mFormat->t_type;
mVersion.setValue(type); mVersion.setValue(type);
mObject = STObject(mFormat->elements, sit); mObject.set(mFormat->elements, sit);
} }
SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : SerializedType("LedgerEntry"), mType(type) SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : SerializedType("LedgerEntry"), mType(type)
@@ -30,7 +30,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : SerializedT
mFormat = getLgrFormat(type); mFormat = getLgrFormat(type);
if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type"); if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mVersion.setValue(static_cast<uint16>(mFormat->t_type)); mVersion.setValue(static_cast<uint16>(mFormat->t_type));
mObject = STObject(mFormat->elements); mObject.set(mFormat->elements);
} }
std::string SerializedLedgerEntry::getFullText() const std::string SerializedLedgerEntry::getFullText() const

View File

@@ -65,7 +65,13 @@ std::auto_ptr<SerializedType> STObject::makeDeserializedObject(SerializedTypeID
return STUInt64::deserialize(sit, name); return STUInt64::deserialize(sit, name);
case STI_AMOUNT: case STI_AMOUNT:
return STAmount::deserialize(sit, name); {
std::cerr << "Deserializing " << name << std::endl;
std::auto_ptr<SerializedType> ptr = STAmount::deserialize(sit, name);
assert(!dynamic_cast<STAmount*>(&*ptr)->isZero());
std::cerr << "Got: " << ptr->getText() << std::endl;
return ptr;
}
case STI_HASH128: case STI_HASH128:
return STHash128::deserialize(sit, name); return STHash128::deserialize(sit, name);
@@ -90,8 +96,12 @@ std::auto_ptr<SerializedType> STObject::makeDeserializedObject(SerializedTypeID
} }
} }
STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mFlagIdx(-1) void STObject::set(SOElement* elem)
{ {
mData.empty();
mType.empty();
mFlagIdx = -1;
while (elem->e_id != STI_DONE) while (elem->e_id != STI_DONE)
{ {
if (elem->e_type == SOE_FLAGS) mFlagIdx = mType.size(); if (elem->e_type == SOE_FLAGS) mFlagIdx = mType.size();
@@ -104,8 +114,17 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
} }
} }
STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name), mFlagIdx(-1) STObject::STObject(SOElement* elem, const char *name) : SerializedType(name)
{ {
set(elem);
}
void STObject::set(SOElement* elem, SerializerIterator& sit)
{
mData.empty();
mType.empty();
mFlagIdx = -1;
int flags = -1; int flags = -1;
while (elem->e_id != STI_DONE) while (elem->e_id != STI_DONE)
{ {
@@ -142,6 +161,11 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
} }
} }
STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name), mFlagIdx(-1)
{
set(elem, sit);
}
std::string STObject::getFullText() const std::string STObject::getFullText() const
{ {
std::string ret; std::string ret;

View File

@@ -95,6 +95,9 @@ public:
STObject(SOElement *t, SerializerIterator& u, const char *n = NULL); STObject(SOElement *t, SerializerIterator& u, const char *n = NULL);
virtual ~STObject() { ; } virtual ~STObject() { ; }
void set(SOElement *t);
void set(SOElement *t, SerializerIterator& u);
int getLength() const; int getLength() const;
SerializedTypeID getSType() const { return STI_OBJECT; } SerializedTypeID getSType() const { return STI_OBJECT; }
virtual bool isEquivalent(const SerializedType& t) const; virtual bool isEquivalent(const SerializedType& t) const;