Skip to content

Commit

Permalink
Avoid array overallocation when parsing background shorthand. r=xidorn
Browse files Browse the repository at this point in the history
This is part 2 of the fix for Gecko bug 1397614
<https://bugzilla.mozilla.org/show_bug.cgi?id=1397614>
  • Loading branch information
bzbarsky committed Sep 9, 2017
1 parent 26b3924 commit 385e34a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions components/style/properties/shorthand/background.mako.rs
Expand Up @@ -37,7 +37,11 @@
let mut background_color = None;

% for name in "image position_x position_y repeat size attachment origin clip".split():
let mut background_${name} = background_${name}::SpecifiedValue(Vec::new());
// Vec grows from 0 to 4 by default on first push(). So allocate
// with capacity 1, so in the 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 background_${name} = background_${name}::SpecifiedValue(Vec::with_capacity(1));
% endfor
input.parse_comma_separated(|input| {
// background-color can only be in the last element, so if it
Expand Down Expand Up @@ -197,8 +201,12 @@

pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
let mut position_x = background_position_x::SpecifiedValue(Vec::new());
let mut position_y = background_position_y::SpecifiedValue(Vec::new());
// Vec grows from 0 to 4 by default on first push(). So allocate with
// capacity 1, so in the 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 position_x = background_position_x::SpecifiedValue(Vec::with_capacity(1));
let mut position_y = background_position_y::SpecifiedValue(Vec::with_capacity(1));
let mut any = false;

input.parse_comma_separated(|input| {
Expand Down
14 changes: 11 additions & 3 deletions components/style/properties/shorthand/mask.mako.rs
Expand Up @@ -38,7 +38,11 @@
pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
let mut mask_${name} = mask_${name}::SpecifiedValue(Vec::new());
// Vec grows from 0 to 4 by default on first push(). So allocate
// with capacity 1, so in the 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 mask_${name} = mask_${name}::SpecifiedValue(Vec::with_capacity(1));
% endfor

input.parse_comma_separated(|input| {
Expand Down Expand Up @@ -183,8 +187,12 @@

pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
let mut position_x = mask_position_x::SpecifiedValue(Vec::new());
let mut position_y = mask_position_y::SpecifiedValue(Vec::new());
// Vec grows from 0 to 4 by default on first push(). So allocate with
// capacity 1, so in the 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 position_x = mask_position_x::SpecifiedValue(Vec::with_capacity(1));
let mut position_y = mask_position_y::SpecifiedValue(Vec::with_capacity(1));
let mut any = false;

input.parse_comma_separated(|input| {
Expand Down

0 comments on commit 385e34a

Please sign in to comment.