Skip to content

Commit 4618d5f

Browse files
Mark-Simulacrumluser
authored andcommitted
Replace custom demangle_stream with rustc_demangle::demangle_stream
The upstream implementation is significantly (~2x) faster.
1 parent 3c81f10 commit 4618d5f

File tree

4 files changed

+36
-140
lines changed

4 files changed

+36
-140
lines changed

Cargo.lock

Lines changed: 25 additions & 91 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ readme = "README.md"
99
license = "Apache-2.0"
1010

1111
[dependencies]
12-
rustc-demangle = "^0.1.4"
13-
lazy_static = "^1"
14-
regex = "^1"
12+
rustc-demangle = { version = "0.1.23", features = ["std"] }
1513

1614
[dependencies.clap]
1715
version = "^2.21.1"

src/main.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,18 @@
1414

1515
#[macro_use]
1616
extern crate clap;
17-
#[macro_use]
18-
extern crate lazy_static;
19-
extern crate regex;
2017
extern crate rustc_demangle;
2118

22-
use rustc_demangle::demangle;
23-
use regex::{Regex, Captures};
19+
use rustc_demangle::demangle_stream;
2420

25-
use std::borrow::Cow;
2621
use std::fs::File;
27-
use std::io::{self, BufRead, BufReader, BufWriter, Write, stdin, stdout, stderr};
22+
use std::io::{self, BufReader, BufWriter, Write, stdin, stdout, stderr};
2823
use std::path::{Path, PathBuf};
2924
use std::str::FromStr;
3025
use std::process::exit;
3126

3227
mod tests;
3328

34-
lazy_static! {
35-
// NOTE: Use [[:alnum::]] instead of \w to only match ASCII word characters, not unicode
36-
static ref MANGLED_NAME_PATTERN: Regex = Regex::new(r"_(ZN|R)[\$\._[:alnum:]]*").unwrap();
37-
}
38-
39-
#[inline] // Except for the nested functions (which don't count), this is a very small function
40-
pub fn demangle_line(line: &str, include_hash: bool) -> Cow<str> {
41-
MANGLED_NAME_PATTERN.replace_all(line, |captures: &Captures| {
42-
let demangled = demangle(&captures[0]);
43-
if include_hash {
44-
demangled.to_string()
45-
} else {
46-
// Use alternate formatting to exclude the hash from the result
47-
format!("{:#}", demangled)
48-
}
49-
})
50-
}
51-
52-
fn demangle_stream<R: BufRead, W: Write>(input: &mut R, output: &mut W, include_hash: bool) -> io::Result<()> {
53-
// NOTE: this is actually more efficient than lines(), since it re-uses the buffer
54-
let mut buf = String::new();
55-
while input.read_line(&mut buf)? > 0 {
56-
{
57-
// NOTE: This includes the line-ending, and leaves it untouched
58-
let demangled_line = demangle_line(&buf, include_hash);
59-
if cfg!(debug_assertions) && buf.ends_with('\n') {
60-
let line_ending = if buf.ends_with("\r\n") { "\r\n" } else { "\n" };
61-
debug_assert!(demangled_line.ends_with(line_ending), "Demangled line has incorrect line ending");
62-
}
63-
output.write_all(demangled_line.as_bytes())?;
64-
}
65-
buf.clear(); // Reset the buffer's position, without freeing it's underlying memory
66-
}
67-
Ok(()) // Successfully hit EOF
68-
}
69-
7029
enum InputType {
7130
Stdin,
7231
File(PathBuf)
@@ -133,7 +92,7 @@ impl OutputType {
13392
#[inline] // It's only used twice ;)
13493
fn demangle_names_to<S: AsRef<str>, O: io::Write>(names: &[S], output: &mut O, include_hash: bool) -> io::Result<()> {
13594
for name in names {
136-
let demangled = demangle(name.as_ref());
95+
let demangled = rustc_demangle::demangle(name.as_ref());
13796
if include_hash {
13897
writeln!(output, "{}", demangled)?
13998
} else {

src/tests.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![cfg(test)]
2-
///! Tests demangle_line, based upon the assumption rustc_demangle works properly
2+
///! Tests demangle_stream, based upon the assumption rustc_demangle works properly
33
44
use rustc_demangle::demangle;
5-
use super::demangle_line;
65
use super::demangle_stream;
76

87
static MANGLED_NAMES: &'static [&'static str] = &[
@@ -24,6 +23,12 @@ static MANGLED_NAMES: &'static [&'static str] = &[
2423
"_RNvNvCs1234_7mycrate4QUUX3FOO",
2524
];
2625

26+
fn demangle_line(s: &str, include_hash: bool) -> std::borrow::Cow<str> {
27+
let mut out = Vec::new();
28+
demangle_stream(&mut s.as_bytes(), &mut out, include_hash).unwrap();
29+
std::borrow::Cow::Owned(String::from_utf8(out).unwrap())
30+
}
31+
2732
#[test]
2833
fn ignores_text() {
2934
for text in &["boom de yada\tboom de yada\n", "bananas are fun for everyone"] {

0 commit comments

Comments
 (0)