Use writer.write_all(b"...") instead of write! for static strings

This commit is contained in:
Erick Tryzelaar 2015-02-14 10:19:15 -08:00
parent 004a89510e
commit 1f98ee0d8f
2 changed files with 118 additions and 118 deletions

View File

@ -848,165 +848,165 @@ fn bench_copy(b: &mut Bencher) {
}
fn manual_serialize_no_escape<W: io::Write>(wr: &mut W, log: &Log) {
(write!(wr, "{{\"timestamp\":")).unwrap();
wr.write(b"{\"timestamp\":").unwrap();
(write!(wr, "{}", log.timestamp)).unwrap();
(write!(wr, ",\"zone_id\":")).unwrap();
wr.write(b",\"zone_id\":").unwrap();
(write!(wr, "{}", log.zone_id)).unwrap();
(write!(wr, ",\"zone_plan\":")).unwrap();
wr.write(b",\"zone_plan\":").unwrap();
(write!(wr, "{}", log.zone_plan as usize)).unwrap();
(write!(wr, ",\"http\":{{\"protocol\":")).unwrap();
wr.write(b",\"http\":{\"protocol\":").unwrap();
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
(write!(wr, ",\"status\":")).unwrap();
wr.write(b",\"status\":").unwrap();
(write!(wr, "{}", log.http.status)).unwrap();
(write!(wr, ",\"host_status\":")).unwrap();
wr.write(b",\"host_status\":").unwrap();
(write!(wr, "{}", log.http.host_status)).unwrap();
(write!(wr, ",\"up_status\":")).unwrap();
wr.write(b",\"up_status\":").unwrap();
(write!(wr, "{}", log.http.up_status)).unwrap();
(write!(wr, ",\"method\":")).unwrap();
wr.write(b",\"method\":").unwrap();
(write!(wr, "{}", log.http.method as usize)).unwrap();
(write!(wr, ",\"content_type\":")).unwrap();
wr.write(b",\"content_type\":").unwrap();
(write!(wr, "\"{}\"", log.http.content_type)).unwrap();
(write!(wr, ",\"user_agent\":")).unwrap();
wr.write(b",\"user_agent\":").unwrap();
(write!(wr, "\"{}\"", log.http.user_agent)).unwrap();
(write!(wr, ",\"referer\":")).unwrap();
wr.write(b",\"referer\":").unwrap();
(write!(wr, "\"{}\"", log.http.referer)).unwrap();
(write!(wr, ",\"request_uri\":")).unwrap();
wr.write(b",\"request_uri\":").unwrap();
(write!(wr, "\"{}\"", log.http.request_uri)).unwrap();
(write!(wr, "}},\"origin\":{{")).unwrap();
wr.write(b"},\"origin\":{").unwrap();
(write!(wr, "\"ip\":")).unwrap();
wr.write(b"\"ip\":").unwrap();
(write!(wr, "\"{}\"", log.origin.ip)).unwrap();
(write!(wr, ",\"port\":")).unwrap();
wr.write(b",\"port\":").unwrap();
(write!(wr, "{}", log.origin.port)).unwrap();
(write!(wr, ",\"hostname\":")).unwrap();
wr.write(b",\"hostname\":").unwrap();
(write!(wr, "\"{}\"", log.origin.hostname)).unwrap();
(write!(wr, ",\"protocol\":")).unwrap();
wr.write(b",\"protocol\":").unwrap();
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
(write!(wr, "}},\"country\":")).unwrap();
wr.write(b"},\"country\":").unwrap();
(write!(wr, "{}", log.country as usize)).unwrap();
(write!(wr, ",\"cache_status\":")).unwrap();
wr.write(b",\"cache_status\":").unwrap();
(write!(wr, "{}", log.cache_status as usize)).unwrap();
(write!(wr, ",\"server_ip\":")).unwrap();
wr.write(b",\"server_ip\":").unwrap();
(write!(wr, "\"{}\"", log.server_ip)).unwrap();
(write!(wr, ",\"server_name\":")).unwrap();
wr.write(b",\"server_name\":").unwrap();
(write!(wr, "\"{}\"", log.server_name)).unwrap();
(write!(wr, ",\"remote_ip\":")).unwrap();
wr.write(b",\"remote_ip\":").unwrap();
(write!(wr, "\"{}\"", log.remote_ip)).unwrap();
(write!(wr, ",\"bytes_dlv\":")).unwrap();
wr.write(b",\"bytes_dlv\":").unwrap();
(write!(wr, "{}", log.bytes_dlv)).unwrap();
(write!(wr, ",\"ray_id\":")).unwrap();
wr.write(b",\"ray_id\":").unwrap();
(write!(wr, "\"{}\"", log.ray_id)).unwrap();
(write!(wr, "}}")).unwrap();
wr.write(b"}").unwrap();
}
fn manual_serialize_escape<W: io::Write>(wr: &mut W, log: &Log) {
(write!(wr, "{{")).unwrap();
wr.write_all(b"{").unwrap();
escape_str(wr, "timestamp").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.timestamp)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "zone_id").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.zone_id)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "zone_plan").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.zone_plan as isize)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "http").unwrap();
(write!(wr, ":{{")).unwrap();
wr.write_all(b":{").unwrap();
escape_str(wr, "protocol").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "status").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.http.status)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "host_status").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.http.host_status)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "up_status").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.http.up_status)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "method").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.http.method as usize)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "content_type").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.http.content_type).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "user_agent").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.http.user_agent).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "referer").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.http.referer).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "request_uri").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.http.request_uri).unwrap();
(write!(wr, "}},")).unwrap();
wr.write_all(b"},").unwrap();
escape_str(wr, "origin").unwrap();
(write!(wr, ":{{")).unwrap();
wr.write_all(b":{").unwrap();
escape_str(wr, "ip").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.origin.ip).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "port").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.origin.port)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "hostname").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.origin.hostname).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "protocol").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
(write!(wr, "}},")).unwrap();
wr.write_all(b"},").unwrap();
escape_str(wr, "country").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.country as usize)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "cache_status").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.cache_status as usize)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "server_ip").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.server_ip).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "server_name").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.server_name).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "remote_ip").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.remote_ip).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "bytes_dlv").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
(write!(wr, "{}", log.bytes_dlv)).unwrap();
(write!(wr, ",")).unwrap();
wr.write_all(b",").unwrap();
escape_str(wr, "ray_id").unwrap();
(write!(wr, ":")).unwrap();
wr.write_all(b":").unwrap();
escape_str(wr, &log.ray_id).unwrap();
(write!(wr, "}}")).unwrap();
wr.write_all(b"}").unwrap();
}
#[test]

View File

@ -8,27 +8,27 @@ use ser::Serialize;
use ser;
fn escape_bytes<W: io::Write>(wr: &mut W, bytes: &[u8]) -> io::Result<()> {
try!(write!(wr, "\""));
try!(wr.write(b"\""));
let mut start = 0;
for (i, byte) in bytes.iter().enumerate() {
let escaped = match *byte {
b'"' => "\\\"",
b'\\' => "\\\\",
b'\x08' => "\\b",
b'\x0c' => "\\f",
b'\n' => "\\n",
b'\r' => "\\r",
b'\t' => "\\t",
b'"' => b"\\\"",
b'\\' => b"\\\\",
b'\x08' => b"\\b",
b'\x0c' => b"\\f",
b'\n' => b"\\n",
b'\r' => b"\\r",
b'\t' => b"\\t",
_ => { continue; }
};
if start < i {
try!(wr.write_all(&bytes[start..i]));
try!(wr.write(&bytes[start..i]));
}
try!(write!(wr, "{}", escaped));
try!(wr.write(escaped));
start = i + 1;
}
@ -37,7 +37,7 @@ fn escape_bytes<W: io::Write>(wr: &mut W, bytes: &[u8]) -> io::Result<()> {
try!(wr.write_all(&bytes[start..]));
}
write!(wr, "\"")
wr.write_all(b"\"")
}
pub fn escape_str<W: io::Write>(wr: &mut W, v: &str) -> io::Result<()> {
@ -115,66 +115,66 @@ impl<W: io::Write> Serializer<W> {
impl<W: io::Write> ser::Serializer<io::Error> for Serializer<W> {
#[inline]
fn serialize_null(&mut self) -> io::Result<()> {
write!(&mut self.wr, "null")
self.wr.write_all(b"null")
}
#[inline]
fn serialize_bool(&mut self, v: bool) -> io::Result<()> {
if v {
write!(&mut self.wr, "true")
self.wr.write_all(b"true")
} else {
write!(&mut self.wr, "false")
self.wr.write_all(b"false")
}
}
#[inline]
fn serialize_isize(&mut self, v: isize) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i8(&mut self, v: i8) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i16(&mut self, v: i16) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i32(&mut self, v: i32) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i64(&mut self, v: i64) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_usize(&mut self, v: usize) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u8(&mut self, v: u8) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u16(&mut self, v: u16) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u32(&mut self, v: u32) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u64(&mut self, v: u64) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
@ -384,60 +384,60 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
#[inline]
fn serialize_bool(&mut self, v: bool) -> io::Result<()> {
if v {
write!(&mut self.wr, "true")
self.wr.write_all(b"true")
} else {
write!(&mut self.wr, "false")
self.wr.write_all(b"false")
}
}
#[inline]
fn serialize_isize(&mut self, v: isize) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i8(&mut self, v: i8) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i16(&mut self, v: i16) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i32(&mut self, v: i32) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_i64(&mut self, v: i64) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_usize(&mut self, v: usize) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u8(&mut self, v: u8) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u16(&mut self, v: u16) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u32(&mut self, v: u32) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
fn serialize_u64(&mut self, v: u64) -> io::Result<()> {
write!(&mut &mut self.wr, "{}", v)
write!(&mut self.wr, "{}", v)
}
#[inline]
@ -463,7 +463,7 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
#[inline]
fn serialize_tuple_start(&mut self, _len: usize) -> io::Result<()> {
self.first = true;
write!(&mut self.wr, "[")
self.wr.write_all(b"[")
}
#[inline]
@ -482,7 +482,7 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
#[inline]
fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> io::Result<()> {
self.first = true;
write!(&mut self.wr, "{{")
self.wr.write_all(b"{")
}
#[inline]
@ -491,7 +491,7 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
>(&mut self, name: &str, value: &T) -> io::Result<()> {
try!(self.serialize_sep());
try!(self.serialize_str(name));
try!(write!(&mut self.wr, ": "));
try!(self.wr.write_all(b": "));
value.serialize(self)
}
@ -503,11 +503,11 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
#[inline]
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: usize) -> io::Result<()> {
self.first = true;
try!(write!(&mut self.wr, "{{"));
try!(self.wr.write_all(b"{"));
try!(self.serialize_sep());
try!(self.serialize_str(variant));
self.first = true;
write!(&mut self.wr, ": [")
self.wr.write_all(b": [")
}
#[inline]
@ -543,7 +543,7 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
T: Serialize<PrettySerializer<W>, io::Error>,
Iter: Iterator<Item=T>
>(&mut self, iter: Iter) -> io::Result<()> {
try!(write!(&mut self.wr, "["));
try!(self.wr.write_all(b"["));
self.first = true;
for elt in iter {
@ -560,13 +560,13 @@ impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
V: Serialize<PrettySerializer<W>, io::Error>,
Iter: Iterator<Item=(K, V)>
>(&mut self, iter: Iter) -> io::Result<()> {
try!(write!(&mut self.wr, "{{"));
try!(self.wr.write_all(b"{"));
self.first = true;
for (key, value) in iter {
try!(self.serialize_sep());
try!(key.serialize(self));
try!(write!(&mut self.wr, ": "));
try!(self.wr.write_all(b": "));
try!(value.serialize(self));
}