Commit b068df71 authored by Jakob Otto's avatar Jakob Otto

Reuse buffers for serializing

parent 3cdf5f18
...@@ -136,7 +136,8 @@ public: ...@@ -136,7 +136,8 @@ public:
// nop // nop
} }
static expected<buffer_type> serialize(actor_system& sys, const message& x); static error_code<sec> serialize(actor_system& sys, const message& x,
std::vector<byte>& buf);
// -- utility functions ------------------------------------------------------ // -- utility functions ------------------------------------------------------
......
...@@ -44,11 +44,9 @@ public: ...@@ -44,11 +44,9 @@ public:
using super = socket_manager; using super = socket_manager;
/// Represents either an error or a serialized payload.
using maybe_buffer = expected<std::vector<byte>>;
/// A function type for serializing message payloads. /// A function type for serializing message payloads.
using serialize_fun_type = maybe_buffer (*)(actor_system&, const message&); using serialize_fun_type = error_code<sec> (*)(actor_system&, const message&,
std::vector<byte>& buf);
// -- constructors, destructors, and assignment operators -------------------- // -- constructors, destructors, and assignment operators --------------------
......
...@@ -60,9 +60,9 @@ error application::write_message( ...@@ -60,9 +60,9 @@ error application::write_message(
// TODO: valid? // TODO: valid?
return none; return none;
} }
auto payload_buf = serialize(system(), ptr->msg->content()); auto payload_buf = writer.next_payload_buffer();
if (!payload_buf) if (auto err = serialize(system(), ptr->msg->content(), payload_buf))
return payload_buf.error(); return err;
if (src != nullptr) { if (src != nullptr) {
auto src_id = src->id(); auto src_id = src->id();
system().registry().put(src_id, src); system().registry().put(src_id, src);
...@@ -75,10 +75,10 @@ error application::write_message( ...@@ -75,10 +75,10 @@ error application::write_message(
auto hdr = writer.next_header_buffer(); auto hdr = writer.next_header_buffer();
to_bytes(header{message_type::actor_message, to_bytes(header{message_type::actor_message,
static_cast<uint32_t>(payload_prefix_buf.size() static_cast<uint32_t>(payload_prefix_buf.size()
+ payload_buf->size()), + payload_buf.size()),
ptr->msg->mid.integer_value()}, ptr->msg->mid.integer_value()},
hdr); hdr);
writer.write_packet(hdr, payload_prefix_buf, *payload_buf); writer.write_packet(hdr, payload_prefix_buf, payload_buf);
return none; return none;
} }
...@@ -121,13 +121,10 @@ void application::local_actor_down(packet_writer& writer, actor_id id, ...@@ -121,13 +121,10 @@ void application::local_actor_down(packet_writer& writer, actor_id id,
writer.write_packet(hdr, payload); writer.write_packet(hdr, payload);
} }
expected<std::vector<byte>> application::serialize(actor_system& sys, error_code<sec> application::serialize(actor_system& sys, const message& x,
const message& x) { std::vector<byte>& buf) {
std::vector<byte> result; binary_serializer sink{sys, buf};
binary_serializer sink{sys, result}; return x.save(sink);
if (auto err = x.save(sink))
return err.value();
return result;
} }
strong_actor_ptr application::resolve_local_path(string_view path) { strong_actor_ptr application::resolve_local_path(string_view path) {
......
...@@ -119,13 +119,13 @@ public: ...@@ -119,13 +119,13 @@ public:
return none; return none;
} }
template <class Transport> template <class Parent>
void write_message(Transport& transport, void write_message(Parent& parent,
std::unique_ptr<endpoint_manager_queue::message> msg) { std::unique_ptr<endpoint_manager_queue::message> msg) {
if (auto payload = serialize(transport.system(), msg->msg->payload)) auto payload_buf = parent.next_payload_buffer();
transport.write_packet(*payload); if (auto err = serialize(parent.system(), msg->msg->payload, payload_buf))
else CAF_FAIL("serializing failed: " << err);
CAF_FAIL("serializing failed: " << payload.error()); parent.write_packet(payload_buf);
} }
template <class Parent> template <class Parent>
...@@ -167,12 +167,10 @@ public: ...@@ -167,12 +167,10 @@ public:
CAF_FAIL("handle_error called: " << to_string(sec)); CAF_FAIL("handle_error called: " << to_string(sec));
} }
static expected<buffer_type> serialize(actor_system& sys, const message& x) { static error_code<sec> serialize(actor_system& sys, const message& x,
buffer_type result; std::vector<byte>& buf) {
binary_serializer sink{sys, result}; binary_serializer sink{sys, buf};
if (auto err = x.save(sink)) return x.save(sink);
return err.value();
return result;
} }
private: private:
......
...@@ -60,13 +60,10 @@ struct fixture : test_coordinator_fixture<>, host_fixture { ...@@ -60,13 +60,10 @@ struct fixture : test_coordinator_fixture<>, host_fixture {
class dummy_application { class dummy_application {
public: public:
static expected<std::vector<byte>> serialize(actor_system& sys, static error_code<sec> serialize(actor_system& sys, const message& x,
const message& x) { std::vector<byte>& buf) {
std::vector<byte> result; binary_serializer sink{sys, buf};
binary_serializer sink{sys, result}; return x.save(sink);
if (auto err = x.save(sink))
return err.value();
return result;
} }
template <class Parent> template <class Parent>
...@@ -74,13 +71,13 @@ public: ...@@ -74,13 +71,13 @@ public:
return none; return none;
} }
template <class Transport> template <class Parent>
void write_message(Transport& transport, void write_message(Parent& parent,
std::unique_ptr<endpoint_manager_queue::message> msg) { std::unique_ptr<endpoint_manager_queue::message> msg) {
if (auto payload = serialize(transport.system(), msg->msg->payload)) auto payload_buf = parent.next_payload_buffer();
transport.write_packet(*payload); if (auto err = serialize(parent.system(), msg->msg->payload, payload_buf))
else CAF_FAIL("serializing failed: " << err);
CAF_FAIL("serializing failed: " << payload.error()); parent.write_packet(payload_buf);
} }
template <class Parent> template <class Parent>
...@@ -119,9 +116,9 @@ class dummy_application_factory { ...@@ -119,9 +116,9 @@ class dummy_application_factory {
public: public:
using application_type = dummy_application; using application_type = dummy_application;
static expected<std::vector<byte>> serialize(actor_system& sys, static error_code<sec> serialize(actor_system& sys, const message& x,
const message& x) { std::vector<byte>& buf) {
return dummy_application::serialize(sys, x); return dummy_application::serialize(sys, x, buf);
} }
template <class Parent> template <class Parent>
......
...@@ -62,13 +62,10 @@ struct fixture : test_coordinator_fixture<>, host_fixture { ...@@ -62,13 +62,10 @@ struct fixture : test_coordinator_fixture<>, host_fixture {
class dummy_application { class dummy_application {
public: public:
static expected<std::vector<byte>> serialize(actor_system& sys, static error_code<sec> serialize(actor_system& sys, const message& x,
const message& x) { std::vector<byte>& buf) {
std::vector<byte> result; binary_serializer sink{sys, buf};
binary_serializer sink{sys, result}; return x.save(sink);
if (auto err = x.save(sink))
return err.value();
return result;
} }
}; };
...@@ -106,12 +103,12 @@ public: ...@@ -106,12 +103,12 @@ public:
template <class Manager> template <class Manager>
bool handle_write_event(Manager& mgr) { bool handle_write_event(Manager& mgr) {
auto sf = mgr.serialize_fun();
for (auto x = mgr.next_message(); x != nullptr; x = mgr.next_message()) { for (auto x = mgr.next_message(); x != nullptr; x = mgr.next_message()) {
if (auto payload = application_type::serialize(mgr.system(), std::vector<byte> payload_buf;
x->msg->payload)) if (auto err = sf(mgr.system(), x->msg->payload, payload_buf))
buf_.insert(buf_.end(), payload->begin(), payload->end()); CAF_FAIL("serializing failed: " << err);
else buf_.insert(buf_.end(), payload_buf.begin(), payload_buf.end());
CAF_FAIL("serializing failed: " << payload.error());
} }
auto res = write(handle_, buf_); auto res = write(handle_, buf_);
if (auto num_bytes = get_if<size_t>(&res)) { if (auto num_bytes = get_if<size_t>(&res)) {
......
...@@ -90,13 +90,13 @@ public: ...@@ -90,13 +90,13 @@ public:
return none; return none;
} }
template <class Transport> template <class Parent>
void write_message(Transport& transport, void write_message(Parent& parent,
std::unique_ptr<endpoint_manager_queue::message> msg) { std::unique_ptr<endpoint_manager_queue::message> msg) {
if (auto payload = serialize(transport.system(), msg->msg->payload)) auto payload_buf = parent.next_payload_buffer();
transport.write_packet(*payload); if (auto err = serialize(parent.system(), msg->msg->payload, payload_buf))
else CAF_FAIL("serializing failed: " << err);
CAF_FAIL("serializing failed: " << payload.error()); parent.write_packet(payload_buf);
} }
template <class Parent> template <class Parent>
...@@ -138,12 +138,10 @@ public: ...@@ -138,12 +138,10 @@ public:
// nop // nop
} }
static expected<buffer_type> serialize(actor_system& sys, const message& x) { static error_code<sec> serialize(actor_system& sys, const message& x,
buffer_type result; std::vector<byte>& buf) {
binary_serializer sink{sys, result}; binary_serializer sink{sys, buf};
if (auto err = x.save(sink)) return x.save(sink);
return err.value();
return result;
} }
private: private:
......
...@@ -105,23 +105,20 @@ public: ...@@ -105,23 +105,20 @@ public:
if (ptr->msg == nullptr) if (ptr->msg == nullptr)
return; return;
auto header_buf = parent.next_header_buffer(); auto header_buf = parent.next_header_buffer();
auto payload_buf = serialize(parent.system(), ptr->msg->payload); auto payload_buf = parent.next_payload_buffer();
if (!payload_buf) if (auto err = serialize(parent.system(), ptr->msg->payload, payload_buf))
CAF_FAIL("serializing failed: " << payload_buf.error()); CAF_FAIL("serializing failed: " << err);
binary_serializer sink{sys_, header_buf}; binary_serializer sink{sys_, header_buf};
header_type header{static_cast<uint32_t>(payload_buf->size())}; header_type header{static_cast<uint32_t>(payload_buf.size())};
if (auto err = sink(header)) if (auto err = sink(header))
CAF_FAIL("serializing failed: " << err); CAF_FAIL("serializing failed: " << err);
parent.write_packet(header_buf, *payload_buf); parent.write_packet(header_buf, payload_buf);
} }
static expected<std::vector<byte>> serialize(actor_system& sys, static error_code<sec> serialize(actor_system& sys, const message& x,
const message& x) { std::vector<byte>& buf) {
std::vector<byte> result; binary_serializer sink{sys, buf};
binary_serializer sink{sys, result}; return x.save(sink);
if (auto err = x.save(sink))
return err.value();
return result;
} }
private: private:
......
...@@ -289,7 +289,8 @@ CAF_TEST(handle_data) { ...@@ -289,7 +289,8 @@ CAF_TEST(handle_data) {
CHECK_HANDLE_DATA(test_data.at(3)); CHECK_HANDLE_DATA(test_data.at(3));
} }
/*CAF_TEST(write_message write_packet) { /*
CAF_TEST(write_message write_packet) {
CHECK_WRITE_MESSAGE(test_data.at(0)); CHECK_WRITE_MESSAGE(test_data.at(0));
CHECK_WRITE_MESSAGE(test_data.at(1)); CHECK_WRITE_MESSAGE(test_data.at(1));
CHECK_WRITE_MESSAGE(test_data.at(2)); CHECK_WRITE_MESSAGE(test_data.at(2));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment