From df1e23c6fb5f3968b9024e6e5fead9c2176835f8 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 11 Sep 2017 10:08:23 -0400 Subject: [PATCH] Avoid array overallocation when parsing background-image property. r=SimonSapin Part 3 of the fix for Gecko bug 1397614 . --- Cargo.toml | 2 +- src/parser.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c409bac9..7d1e6a5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssparser" -version = "0.20.1" +version = "0.20.2" authors = [ "Simon Sapin " ] description = "Rust implementation of CSS Syntax Level 3" diff --git a/src/parser.rs b/src/parser.rs index 93a40a62..4887becd 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -478,7 +478,11 @@ impl<'i: 't, 't> Parser<'i, 't> { #[inline] pub fn parse_comma_separated(&mut self, mut parse_one: F) -> Result, ParseError<'i, E>> where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result> { - let mut values = vec![]; + // Vec grows from 0 to 4 by default on first push(). So allocate with + // capacity 1, so in the somewhat common case of only one item we don't + // way overallocate. Note that we always push at least one item if + // parsing succeeds. + let mut values = Vec::with_capacity(1); loop { self.skip_whitespace(); // Unnecessary for correctness, but may help try() in parse_one rewind less. values.push(self.parse_until_before(Delimiter::Comma, &mut parse_one)?);