summaryrefslogtreecommitdiffstats
path: root/storageapi
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@verizonmedia.com>2019-04-05 14:38:15 +0000
committerTor Brede Vekterli <vekterli@verizonmedia.com>2019-04-05 14:38:15 +0000
commit75cb5f0dc21e54be37fd5e0bada2fb93cac0b049 (patch)
tree8e3a77e8efb947c13eb26493f7c16cfaac0f2ca6 /storageapi
parentb2eb61ee0eec53d41a112c3679478c725bfc67aa (diff)
Faster protobuf serialization without `std::string` indirection
Diffstat (limited to 'storageapi')
-rw-r--r--storageapi/src/tests/mbusprot/storageprotocoltest.cpp10
-rw-r--r--storageapi/src/vespa/storageapi/mbusprot/protocolserialization7.cpp28
2 files changed, 25 insertions, 13 deletions
diff --git a/storageapi/src/tests/mbusprot/storageprotocoltest.cpp b/storageapi/src/tests/mbusprot/storageprotocoltest.cpp
index 5a5d53ce950..fd375fd6e74 100644
--- a/storageapi/src/tests/mbusprot/storageprotocoltest.cpp
+++ b/storageapi/src/tests/mbusprot/storageprotocoltest.cpp
@@ -16,6 +16,7 @@
#include <vespa/document/test/make_bucket_space.h>
#include <vespa/vespalib/util/growablebytebuffer.h>
#include <vespa/vespalib/objects/nbostream.h>
+
#include <iomanip>
#include <sstream>
@@ -35,6 +36,15 @@ using document::test::makeBucketSpace;
using storage::lib::ClusterState;
using vespalib::string;
+namespace vespalib {
+
+// Needed for GTest to properly understand how to print Version values.
+void PrintTo(const vespalib::Version& v, std::ostream* os) {
+ *os << v.toString();
+}
+
+}
+
namespace storage::api {
struct StorageProtocolTest : TestWithParam<vespalib::Version> {
diff --git a/storageapi/src/vespa/storageapi/mbusprot/protocolserialization7.cpp b/storageapi/src/vespa/storageapi/mbusprot/protocolserialization7.cpp
index 979e4162a7a..1692a1fcfcd 100644
--- a/storageapi/src/vespa/storageapi/mbusprot/protocolserialization7.cpp
+++ b/storageapi/src/vespa/storageapi/mbusprot/protocolserialization7.cpp
@@ -97,12 +97,13 @@ void write_request_header(vespalib::GrowableByteBuffer& buf, const api::StorageC
hdr.set_source_index(cmd.getSourceIndex());
hdr.set_loadtype_id(cmd.getLoadType().getId());
- char dest[128]; // Only primitive fields, should be plenty large enough.
+ uint8_t dest[128]; // Only primitive fields, should be plenty large enough.
auto encoded_size = static_cast<uint32_t>(hdr.ByteSizeLong());
- bool ok = hdr.SerializeToArray(dest, sizeof(dest));
- assert(ok); // TODO
+ assert(encoded_size <= sizeof(dest));
+ [[maybe_unused]] bool ok = hdr.SerializeWithCachedSizesToArray(dest);
+ assert(ok);
buf.putInt(encoded_size);
- buf.putBytes(dest, encoded_size);
+ buf.putBytes(reinterpret_cast<const char*>(dest), encoded_size);
}
void write_response_header(vespalib::GrowableByteBuffer& buf, const api::StorageReply& reply) {
@@ -115,11 +116,12 @@ void write_response_header(vespalib::GrowableByteBuffer& buf, const api::Storage
hdr.set_message_id(reply.getMsgId());
hdr.set_priority(reply.getPriority());
- std::string encoded; // TODO wrap in zero copy buffers!
- bool ok = hdr.SerializeToString(&encoded);
- assert(ok); // TODO
- buf.putInt(static_cast<uint32_t>(encoded.size()));
- buf.putBytes(encoded.data(), static_cast<uint32_t>(encoded.size()));
+ const auto header_size = hdr.ByteSizeLong();
+ buf.putInt(static_cast<uint32_t>(header_size));
+
+ auto* dest_buf = reinterpret_cast<uint8_t*>(buf.allocate(header_size));
+ [[maybe_unused]] bool ok = hdr.SerializeWithCachedSizesToArray(dest_buf);
+ assert(ok);
}
void decode_request_header(document::ByteBuffer& buf, protobuf::RequestHeader& hdr) {
@@ -163,10 +165,10 @@ public:
void encode() {
assert(_proto_obj != nullptr);
- std::string encoded; // TODO wrap in zero copy buffers!
- bool ok = _proto_obj->SerializeToString(&encoded);
- assert(ok); // TODO
- _out_buf.putBytes(encoded.data(), encoded.size());
+ const auto sz = _proto_obj->ByteSizeLong();
+ auto* buf = reinterpret_cast<uint8_t*>(_out_buf.allocate(sz));
+ [[maybe_unused]] bool ok = _proto_obj->SerializeWithCachedSizesToArray(buf);
+ assert(ok);
_proto_obj = nullptr;
}
protected: