diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-01-27 15:39:54 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-01-27 15:39:54 +0000 |
commit | 53cc0b26079e41390588a39553355d34fa368ffc (patch) | |
tree | 3c31ba9bcc4ee6da035b2e113de85f0b9a0a2d89 /document | |
parent | 39a54ddf952cdaf898f2a2b6011a415610d08ffd (diff) |
Allow room for bringing allocation along to reduce the need to copy.
Diffstat (limited to 'document')
9 files changed, 34 insertions, 14 deletions
diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp index 6993596392c..fed4de67f28 100644 --- a/document/src/tests/documenttestcase.cpp +++ b/document/src/tests/documenttestcase.cpp @@ -36,7 +36,7 @@ TEST(DocumentTest, testSizeOf) EXPECT_EQ(32u, sizeof(vespalib::GrowableByteBuffer)); EXPECT_EQ(88ul, sizeof(IdString)); EXPECT_EQ(104ul, sizeof(DocumentId)); - EXPECT_EQ(232ul, sizeof(Document)); + EXPECT_EQ(240ul, sizeof(Document)); EXPECT_EQ(96ul, sizeof(StructFieldValue)); EXPECT_EQ(16ul, sizeof(StructuredFieldValue)); EXPECT_EQ(56ul, sizeof(SerializableArray)); diff --git a/document/src/vespa/document/datatype/structdatatype.cpp b/document/src/vespa/document/datatype/structdatatype.cpp index 7c308202e3b..ed17d22da59 100644 --- a/document/src/vespa/document/datatype/structdatatype.cpp +++ b/document/src/vespa/document/datatype/structdatatype.cpp @@ -118,7 +118,7 @@ StructDataType::addInheritedField(const Field& field) FieldValue::UP StructDataType::createFieldValue() const { - return FieldValue::UP(new StructFieldValue(*this)); + return std::make_unique<StructFieldValue>(*this); } const Field& diff --git a/document/src/vespa/document/fieldvalue/document.cpp b/document/src/vespa/document/fieldvalue/document.cpp index cc60234a093..fb79b516b90 100644 --- a/document/src/vespa/document/fieldvalue/document.cpp +++ b/document/src/vespa/document/fieldvalue/document.cpp @@ -11,6 +11,7 @@ #include <vespa/document/util/serializableexceptions.h> #include <vespa/document/fieldset/fieldsets.h> #include <vespa/document/util/bytebuffer.h> +#include <vespa/vespalib/data/databuffer.h> #include <vespa/vespalib/util/xmlstream.h> #include <vespa/vespalib/stllike/hash_map.hpp> #include <cassert> @@ -71,6 +72,7 @@ Document::Document() : StructuredFieldValue(*DataType::DOCUMENT), _id(), _fields(getType().getFieldsType()), + _backingBuffer(), _lastModified(0) { _fields.setDocumentType(getType()); @@ -80,6 +82,7 @@ Document::Document(const Document& rhs) : StructuredFieldValue(rhs), _id(rhs._id), _fields(rhs._fields), + _backingBuffer(), _lastModified(rhs._lastModified) {} @@ -87,6 +90,7 @@ Document::Document(const DataType &type, DocumentId documentId) : StructuredFieldValue(verifyDocumentType(&type)), _id(std::move(documentId)), _fields(getType().getFieldsType()), + _backingBuffer(), _lastModified(0) { _fields.setDocumentType(getType()); @@ -104,11 +108,29 @@ Document::Document(const DocumentTypeRepo& repo, vespalib::nbostream & is) : StructuredFieldValue(*DataType::DOCUMENT), _id(), _fields(static_cast<const DocumentType &>(getType()).getFieldsType()), + _backingBuffer(), _lastModified(0) { deserialize(repo, is); } +Document::Document(const DocumentTypeRepo& repo, vespalib::DataBuffer && backingBuffer) + : StructuredFieldValue(*DataType::DOCUMENT), + _id(), + _fields(static_cast<const DocumentType &>(getType()).getFieldsType()), + _backingBuffer(), + _lastModified(0) +{ + if (backingBuffer.referencesExternalData()) { + vespalib::nbostream is(backingBuffer.getData(), backingBuffer.getDataLen()); + deserialize(repo, is); + } else { + vespalib::nbostream_longlivedbuf is(backingBuffer.getData(), backingBuffer.getDataLen()); + deserialize(repo, is); + _backingBuffer = std::make_unique<vespalib::DataBuffer>(std::move(backingBuffer)); + } +} + Document::Document(Document &&) noexcept = default; Document::~Document() noexcept = default; @@ -117,6 +139,7 @@ Document::operator =(Document &&rhs) noexcept { assert( ! _cache && ! rhs._cache); _id = std::move(rhs._id); _fields = std::move(rhs._fields); + _backingBuffer = std::move(rhs._backingBuffer); _lastModified = rhs._lastModified; StructuredFieldValue::operator=(std::move(rhs)); return *this; @@ -124,15 +147,16 @@ Document::operator =(Document &&rhs) noexcept { Document & Document::operator =(const Document &rhs) { + if (this == &rhs) return *this; assert( ! _cache && ! rhs._cache); _id = rhs._id; _fields = rhs._fields; _lastModified = rhs._lastModified; StructuredFieldValue::operator=(rhs); + _backingBuffer.reset(); return *this; } - const DocumentType& Document::getType() const { return static_cast<const DocumentType &>(StructuredFieldValue::getType()); diff --git a/document/src/vespa/document/fieldvalue/document.h b/document/src/vespa/document/fieldvalue/document.h index b487aa067da..cb27ca5f338 100644 --- a/document/src/vespa/document/fieldvalue/document.h +++ b/document/src/vespa/document/fieldvalue/document.h @@ -19,6 +19,7 @@ #include <vespa/document/base/documentid.h> #include <vespa/document/base/field.h> +namespace vespalib { class DataBuffer; } namespace document { class TransactionGuard; @@ -29,6 +30,7 @@ private: DocumentId _id; StructFieldValue _fields; std::unique_ptr<StructuredCache> _cache; + std::unique_ptr<vespalib::DataBuffer> _backingBuffer; // To avoid having to return another container object out of docblocks // the meta data has been added to document. This will not be serialized @@ -50,6 +52,7 @@ public: Document & operator =(Document &&) noexcept; Document(const DataType &, DocumentId id); Document(const DocumentTypeRepo& repo, vespalib::nbostream& stream); + Document(const DocumentTypeRepo& repo, vespalib::DataBuffer && buffer); ~Document() noexcept override; void setRepo(const DocumentTypeRepo & repo); diff --git a/document/src/vespa/document/fieldvalue/serializablearray.cpp b/document/src/vespa/document/fieldvalue/serializablearray.cpp index be5001c6f5b..3664554ffcc 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.cpp +++ b/document/src/vespa/document/fieldvalue/serializablearray.cpp @@ -26,8 +26,6 @@ public: } -SerializableArray::SerializableArray() = default; - SerializableArray::SerializableArray(EntryMap entries, ByteBuffer buffer, CompressionConfig::Type comp_type, uint32_t uncompressed_length) : _entries(std::move(entries)), diff --git a/document/src/vespa/document/fieldvalue/serializablearray.h b/document/src/vespa/document/fieldvalue/serializablearray.h index 80edc8a810c..30777d7e337 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.h +++ b/document/src/vespa/document/fieldvalue/serializablearray.h @@ -84,7 +84,7 @@ public: using CompressionConfig = vespalib::compression::CompressionConfig; using CompressionInfo = vespalib::compression::CompressionInfo; - SerializableArray(); + SerializableArray() = default; SerializableArray(const SerializableArray&); SerializableArray& operator=(const SerializableArray&); SerializableArray(SerializableArray &&) noexcept; diff --git a/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp b/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp index 755186edd4c..2541a253c80 100644 --- a/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp @@ -30,12 +30,6 @@ StructuredFieldValue::Iterator::Iterator(const StructuredFieldValue& owner, cons { } -StructuredFieldValue::StructuredFieldValue(const DataType &type) - : FieldValue(), - _type(&type) -{ -} - void StructuredFieldValue::setFieldValue(const Field & field, const FieldValue & value) { diff --git a/document/src/vespa/document/fieldvalue/structuredfieldvalue.h b/document/src/vespa/document/fieldvalue/structuredfieldvalue.h index 55b964147be..867aabca537 100644 --- a/document/src/vespa/document/fieldvalue/structuredfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/structuredfieldvalue.h @@ -44,7 +44,7 @@ class StructuredFieldValue : public FieldValue virtual StructuredCache * getCache() const { return nullptr; } protected: - VESPA_DLL_LOCAL StructuredFieldValue(const DataType &type); + StructuredFieldValue(const DataType &type) : FieldValue(), _type(&type) {} /** Called from Document when deserializing alters type. */ virtual void setType(const DataType& type) { _type = &type; } diff --git a/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp b/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp index 039df466872..a28a65c3a3b 100644 --- a/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp +++ b/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp @@ -279,7 +279,8 @@ void readFieldInfo(nbostream& input, SerializableArray::EntryMap & field_info) { for (size_t i = 0; i < field_count; ++i) { const uint32_t id = getInt1_4Bytes(input); const uint32_t size = getInt2_4_8Bytes(input); - field_info.emplace_back(id, size, offset); + field_info.emplace_back(id, + size, offset); offset += size; } } |