From e9fc22ea2dd98ecba54a1379a7970a204d9d2bf7 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 25 Aug 2017 22:01:38 +0200 Subject: [PATCH] Use a SmallVec instead of Vec in consume_until_end_of_block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … to avoid allocating in common cases. --- Cargo.toml | 1 + src/lib.rs | 1 + src/parser.rs | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da3444ba..b2120114 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ matches = "0.1" phf = "0.7" procedural-masquerade = {path = "./procedural-masquerade", version = "0.1"} serde = {version = "1.0", optional = true} +smallvec = "0.4.3" [build-dependencies] syn = "0.11" diff --git a/src/lib.rs b/src/lib.rs index 693c771a..5c6edf03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,6 +78,7 @@ extern crate dtoa_short; #[cfg(test)] extern crate rustc_serialize; #[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "heapsize")] #[macro_use] extern crate heapsize; +extern crate smallvec; pub use cssparser_macros::*; diff --git a/src/parser.rs b/src/parser.rs index 66b26e1a..74618d93 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use cow_rc_str::CowRcStr; +use smallvec::SmallVec; use std::ops::Range; use std::ascii::AsciiExt; use std::ops::BitOr; @@ -858,7 +859,8 @@ pub fn parse_nested_block<'i: 't, 't, F, T, E>(parser: &mut Parser<'i, 't>, pars } fn consume_until_end_of_block(block_type: BlockType, tokenizer: &mut Tokenizer) { - let mut stack = vec![block_type]; + let mut stack = SmallVec::<[BlockType; 16]>::new(); + stack.push(block_type); // FIXME: have a special-purpose tokenizer method for this that does less work. while let Ok(ref token) = tokenizer.next() {