Skip to content

Commit 7c6c986

Browse files
author
bors-servo
authored
Auto merge of #195 - bzbarsky:avoid-list-overallocation, r=SimonSapin
Avoid array overallocation when parsing background-image property Part 3 of the fix for Gecko bug 1397614 <https://bugzilla.mozilla.org/show_bug.cgi?id=1397614>. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/195) <!-- Reviewable:end -->
2 parents 898ae34 + df1e23c commit 7c6c986

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.20.1"
3+
version = "0.20.2"
44
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"

src/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ impl<'i: 't, 't> Parser<'i, 't> {
478478
#[inline]
479479
pub fn parse_comma_separated<F, T, E>(&mut self, mut parse_one: F) -> Result<Vec<T>, ParseError<'i, E>>
480480
where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>> {
481-
let mut values = vec![];
481+
// Vec grows from 0 to 4 by default on first push(). So allocate with
482+
// capacity 1, so in the somewhat common case of only one item we don't
483+
// way overallocate. Note that we always push at least one item if
484+
// parsing succeeds.
485+
let mut values = Vec::with_capacity(1);
482486
loop {
483487
self.skip_whitespace(); // Unnecessary for correctness, but may help try() in parse_one rewind less.
484488
values.push(self.parse_until_before(Delimiter::Comma, &mut parse_one)?);

0 commit comments

Comments
 (0)