Skip to content

Commit

Permalink
Avoid allocing a new buffer for each protobuf payload (#192)
Browse files Browse the repository at this point in the history
This commit avoids creating a new buffer for every protobuf payload
the native source receives. There's still some temporary objects
being made--the implicit CodedInputStream could be reused--but it's
a start.

Signed-off-by: Brian L. Troutwine <blt@postmates.com>
  • Loading branch information
blt authored Jan 16, 2017
1 parent af12873 commit 4941129
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/source/native.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use byteorder::{BigEndian, ReadBytesExt};
use hopper;
use metric;
use protobuf::parse_from_bytes;
use protocols::native::{AggregationMethod, Payload};
use protobuf;
use std::io;
use std::io::Read;
use std::net::{TcpListener, TcpStream, ToSocketAddrs};
Expand Down Expand Up @@ -63,16 +63,17 @@ fn handle_tcp(chans: util::Channel,
fn handle_stream(mut chans: util::Channel, tags: metric::TagMap, stream: TcpStream) {
thread::spawn(move || {
let mut reader = io::BufReader::new(stream);
let mut buf = Vec::with_capacity(4000);
loop {
let payload_size_in_bytes = match reader.read_u32::<BigEndian>() {
Ok(i) => i,
Ok(i) => i as usize,
Err(_) => return,
};
let mut buf = vec![0; payload_size_in_bytes as usize];
buf.resize(payload_size_in_bytes, 0);
if reader.read_exact(&mut buf).is_err() {
return;
}
match parse_from_bytes::<Payload>(&buf) {
match protobuf::parse_from_bytes::<Payload>(&buf) {
Ok(pyld) => {
for point in pyld.get_points() {
let name: &str = point.get_name();
Expand Down

0 comments on commit 4941129

Please sign in to comment.