Skip to content

Commit

Permalink
More padding optimization
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rocallahan committed Mar 3, 2018
1 parent 8a023cc commit f138c5b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
11 changes: 9 additions & 2 deletions examples/dwarfdump.rs
Expand Up @@ -676,7 +676,8 @@ fn dump_entries<R: Reader, W: Write>(
print_local = false;
}
writeln!(w,
"<{:2}><0x{:08x}>{}{}",
"<{}{}><0x{:08x}>{}{}",
if depth < 10 { " " } else { "" },
depth,
entry.offset().0,
spaces(&mut spaces_buf, indent),
Expand Down Expand Up @@ -709,7 +710,13 @@ fn dump_entries<R: Reader, W: Write>(

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 {
Expand Down
24 changes: 17 additions & 7 deletions src/constants.rs
Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit f138c5b

Please sign in to comment.