From 9be877c68503c35f9f72c8b92bd11454e4fcad97 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Tue, 8 Dec 2009 15:41:50 +0100 Subject: ZMQII-26: Use zero-copy for large messages --- src/encoder.hpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/encoder.hpp') diff --git a/src/encoder.hpp b/src/encoder.hpp index 653fbfb..62abb03 100644 --- a/src/encoder.hpp +++ b/src/encoder.hpp @@ -24,6 +24,8 @@ #include #include +#include + namespace zmq { @@ -44,17 +46,31 @@ namespace zmq // NULL, it is filled by offset of the first message in the batch. // If there's no beginning of a message in the batch, offset is // set to -1. - inline size_t read (unsigned char *data_, size_t size_, + inline void read (unsigned char **data_, size_t *size_, int *offset_ = NULL) { int offset = -1; size_t pos = 0; - while (pos < size_) { + while (pos < *size_) { + + // If we are able to fill whole buffer in a single go, let's + // use zero-copy. There's no disadvantage to it as we cannot + // stuck multiple messages into the buffer anyway. + if (pos == 0 && to_write >= *size_) { + *data_ = write_pos; + write_pos += *size_; + to_write -= *size_; + + // TODO: manage beginning & offset here. + + return; + } + if (to_write) { - size_t to_copy = std::min (to_write, size_ - pos); - memcpy (data_ + pos, write_pos, to_copy); + size_t to_copy = std::min (to_write, *size_ - pos); + memcpy (*data_ + pos, write_pos, to_copy); pos += to_copy; write_pos += to_copy; to_write -= to_copy; @@ -70,10 +86,12 @@ namespace zmq } } + // Return offset of the first message in the buffer. if (offset_) *offset_ = offset; - return pos; + // Return the size of the filled-in portion of the buffer. + *size_ = pos; } protected: -- cgit v1.2.3