zero-copy message receive

Construct messages from a reference-counted buffer allocated once
per receive instead of copying the data.
This commit is contained in:
Jens Auer
2015-05-29 23:54:43 +02:00
parent 611e96c701
commit e9b403a7b1
6 changed files with 273 additions and 67 deletions

View File

@@ -42,6 +42,45 @@
namespace zmq
{
// Static buffer policy.
class c_single_allocator
{
public:
c_single_allocator(size_t bufsize_):
buf((unsigned char*) malloc (bufsize) ),
bufsize(bufsize_)
{
alloc_assert (buf);
}
~c_single_allocator()
{
free(buf);
}
unsigned char* allocate()
{
return buf;
}
void deallocate()
{
}
size_t size() const
{
return bufsize;
}
private:
unsigned char* buf;
size_t bufsize;
c_single_allocator( c_single_allocator const& );
c_single_allocator& operator=(c_single_allocator const&);
};
// Helper base class for decoders that know the amount of data to read
// in advance at any moment. Knowing the amount in advance is a property
// of the protocol used. 0MQ framing protocol is based size-prefixed
@@ -52,31 +91,34 @@ namespace zmq
//
// This class implements the state machine that parses the incoming buffer.
// Derived class should implement individual state machine actions.
template <typename T> class decoder_base_t : public i_decoder
//
// Buffer managment is done by an allocator policy.
template <typename T, typename A = c_single_allocator>
class decoder_base_t : public i_decoder
{
public:
inline decoder_base_t (size_t bufsize_) :
next (NULL),
read_pos (NULL),
to_read (0),
bufsize (bufsize_)
inline decoder_base_t (A* allocator_) :
next (NULL),
read_pos (NULL),
to_read (0),
allocator( allocator_ )
{
buf = (unsigned char*) malloc (bufsize_);
alloc_assert (buf);
buf = allocator->allocate();
}
// The destructor doesn't have to be virtual. It is mad virtual
// just to keep ICC and code checking tools from complaining.
inline virtual ~decoder_base_t ()
{
free (buf);
allocator->deallocate();
}
// Returns a buffer to be filled with binary data.
inline void get_buffer (unsigned char **data_, size_t *size_)
{
buf = allocator->allocate();
// If we are expected to read large message, we'll opt for zero-
// copy, i.e. we'll ask caller to fill the data directly to the
// message. Note that subsequent read(s) are non-blocking, thus
@@ -85,14 +127,14 @@ namespace zmq
// As a consequence, large messages being received won't block
// other engines running in the same I/O thread for excessive
// amounts of time.
if (to_read >= bufsize) {
if (to_read >= allocator->size()) {
*data_ = read_pos;
*size_ = to_read;
return;
}
*data_ = buf;
*size_ = bufsize;
*size_ = allocator->size();
}
// Processes the data in the buffer previously allocated using
@@ -116,7 +158,7 @@ namespace zmq
bytes_used_ = size_;
while (!to_read) {
const int rc = (static_cast <T*> (this)->*next) ();
const int rc = (static_cast <T*> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
}
@@ -126,14 +168,20 @@ namespace zmq
while (bytes_used_ < size_) {
// Copy the data from buffer to the message.
const size_t to_copy = std::min (to_read, size_ - bytes_used_);
memcpy (read_pos, data_ + bytes_used_, to_copy);
// only copy when the destination address is different from the
// current address in the buffer
if (read_pos != data_ + bytes_used_) {
memcpy(read_pos, data_ + bytes_used_, to_copy);
}
read_pos += to_copy;
to_read -= to_copy;
bytes_used_ += to_copy;
// Try to get more space in the message to fill in.
// If none is available, return.
while (to_read == 0) {
const int rc = (static_cast <T*> (this)->*next) ();
// pass current address in the buffer
const int rc = (static_cast <T*> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
}
@@ -146,7 +194,7 @@ namespace zmq
// Prototype of state machine action. Action should return false if
// it is unable to push the data to the system.
typedef int (T::*step_t) ();
typedef int (T::*step_t) (unsigned char const*);
// This function should be called from derived class to read data
// from the buffer and schedule next state machine action.
@@ -171,8 +219,8 @@ namespace zmq
size_t to_read;
// The duffer for data to decode.
size_t bufsize;
unsigned char *buf;
A* allocator;
unsigned char* buf;
decoder_base_t (const decoder_base_t&);
const decoder_base_t &operator = (const decoder_base_t&);