Skip to content

Commit 8164dcd

Browse files
committed
Switch from HashMap to TreeMap for tokenizer profiling
Because HashMap requires libstd and a task-local RNG (by default).
1 parent 6c7f7d4 commit 8164dcd

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/tokenizer/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ use core::mem::replace;
3434
use core::iter::AdditiveIterator;
3535
use core::default::Default;
3636
use alloc::boxed::Box;
37-
use collections::MutableSeq;
37+
use collections::{MutableSeq, MutableMap};
3838
use collections::vec::Vec;
3939
use collections::string::String;
4040
use collections::str::{MaybeOwned, Slice, Owned};
41-
use std::collections::hashmap::HashMap;
41+
use collections::treemap::TreeMap;
4242

4343
pub mod states;
4444
mod interface;
@@ -167,7 +167,7 @@ pub struct Tokenizer<'sink, Sink> {
167167
temp_buf: String,
168168

169169
/// Record of how many ns we spent in each state, if profiling is enabled.
170-
state_profile: HashMap<states::State, u64>,
170+
state_profile: TreeMap<states::State, u64>,
171171

172172
/// Record of how many ns we spent in the token sink.
173173
time_in_sink: u64,
@@ -201,7 +201,7 @@ impl<'sink, Sink: TokenSink> Tokenizer<'sink, Sink> {
201201
current_doctype: Doctype::new(),
202202
last_start_tag_name: start_tag_name,
203203
temp_buf: empty_str(),
204-
state_profile: HashMap::new(),
204+
state_profile: TreeMap::new(),
205205
time_in_sink: 0,
206206
}
207207
}
@@ -336,7 +336,17 @@ impl<'sink, Sink: TokenSink> Tokenizer<'sink, Sink> {
336336
let old_sink = self.time_in_sink;
337337
let (run, mut dt) = time!(self.step());
338338
dt -= (self.time_in_sink - old_sink);
339-
self.state_profile.insert_or_update_with(state, dt, |_, x| *x += dt);
339+
let new = match self.state_profile.find_mut(&state) {
340+
Some(x) => {
341+
*x += dt;
342+
false
343+
}
344+
None => true,
345+
};
346+
if new {
347+
// do this here because of borrow shenanigans
348+
self.state_profile.insert(state, dt);
349+
}
340350
if !run { break; }
341351
}
342352
} else {

src/tokenizer/states.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414
1515
use core::prelude::*;
1616

17-
#[deriving(PartialEq, Eq, Clone, Hash)]
17+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
1818
pub enum ScriptEscapeKind {
1919
Escaped,
2020
DoubleEscaped,
2121
}
2222

23-
#[deriving(PartialEq, Eq, Clone, Hash)]
23+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
2424
pub enum DoctypeIdKind {
2525
Public,
2626
System,
2727
}
2828

29-
#[deriving(PartialEq, Eq, Clone, Hash)]
29+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
3030
pub enum RawKind {
3131
Rcdata,
3232
Rawtext,
3333
ScriptData,
3434
ScriptDataEscaped(ScriptEscapeKind),
3535
}
3636

37-
#[deriving(PartialEq, Eq, Clone, Hash)]
37+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
3838
pub enum AttrValueKind {
3939
Unquoted,
4040
SingleQuoted,
4141
DoubleQuoted,
4242
}
4343

44-
#[deriving(PartialEq, Eq, Clone, Hash)]
44+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
4545
pub enum State {
4646
Data,
4747
Plaintext,

0 commit comments

Comments
 (0)