From f138c5beb88cba7c1cbfac09b2dc91ab69fd53a3 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Mon, 26 Feb 2018 22:14:16 +1300 Subject: [PATCH] More padding optimization Using slices of spaces instead of regular padding can speed things up even more, though it's a little more invasive; we need to be able to obtain the value of an enum as a static string to avoid dynamically calculating its length. Before: time ( target/release/examples/dwarfdump -i ~/mozilla-central/obj-ff-opt/dist/bin/libxul.so >& /dev/null ) real 2m21.745s user 2m20.175s sys 0m1.412s After: time ( target/release/examples/dwarfdump -i ~/mozilla-central/obj-ff-opt/dist/bin/libxul.so >& /dev/null ) real 1m39.153s user 1m37.714s sys 0m1.320s --- examples/dwarfdump.rs | 11 +++++++++-- src/constants.rs | 24 +++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/examples/dwarfdump.rs b/examples/dwarfdump.rs index c12c314cc..1558efd00 100644 --- a/examples/dwarfdump.rs +++ b/examples/dwarfdump.rs @@ -676,7 +676,8 @@ fn dump_entries( print_local = false; } writeln!(w, - "<{:2}><0x{:08x}>{}{}", + "<{}{}><0x{:08x}>{}{}", + if depth < 10 { " " } else { "" }, depth, entry.offset().0, spaces(&mut spaces_buf, indent), @@ -709,7 +710,13 @@ fn dump_entries( let mut attrs = entry.attrs(); while let Some(attr) = attrs.next()? { - write!(w, "{}{:27} ", spaces(&mut spaces_buf, indent + 18), attr.name())?; + w.write_all(spaces(&mut spaces_buf, indent + 18).as_bytes())?; + if let Some(n) = attr.name().static_string() { + let right_padding = 27 - std::cmp::min(27, n.len()); + write!(w, "{}{} ", n, spaces(&mut spaces_buf, right_padding))?; + } else { + write!(w, "{:27} ", attr.name())?; + } if flags.raw { writeln!(w, "{:?}", attr.raw_value())?; } else { diff --git a/src/constants.rs b/src/constants.rs index 9c70db1df..4076e06d3 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -63,15 +63,25 @@ macro_rules! dw { pub const $name: $struct_name = $struct_name($val); )+ - impl fmt::Display for $struct_name { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - match *self { + impl $struct_name { + pub fn static_string(&self) -> Option<&'static str> { + Some(match *self { $( - $name => f.pad(stringify!($name)), + $name => stringify!($name), )+ - otherwise => f.pad(&format!("Unknown {}: {}", - stringify!($struct_name), - otherwise.0)), + _ => return None, + }) + } + } + + impl fmt::Display for $struct_name { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + if let Some(s) = self.static_string() { + f.pad(s) + } else { + f.pad(&format!("Unknown {}: {}", + stringify!($struct_name), + self.0)) } } }