summaryrefslogtreecommitdiff
path: root/src/encoder.hpp
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@fastmq.commkdir>2009-12-08 15:41:50 +0100
committerMartin Sustrik <sustrik@fastmq.commkdir>2009-12-08 15:41:50 +0100
commit9be877c68503c35f9f72c8b92bd11454e4fcad97 (patch)
tree8e5ea05ee40adbe49d7ef9edf1f841681d0e95b8 /src/encoder.hpp
parentbfef2fcd0ba590169ad46ea45da9d36dca1b5b97 (diff)
ZMQII-26: Use zero-copy for large messages
Diffstat (limited to 'src/encoder.hpp')
-rw-r--r--src/encoder.hpp28
1 files changed, 23 insertions, 5 deletions
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 <string.h>
#include <algorithm>
+#include <stdio.h>
+
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: