diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-01-22 22:43:48 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-01-23 05:56:45 +0000 |
commit | 753c6869ac764e8b1463ba214980c82401479a88 (patch) | |
tree | 58c94b8acde28cfff294b1050aa7f7607a20482c /document | |
parent | 9f438cdcdae81a9fd55dc370a42af85b275a934c (diff) |
Remove ByteBuffer indirection.
Diffstat (limited to 'document')
8 files changed, 50 insertions, 46 deletions
diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp index b24b1d97788..9990c00f57e 100644 --- a/document/src/tests/documenttestcase.cpp +++ b/document/src/tests/documenttestcase.cpp @@ -36,10 +36,10 @@ TEST(DocumentTest, testSizeOf) EXPECT_EQ(32u, sizeof(vespalib::GrowableByteBuffer)); EXPECT_EQ(88ul, sizeof(IdString)); EXPECT_EQ(104ul, sizeof(DocumentId)); - EXPECT_EQ(224ul, sizeof(Document)); - EXPECT_EQ(88ul, sizeof(StructFieldValue)); + EXPECT_EQ(256ul, sizeof(Document)); + EXPECT_EQ(120ul, sizeof(StructFieldValue)); EXPECT_EQ(24ul, sizeof(StructuredFieldValue)); - EXPECT_EQ(40ul, sizeof(SerializableArray)); + EXPECT_EQ(72ul, sizeof(SerializableArray)); } TEST(DocumentTest, testFieldPath) diff --git a/document/src/vespa/document/fieldvalue/serializablearray.cpp b/document/src/vespa/document/fieldvalue/serializablearray.cpp index 963fbc4dbe9..d97f653615e 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.cpp +++ b/document/src/vespa/document/fieldvalue/serializablearray.cpp @@ -17,7 +17,7 @@ namespace document { namespace serializablearray { -using BufferMapT = vespalib::hash_map<int, ByteBuffer::UP>; +using BufferMapT = vespalib::hash_map<int, ByteBuffer>; class BufferMap : public BufferMapT { public: @@ -28,9 +28,10 @@ public: SerializableArray::SerializableArray() = default; -SerializableArray::SerializableArray(EntryMap entries, ByteBuffer::UP buffer, +SerializableArray::SerializableArray(EntryMap entries, ByteBuffer buffer, CompressionConfig::Type comp_type, uint32_t uncompressed_length) : _entries(std::move(entries)), + _uncompSerData(), _unlikely() { @@ -63,7 +64,7 @@ ensure(std::unique_ptr<T> &owned) { SerializableArray::Unlikely::Unlikely() : _owned(), - _compSerData(), + _compSerData(nullptr, 0), _serializedCompression(CompressionConfig::NONE), _uncompressedLength(0) { } @@ -71,22 +72,22 @@ SerializableArray::Unlikely::~Unlikely() = default; SerializableArray::Unlikely::Unlikely(const Unlikely & rhs) : _owned(), - _compSerData(rhs._compSerData ? new ByteBuffer(*rhs._compSerData) : nullptr), + _compSerData(rhs._compSerData), _serializedCompression(rhs._serializedCompression), _uncompressedLength(rhs._uncompressedLength) { } SerializableArray::SerializableArray(const SerializableArray& rhs) : _entries(rhs._entries), - _unlikely(rhs._unlikely ? new Unlikely(*rhs._unlikely) : nullptr), - _uncompSerData(rhs._uncompSerData ? new ByteBuffer(*rhs._uncompSerData) : nullptr) + _uncompSerData(rhs._uncompSerData), + _unlikely(rhs._unlikely ? new Unlikely(*rhs._unlikely) : nullptr) { for (size_t i(0); i < _entries.size(); i++) { Entry & e(_entries[i]); if (e.hasBuffer()) { // Pointing to a buffer in the _owned structure. - ByteBuffer::UP buf(ByteBuffer::copyBuffer(e.getBuffer(_uncompSerData.get()), e.size())); - e.setBuffer(buf->getBuffer()); + ByteBuffer buf(ByteBuffer::copyBuffer(e.getBuffer(&_uncompSerData), e.size())); + e.setBuffer(buf.getBuffer()); ensure(_unlikely->_owned)[e.id()] = std::move(buf); } else { // If not it is relative to the buffer _uncompSerData, and hence it is valid as is. @@ -104,7 +105,7 @@ SerializableArray::operator=(const SerializableArray &rhs) void SerializableArray::clear() { _entries.clear(); - _uncompSerData.reset(); + _uncompSerData = ByteBuffer(nullptr, 0); _unlikely.reset(); } @@ -112,15 +113,15 @@ void SerializableArray::invalidate() { if (_unlikely) { - _unlikely->_compSerData.reset(); + _unlikely->_compSerData = ByteBuffer(nullptr, 0);; } } void -SerializableArray::set(int id, ByteBuffer::UP buffer) +SerializableArray::set(int id, ByteBuffer buffer) { maybeDecompress(); - Entry e(id, buffer->getRemaining(), buffer->getBuffer()); + Entry e(id, buffer.getRemaining(), buffer.getBuffer()); ensure(ensure(_unlikely)._owned)[id] = std::move(buffer); auto it = find(id); if (it == _entries.end()) { @@ -133,7 +134,7 @@ SerializableArray::set(int id, ByteBuffer::UP buffer) void SerializableArray::set(int id, const char* value, int len) { - set(id, std::unique_ptr<ByteBuffer>(ByteBuffer::copyBuffer(value,len))); + set(id, ByteBuffer::copyBuffer(value,len)); } SerializableArray::EntryMap::const_iterator @@ -163,7 +164,7 @@ SerializableArray::get(int id) const if (found != _entries.end()) { const Entry& entry = *found; - buf = vespalib::ConstBufferRef(entry.getBuffer(_uncompSerData.get()), entry.size()); + buf = vespalib::ConstBufferRef(entry.getBuffer(&_uncompSerData), entry.size()); } } else { // should we clear all or what? @@ -204,18 +205,18 @@ SerializableArray::deCompress() // throw (DeserializeException) using vespalib::compression::decompress; // will only do this once - assert(_unlikely && _unlikely->_compSerData); - assert(!_uncompSerData); + assert(_unlikely && (_unlikely->_compSerData.getRemaining() != 0)); + assert(_uncompSerData.getRemaining() == 0); assert(CompressionConfig::isCompressed(_unlikely->_serializedCompression)); uint32_t uncompressedLength = _unlikely->_uncompressedLength; - auto newSerialization = std::make_unique<ByteBuffer>(vespalib::alloc::Alloc::alloc(uncompressedLength), uncompressedLength); - vespalib::DataBuffer unCompressed(newSerialization->getBuffer(), newSerialization->getLength()); + ByteBuffer newSerialization(vespalib::alloc::Alloc::alloc(uncompressedLength), uncompressedLength); + vespalib::DataBuffer unCompressed(newSerialization.getBuffer(), newSerialization.getLength()); unCompressed.clear(); try { decompress(_unlikely->_serializedCompression, uncompressedLength, - vespalib::ConstBufferRef(_unlikely->_compSerData->getBufferAtPos(), _unlikely->_compSerData->getRemaining()), + vespalib::ConstBufferRef(_unlikely->_compSerData.getBufferAtPos(), _unlikely->_compSerData.getRemaining()), unCompressed, false); } catch (const std::runtime_error & e) { @@ -227,19 +228,19 @@ SerializableArray::deCompress() // throw (DeserializeException) if (unCompressed.getDataLen() != (size_t)uncompressedLength) { throw DeserializeException( make_string("Did not decompress to the expected length: had %u, wanted %d, got %zu", - _unlikely->_compSerData->getRemaining(), uncompressedLength, unCompressed.getDataLen()), + _unlikely->_compSerData.getRemaining(), uncompressedLength, unCompressed.getDataLen()), VESPA_STRLOC); } - assert(newSerialization->getBuffer() == unCompressed.getData()); + assert(newSerialization.getBuffer() == unCompressed.getData()); _uncompSerData = std::move(newSerialization); - LOG_ASSERT(uncompressedLength == _uncompSerData->getRemaining()); + LOG_ASSERT(uncompressedLength == _uncompSerData.getRemaining()); } vespalib::compression::CompressionInfo SerializableArray::getCompressionInfo() const { return _unlikely - ? CompressionInfo(_unlikely->_uncompressedLength, _unlikely->_compSerData->getRemaining()) - : CompressionInfo(_uncompSerData->getRemaining(), CompressionConfig::NONE); + ? CompressionInfo(_unlikely->_uncompressedLength, _unlikely->_compSerData.getRemaining()) + : CompressionInfo(_uncompSerData.getRemaining(), CompressionConfig::NONE); } const char * diff --git a/document/src/vespa/document/fieldvalue/serializablearray.h b/document/src/vespa/document/fieldvalue/serializablearray.h index d4e9a4f3f5e..7ba6df6af73 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.h +++ b/document/src/vespa/document/fieldvalue/serializablearray.h @@ -19,6 +19,7 @@ #include <vespa/vespalib/util/compressionconfig.h> #include <vespa/vespalib/util/buffer.h> #include <vespa/vespalib/util/memory.h> +#include <vespa/document/util/bytebuffer.h> #include <vector> #define VESPA_DLL_LOCAL __attribute__ ((visibility("hidden"))) @@ -88,7 +89,7 @@ public: SerializableArray& operator=(const SerializableArray&); SerializableArray(SerializableArray &&) noexcept; SerializableArray& operator=(SerializableArray &&) noexcept; - SerializableArray(EntryMap entries, ByteBufferUP buffer, + SerializableArray(EntryMap entries, ByteBuffer buffer, CompressionConfig::Type comp_type, uint32_t uncompressed_length); ~SerializableArray(); @@ -102,7 +103,7 @@ public: void set(int id, const char* value, int len); /** Stores a value in the array. */ - void set(int id, ByteBufferUP buffer); + void set(int id, ByteBuffer buffer); /** * Gets a value from the array. This is the faster version of the above. @@ -137,14 +138,14 @@ public: const ByteBuffer* getSerializedBuffer() const { return CompressionConfig::isCompressed(getCompression()) - ? _unlikely->_compSerData.get() - : _uncompSerData.get(); + ? &_unlikely->_compSerData + : &_uncompSerData; } const EntryMap & getEntries() const { return _entries; } private: bool shouldDecompress() const { - return _unlikely && _unlikely->_compSerData && !_uncompSerData; + return _unlikely && (_unlikely->_compSerData.getRemaining() != 0) && (_uncompSerData.getBuffer() == 0); } bool maybeDecompressAndCatch() const { if ( shouldDecompress() ) { @@ -167,16 +168,16 @@ private: Unlikely(const Unlikely &); ~Unlikely(); std::unique_ptr<serializablearray::BufferMap> _owned; - ByteBufferUP _compSerData; + ByteBuffer _compSerData; CompressionConfig::Type _serializedCompression; uint32_t _uncompressedLength; }; /** Contains the stored attributes, with reference to the real data.. */ EntryMap _entries; + /** Data we deserialized from, if applicable. */ + ByteBuffer _uncompSerData; std::unique_ptr<Unlikely> _unlikely; - /** Data we deserialized from, if applicable. */ - ByteBufferUP _uncompSerData; VESPA_DLL_LOCAL void invalidate(); VESPA_DLL_LOCAL EntryMap::const_iterator find(int id) const; diff --git a/document/src/vespa/document/fieldvalue/structfieldvalue.cpp b/document/src/vespa/document/fieldvalue/structfieldvalue.cpp index 55c389c503e..703650f7325 100644 --- a/document/src/vespa/document/fieldvalue/structfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/structfieldvalue.cpp @@ -59,7 +59,7 @@ void StructFieldValue::lazyDeserialize(const FixedTypeRepo &repo, uint16_t version, SerializableArray::EntryMap && fm, - ByteBuffer::UP buffer, + ByteBuffer buffer, CompressionConfig::Type comp_type, int32_t uncompressed_length) { @@ -217,7 +217,7 @@ StructFieldValue::setFieldValue(const Field& field, FieldValue::UP value) std::unique_ptr<ByteBuffer> serialized = serializeDoc(*value); - _fields.set(fieldId, std::move(serialized)); + _fields.set(fieldId, std::move(*serialized)); _hasChanged = true; } diff --git a/document/src/vespa/document/fieldvalue/structfieldvalue.h b/document/src/vespa/document/fieldvalue/structfieldvalue.h index 8c9d2cc6bb2..e2784189e04 100644 --- a/document/src/vespa/document/fieldvalue/structfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/structfieldvalue.h @@ -51,7 +51,7 @@ public: void lazyDeserialize(const FixedTypeRepo &repo, uint16_t version, SerializableArray::EntryMap && fields, - std::unique_ptr<ByteBuffer> buffer, + ByteBuffer buffer, CompressionConfig::Type comp_type, int32_t uncompressed_length); diff --git a/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp b/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp index 9fa2fb5c005..039df466872 100644 --- a/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp +++ b/document/src/vespa/document/serialization/vespadocumentdeserializer.cpp @@ -306,8 +306,8 @@ void VespaDocumentDeserializer::readStructNoReset(StructFieldValue &value) { } if (data_size > 0) { - ByteBuffer::UP buffer(_stream.isLongLivedBuffer() - ? new ByteBuffer(_stream.peek(), data_size) + ByteBuffer buffer(_stream.isLongLivedBuffer() + ? ByteBuffer(_stream.peek(), data_size) : ByteBuffer::copyBuffer(_stream.peek(), data_size)); if (value.getFields().empty()) { LOG(spam, "Lazy deserializing into %s with _version %u", diff --git a/document/src/vespa/document/util/bytebuffer.cpp b/document/src/vespa/document/util/bytebuffer.cpp index 0fbc6ca1c63..e1999c34288 100644 --- a/document/src/vespa/document/util/bytebuffer.cpp +++ b/document/src/vespa/document/util/bytebuffer.cpp @@ -93,14 +93,15 @@ ByteBuffer::ByteBuffer(const ByteBuffer& rhs) : ByteBuffer::~ByteBuffer() = default; -ByteBuffer* ByteBuffer::copyBuffer(const char* buffer, uint32_t len) +ByteBuffer +ByteBuffer::copyBuffer(const char* buffer, uint32_t len) { if (buffer && len) { Alloc newBuf = Alloc::alloc(len); memcpy(newBuf.get(), buffer, len); - return new ByteBuffer(std::move(newBuf), len); + return ByteBuffer(std::move(newBuf), len); } else { - return nullptr; + return ByteBuffer(); } } diff --git a/document/src/vespa/document/util/bytebuffer.h b/document/src/vespa/document/util/bytebuffer.h index 17ab0322a7d..3f64f51a88a 100644 --- a/document/src/vespa/document/util/bytebuffer.h +++ b/document/src/vespa/document/util/bytebuffer.h @@ -26,8 +26,9 @@ public: ByteBuffer(const ByteBuffer &); ByteBuffer& operator=(const ByteBuffer &) = delete; ByteBuffer(ByteBuffer &&) = default; - ByteBuffer& operator=(ByteBuffer &&) = delete; + ByteBuffer& operator=(ByteBuffer &&) = default; + ByteBuffer() : ByteBuffer(nullptr, 0) { } ~ByteBuffer(); /** @@ -56,7 +57,7 @@ public: * @return Returns a newly created bytebuffer object, or nullptr * if buffer was nullptr, or len was <=0. */ - static ByteBuffer* copyBuffer(const char* buffer, uint32_t len); + static ByteBuffer copyBuffer(const char* buffer, uint32_t len); /** @return Returns the buffer pointed to by this object (at position 0) */ const char* getBuffer() const { return _buffer; } @@ -110,7 +111,7 @@ private: void incPosNoCheck(uint32_t pos) { _pos += pos; } const char * _buffer; - const uint32_t _len; + uint32_t _len; uint32_t _pos; vespalib::alloc::Alloc _ownedBuffer; }; |