Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

get perf improvements

- service a single-chunk item using a single async_write
- avoid stringstream to build strings - it's slow
  • Loading branch information...
commit 371c26977f113353c479f5c93562a37d733d5f90 1 parent c6702e8
@erikfrey erikfrey authored
Showing with 32 additions and 15 deletions.
  1. +5 −4 include/darner/net/handler.h
  2. +27 −11 src/net/handler.cpp
View
9 include/darner/net/handler.h
@@ -1,6 +1,9 @@
#ifndef __DARNER_HANDLER_HPP__
#define __DARNER_HANDLER_HPP__
+#include <vector>
+#include <string>
+
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/bind.hpp>
@@ -65,8 +68,6 @@ class handler : public boost::enable_shared_from_this<handler>
void get_on_queue_return(const boost::system::error_code& e);
- void write_first_chunk();
-
void get_on_read_next_chunk(const boost::system::error_code& e);
void get_on_write_chunk(const boost::system::error_code& e, size_t bytes_transferred);
@@ -104,7 +105,7 @@ class handler : public boost::enable_shared_from_this<handler>
{
buf_ = "SERVER_ERROR " + ex.code().message() + "\r\n";
boost::asio::async_write(
- socket_, boost::asio::buffer(buf_), boost::bind(&handler::hang_up, shared_from_this(), _1, _2));
+ socket_, boost::asio::buffer(buf_), boost::bind(&handler::hang_up, shared_from_this(), _1, _2));
}
}
@@ -117,7 +118,7 @@ class handler : public boost::enable_shared_from_this<handler>
queue_map& queues_;
stats& stats_;
boost::asio::streambuf in_;
- std::string header_buf_;
+ std::vector<char> header_buf_;
std::string buf_;
request req_;
View
38 src/net/handler.cpp
@@ -1,5 +1,7 @@
#include "darner/net/handler.h"
+#include <cstdio>
+
#include <boost/array.hpp>
using namespace std;
@@ -197,7 +199,31 @@ void handler::get()
return error("get", ex);
}
- write_first_chunk();
+ header_buf_.resize(21 + req_.queue.size()); // 21 = len("VALUE 0 4294967296\r\n")
+ header_buf_.resize(::sprintf(&header_buf_[0], "VALUE %s 0 %lu\r\n", req_.queue.c_str(), pop_stream_.size()));
+
+ if (pop_stream_.tell() == pop_stream_.size())
+ {
+ if (!req_.get_open)
+ {
+ try
+ {
+ pop_stream_.close(!req_.get_peek);
+ }
+ catch (const system::system_error& ex)
+ {
+ return error("get_on_write_chunk", ex, false);
+ }
+ }
+ ++stats_.items_dequeued;
+ array<const_buffer, 3> bufs = {{ buffer(header_buf_), buffer(buf_), buffer("\r\nEND\r\n", 7) }};
+ async_write(socket_, bufs, bind(&handler::read_request, shared_from_this(), _1, _2));
+ }
+ else
+ {
+ array<const_buffer, 2> bufs = {{ buffer(header_buf_), buffer(buf_) }};
+ async_write(socket_, bufs, bind(&handler::get_on_write_chunk, shared_from_this(), _1, _2));
+ }
}
void handler::get_on_queue_return(const boost::system::error_code& e)
@@ -210,16 +236,6 @@ void handler::get_on_queue_return(const boost::system::error_code& e)
get();
}
-void handler::write_first_chunk()
-{
- ostringstream oss;
- oss << "VALUE " << req_.queue << " 0 " << pop_stream_.size() << "\r\n";
- header_buf_ = oss.str();
- array<const_buffer, 2> bufs = {{ buffer(header_buf_), buffer(buf_) }};
-
- async_write(socket_, bufs, bind(&handler::get_on_write_chunk, shared_from_this(), _1, _2));
-}
-
void handler::get_on_write_chunk(const boost::system::error_code& e, size_t bytes_transferred)
{
if (e)
Please sign in to comment.
Something went wrong with that request. Please try again.