GenericValue: improve copying performance

The GenericValue "copy" constructor (with Allocator) uses a temporary
GenericDocument object to perform the deep copying with the provided
allocator.  This leads to the temporary allocation of the `Stack`
memory, even in case of shallow values (numbers, etc.).

This patch improves the performance of this operation by only resorting
the the SAX Handler implementation in case of Array or Object values.
This commit is contained in:
Philipp A. Hartmann 2014-11-26 23:11:00 +01:00
parent 28e55ee24d
commit 2aab79207e

View File

@ -1896,10 +1896,27 @@ template <typename SourceAllocator>
inline inline
GenericValue<Encoding,Allocator>::GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator) GenericValue<Encoding,Allocator>::GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator)
{ {
switch (rhs.GetType()) {
case kObjectType:
case kArrayType: { // perform deep copy via SAX Handler
GenericDocument<Encoding,Allocator> d(&allocator); GenericDocument<Encoding,Allocator> d(&allocator);
rhs.Accept(d); rhs.Accept(d);
RawAssign(*d.stack_.template Pop<GenericValue>(1)); RawAssign(*d.stack_.template Pop<GenericValue>(1));
} }
break;
case kStringType:
if (rhs.flags_ == kConstStringFlag) {
flags_ = rhs.flags_;
data_ = *reinterpret_cast<const Data*>(&rhs.data_);
} else {
SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
}
break;
default: // kNumberType, kTrueType, kFalseType, kNullType
flags_ = rhs.flags_;
data_ = *reinterpret_cast<const Data*>(&rhs.data_);
}
}
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END