Optimize encode performance for NoticeResponse

This commit is contained in:
Ryan Leckey 2019-06-21 20:58:32 -07:00
parent 65c41d0484
commit 3fa880f5f3
5 changed files with 25 additions and 18 deletions

View file

@ -24,3 +24,5 @@ bytes = "0.4.12"
[profile.bench]
lto = true
codegen-units = 1
incremental = false

View file

@ -11,6 +11,7 @@ byteorder = "1.3.1"
bytes = "0.4.12"
memchr = "2.2.0"
md-5 = "0.8.0"
itoa = "0.4.4"
[dev-dependencies]
matches = "0.1.8"

View file

@ -5,21 +5,24 @@ use criterion::{Criterion};
use mason_postgres_protocol::{Encode, NoticeResponse, Severity};
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("encode NoticeResponse", |b| {
b.iter(|| {
let message = NoticeResponse::builder()
.severity(Severity::Notice)
.code("42710")
.message("extension \"uuid-ossp\" already exists, skipping")
.file("extension.c")
.line(1656)
.routine("CreateExtension")
.build();
c.bench_function("encode NoticeResponse",
|b| {
let mut dst = Vec::new();
b.iter(|| {
let message = NoticeResponse::builder()
.severity(Severity::Notice)
.code("42710")
.message("extension \"uuid-ossp\" already exists, skipping")
.file("extension.c")
.line(1656)
.routine("CreateExtension")
.build();
let mut dst = Vec::with_capacity(message.size_hint());
message.encode(&mut dst).unwrap();
})
});
dst.truncate(0);
message.encode(&mut dst).unwrap();
})
}
);
}
criterion_group!(benches, criterion_benchmark);

View file

@ -9,6 +9,7 @@ use std::{
ptr::NonNull,
str::{self, FromStr},
};
use core::fmt::Debug;
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
pub enum Severity {
@ -625,13 +626,13 @@ impl<'a> Encode for NoticeResponseBuilder<'a> {
if let Some(position) = &self.position {
buf.push(b'P');
buf.write_all(position.to_string().as_bytes())?;
itoa::write(&mut *buf, *position)?;
buf.push(0);
}
if let Some(internal_position) = &self.internal_position {
buf.push(b'p');
buf.write_all(internal_position.to_string().as_bytes())?;
itoa::write(&mut *buf, *internal_position)?;
buf.push(0);
}
@ -685,7 +686,7 @@ impl<'a> Encode for NoticeResponseBuilder<'a> {
if let Some(line) = &self.line {
buf.push(b'L');
buf.write_all(line.to_string().as_bytes())?;
itoa::write(&mut *buf, *line)?;
buf.push(0);
}

View file

@ -1 +1 @@
nightly-2019-06-06
nightly-2019-06-21