Skip to content

Commit 0b538dd

Browse files
author
bors-servo
authored
Auto merge of #350 - PluieElectrique:escape_log_enabled, r=jdm
Use log_enabled! to avoid calling to_escaped_string `to_escaped_string` is somewhat expensive because it allocates twice and iterates over each char individually to escape it. Furthermore, `TreeBuilder::step` produces a lot of `debug_step` messages. So, we use `log_enabled!` to not escape any strings unless debug messages are being printed.
2 parents 7ec6509 + 9748ba7 commit 0b538dd

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

html5ever/src/tree_builder/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use tokenizer::states::{RawData, RawKind};
3737
use tree_builder::types::*;
3838
use tree_builder::tag_sets::*;
3939
use util::str::to_escaped_string;
40+
use log::Level;
4041

4142
pub use self::PushFlag::*;
4243

@@ -293,8 +294,9 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
293294
}
294295

295296
fn debug_step(&self, mode: InsertionMode, token: &Token) {
296-
use util::str::to_escaped_string;
297-
debug!("processing {} in insertion mode {:?}", to_escaped_string(token), mode);
297+
if log_enabled!(Level::Debug) {
298+
debug!("processing {} in insertion mode {:?}", to_escaped_string(token), mode);
299+
}
298300
}
299301

300302
fn process_to_completion(&mut self, mut token: Token) -> TokenSinkResult<Handle> {

html5ever/src/util/str.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
use std::fmt;
1111

1212
pub fn to_escaped_string<T: fmt::Debug>(x: &T) -> String {
13-
use std::fmt::Write;
14-
1513
// FIXME: don't allocate twice
16-
let mut buf = String::new();
17-
let _ = buf.write_fmt(format_args!("{:?}", x));
18-
buf.shrink_to_fit();
19-
buf.chars().flat_map(|c| c.escape_default()).collect()
14+
let string = format!("{:?}", x);
15+
string.chars().flat_map(|c| c.escape_default()).collect()
2016
}
2117

2218
/// If `c` is an ASCII letter, return the corresponding lowercase

0 commit comments

Comments
 (0)