Skip to content

Commit

Permalink
Use to_writer to avoid extra allocation and copy
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Sichert <mail@pablosichert.com>
  • Loading branch information
pablosichert committed Jan 12, 2022
1 parent 597a2d2 commit 650c17f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/codecs/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,12 @@ impl Encoder<Event> for JsonSerializer {
type Error = crate::Error;

fn encode(&mut self, event: Event, buffer: &mut bytes::BytesMut) -> Result<(), Self::Error> {
let json = match event {
Event::Log(log) => serde_json::to_vec(&log),
Event::Metric(metric) => serde_json::to_vec(&metric),
}?;

buffer.put(json.as_slice());

Ok(())
let writer = buffer.writer();
match event {
Event::Log(log) => serde_json::to_writer(writer, &log),
Event::Metric(metric) => serde_json::to_writer(writer, &metric),
}
.map_err(Into::into)
}
}

Expand Down

0 comments on commit 650c17f

Please sign in to comment.