fix performance regression in output generation, use copy elision and return result char array

This commit is contained in:
Dennis Luxen 2015-01-26 13:35:24 +01:00
parent 37a9813482
commit 06a08a874f
2 changed files with 10 additions and 10 deletions

View File

@ -86,7 +86,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
case deflateRFC1951: case deflateRFC1951:
// use deflate for compression // use deflate for compression
reply.headers.insert(reply.headers.begin(), {"Content-Encoding", "deflate"}); reply.headers.insert(reply.headers.begin(), {"Content-Encoding", "deflate"});
CompressBufferCollection(reply.content, compression_type, compressed_output); compressed_output = compress_buffers(reply.content, compression_type);
reply.SetSize(static_cast<unsigned>(compressed_output.size())); reply.SetSize(static_cast<unsigned>(compressed_output.size()));
output_buffer = reply.HeaderstoBuffers(); output_buffer = reply.HeaderstoBuffers();
output_buffer.push_back(boost::asio::buffer(compressed_output)); output_buffer.push_back(boost::asio::buffer(compressed_output));
@ -94,7 +94,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
case gzipRFC1952: case gzipRFC1952:
// use gzip for compression // use gzip for compression
reply.headers.insert(reply.headers.begin(), {"Content-Encoding", "gzip"}); reply.headers.insert(reply.headers.begin(), {"Content-Encoding", "gzip"});
CompressBufferCollection(reply.content, compression_type, compressed_output); compressed_output = compress_buffers(reply.content, compression_type);
reply.SetSize(static_cast<unsigned>(compressed_output.size())); reply.SetSize(static_cast<unsigned>(compressed_output.size()));
output_buffer = reply.HeaderstoBuffers(); output_buffer = reply.HeaderstoBuffers();
output_buffer.push_back(boost::asio::buffer(compressed_output)); output_buffer.push_back(boost::asio::buffer(compressed_output));
@ -142,9 +142,8 @@ void Connection::handle_write(const boost::system::error_code &error)
} }
} }
void Connection::CompressBufferCollection(std::vector<char> uncompressed_data, std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data,
CompressionType compression_type, const CompressionType compression_type)
std::vector<char> &compressed_data)
{ {
boost::iostreams::gzip_params compression_parameters; boost::iostreams::gzip_params compression_parameters;
@ -156,12 +155,14 @@ void Connection::CompressBufferCollection(std::vector<char> uncompressed_data,
compression_parameters.noheader = true; compression_parameters.noheader = true;
} }
BOOST_ASSERT(compressed_data.empty()); std::vector<char> compressed_data;
// plug data into boost's compression stream // plug data into boost's compression stream
boost::iostreams::filtering_ostream gzip_stream; boost::iostreams::filtering_ostream gzip_stream;
gzip_stream.push(boost::iostreams::gzip_compressor(compression_parameters)); gzip_stream.push(boost::iostreams::gzip_compressor(compression_parameters));
gzip_stream.push(boost::iostreams::back_inserter(compressed_data)); gzip_stream.push(boost::iostreams::back_inserter(compressed_data));
gzip_stream.write(&uncompressed_data[0], uncompressed_data.size()); gzip_stream.write(&uncompressed_data[0], uncompressed_data.size());
boost::iostreams::close(gzip_stream); boost::iostreams::close(gzip_stream);
return compressed_data;
} }
} }

View File

@ -1,6 +1,6 @@
/* /*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others Copyright (c) 2015, Project OSRM, Dennis Luxen, others
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
@ -75,9 +75,8 @@ class Connection : public std::enable_shared_from_this<Connection>
/// Handle completion of a write operation. /// Handle completion of a write operation.
void handle_write(const boost::system::error_code &e); void handle_write(const boost::system::error_code &e);
void CompressBufferCollection(std::vector<char> uncompressed_data, std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
CompressionType compression_type, const CompressionType compression_type);
std::vector<char> &compressed_data);
boost::asio::io_service::strand strand; boost::asio::io_service::strand strand;
boost::asio::ip::tcp::socket TCP_socket; boost::asio::ip::tcp::socket TCP_socket;