Bug 57964 - Fast path for parsing simple CSS values
Summary: Fast path for parsing simple CSS values
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac (Intel) OS X 10.6
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-06 11:38 PDT by Ian Henderson
Modified: 2011-04-06 16:42 PDT (History)
4 users (show)

See Also:


Attachments
proposed patch (9.84 KB, patch)
2011-04-06 11:49 PDT, Ian Henderson
koivisto: review+
koivisto: commit-queue-
Details | Formatted Diff | Diff
updated patch (11.14 KB, patch)
2011-04-06 14:45 PDT, Ian Henderson
simon.fraser: review-
simon.fraser: commit-queue-
Details | Formatted Diff | Diff
updated patch 2 (11.23 KB, patch)
2011-04-06 15:42 PDT, Ian Henderson
simon.fraser: review+
simon.fraser: commit-queue+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Henderson 2011-04-06 11:38:24 PDT
Javascript animations repeatedly set style properties of an element to values like "10px" or "5%".  We should optimize this common case.
Comment 1 Ian Henderson 2011-04-06 11:49:43 PDT
Created attachment 88473 [details]
proposed patch
Comment 2 WebKit Review Bot 2011-04-06 11:52:15 PDT
Attachment 88473 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style']" exit_code: 1

Source/WebCore/css/CSSParser.cpp:303:  Boolean expressions that span multiple lines should have their operators on the left side of the line instead of the right side.  [whitespace/operators] [4]
Source/WebCore/css/CSSParser.cpp:377:  Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.  [readability/comparison_to_zero] [5]
Total errors found: 2 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Simon Fraser (smfr) 2011-04-06 11:57:25 PDT
Comment on attachment 88473 [details]
proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=88473&action=review

> Source/WebCore/css/CSSParser.cpp:262
> +static bool isSimpleColorPropertyID(int id)

Make inline?

> Source/WebCore/css/CSSParser.cpp:352
> +static bool parseSimpleDimensionValue(CSSMutableStyleDeclaration* declaration, int id, const String& string, bool important, bool strict)

Say Length instead of DimensionValue?

> Source/WebCore/css/CSSParser.cpp:390
> +bool CSSParser::parseValue(CSSMutableStyleDeclaration* declaration, int id, const String& string, bool important, bool strict)

Please rename 'id' to 'propId' or 'propertyId'.
Comment 4 Dave Hyatt 2011-04-06 13:43:43 PDT
Comment on attachment 88473 [details]
proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=88473&action=review

>> Source/WebCore/css/CSSParser.cpp:262
>> +static bool isSimpleColorPropertyID(int id)
> 
> Make inline?

It's not clear to me why we need to call this "isSimpleColorPropertyID"... as opposed to what... a complex color property ID?  It also seems like a bunch of color properties are missing from this list. What about outline-color and all of the various SVG color properties?

> Source/WebCore/css/CSSParser.cpp:288
> +static bool parseSimpleColorValue(CSSMutableStyleDeclaration* declaration, int id, const String& string, bool important, bool strict)

Are there colors you're omitting on purpose? Still not clear on the terminology unless maybe that's the case....

> Source/WebCore/css/CSSParser.cpp:322
> +static bool isSimpleDimensionPropertyID(int id, bool& acceptsNegativeNumbers)

What about the padding properties?  Also, as SImon points out, I don't think "dimension" is a good term here, since that has meaning in CSS.  I would use Length.
Comment 5 Antti Koivisto 2011-04-06 13:48:56 PDT
Comment on attachment 88473 [details]
proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=88473&action=review

Looks good, r=me with some comments

> Source/WebCore/css/CSSParser.cpp:262
> +static bool isSimpleColorPropertyID(int id)

This could be inline.
id is too generic, call it propertyID.

> Source/WebCore/css/CSSParser.cpp:288
> +static bool parseSimpleColorValue(CSSMutableStyleDeclaration* declaration, int id, const String& string, bool important, bool strict)

id -> propertyID

> Source/WebCore/css/CSSParser.cpp:318
> +    if (validPrimitive) {
> +        CSSProperty property(id, CSSPrimitiveValue::createIdentifier(valueID), important);
> +        CSSProperty* propertyArray = &property;
> +        declaration->addParsedProperties(&propertyArray, 1);
> +        return true;
> +    }
> +    RGBA32 color;
> +    if (!CSSParser::parseColor(string, color, strict && string[0] != '#'))
> +        return false;
> +    CSSProperty property(id, CSSPrimitiveValue::createColor(color), important);
> +    CSSProperty* propertyArray = &property;
> +    declaration->addParsedProperties(&propertyArray, 1);

There is CSSMutableStyleDeclaration::addParsedProperty() for adding a single property. Perhaps you can use that?

> Source/WebCore/css/CSSParser.cpp:331
> +    case CSSPropertyWebkitLogicalHeight:
> +        acceptsNegativeNumbers = false;
> +        return true;
> +

remove empty line

> Source/WebCore/css/CSSParser.cpp:346
> +    case CSSPropertyWebkitMarginAfter:
> +        acceptsNegativeNumbers = true;
> +        return true;
> +

remove empty line

> Source/WebCore/css/CSSParser.cpp:353
> +static bool parseSimpleDimensionValue(CSSMutableStyleDeclaration* declaration, int id, const String& string, bool important, bool strict)
> +{

id -> propertyID

> Source/WebCore/css/CSSParser.cpp:386
> +    CSSProperty property(id, CSSPrimitiveValue::create(number, unit), important);
> +    CSSProperty* propertyArray = &property;
> +    declaration->addParsedProperties(&propertyArray, 1);

There is CSSMutableStyleDeclaration::addParsedProperty() for adding a single property. Perhaps you can use that?
Comment 6 Antti Koivisto 2011-04-06 13:54:02 PDT
Comment on attachment 88473 [details]
proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=88473&action=review

> Source/WebCore/css/CSSParser.cpp:308
> +        CSSProperty property(id, CSSPrimitiveValue::createIdentifier(valueID), important);

You should construct CSSPrimitiveValue using CSSPrimitiveValueCache. That hangs from the document which you can get through the style declaration.

> Source/WebCore/css/CSSParser.cpp:316
> +    CSSProperty property(id, CSSPrimitiveValue::createColor(color), important);

You should construct CSSPrimitiveValue using CSSPrimitiveValueCache. That hangs from the document which you can get through the style declaration.

> Source/WebCore/css/CSSParser.cpp:384
> +    CSSProperty property(id, CSSPrimitiveValue::create(number, unit), important);

You should construct CSSPrimitiveValue using CSSPrimitiveValueCache. That hangs from the document which you can get through the style declaration.
Comment 7 Ian Henderson 2011-04-06 14:45:01 PDT
Created attachment 88518 [details]
updated patch
Comment 8 Simon Fraser (smfr) 2011-04-06 14:58:38 PDT
Comment on attachment 88518 [details]
updated patch

View in context: https://bugs.webkit.org/attachment.cgi?id=88518&action=review

r- for the "px" parsing issue.

> Source/WebCore/css/CSSParser.cpp:281
> +    case CSSPropertyBackgroundColor:
> +    case CSSPropertyBorderTopColor:
> +    case CSSPropertyBorderRightColor:
> +    case CSSPropertyBorderBottomColor:
> +    case CSSPropertyBorderLeftColor:
> +    case CSSPropertyWebkitBorderStartColor:
> +    case CSSPropertyWebkitBorderEndColor:
> +    case CSSPropertyWebkitBorderBeforeColor:
> +    case CSSPropertyWebkitBorderAfterColor:
> +    case CSSPropertyColor:
> +    case CSSPropertyTextLineThroughColor:
> +    case CSSPropertyTextUnderlineColor:
> +    case CSSPropertyTextOverlineColor:
> +    case CSSPropertyWebkitColumnRuleColor:
> +    case CSSPropertyWebkitTextEmphasisColor:
> +    case CSSPropertyWebkitTextFillColor:
> +    case CSSPropertyWebkitTextStrokeColor:

These might be better ordered by ID, rather than alphabetically, for potential compiler optimization, and easy of matching with the enum of all properties.

What about CSSPropertyOutlineColor, CSSPropertyWebkitColumnRuleColor?

> Source/WebCore/css/CSSParser.cpp:295
> +    RefPtr<CSSPrimitiveValueCache> primitiveValueCache = stylesheet->document()->cssPrimitiveValueCache();

I don't think you need a RefPtr to the cssPrimitiveValueCache. You could also fetch it later.

> Source/WebCore/css/CSSParser.cpp:297
> +    if (!string.length())
> +        return false;

Why not check this earlier?

> Source/WebCore/css/CSSParser.cpp:374
> +    RefPtr<CSSPrimitiveValueCache> primitiveValueCache = stylesheet->document()->cssPrimitiveValueCache();

Ditto. Also, why not fetch this just before you use it lower down?

> Source/WebCore/css/CSSParser.cpp:384
> +    if (length > 2 && characters[length - 2] == 'p' && characters[length - 1] == 'x') {
> +        length -= 2;
> +        unit = CSSPrimitiveValue::CSS_PX;

This would break if we ever created a unit like "ppx" or 'screenpx". I think you need to walk back looking for non-unit characters.

> Source/WebCore/css/CSSParser.cpp:387
> +    } else if (length > 1 && characters[length - 1] == '%') {
> +        length -= 1;
> +        unit = CSSPrimitiveValue::CSS_PERCENTAGE;

Ditto if a "%%" unit was invented.
Comment 9 Ian Henderson 2011-04-06 15:42:06 PDT
Created attachment 88534 [details]
updated patch 2

Updated to address Simon's comments.  The "px" issue doesn't actually happen, since charactersToDouble validates its input.  I added a comment to emphasize this.
Comment 10 WebKit Review Bot 2011-04-06 15:44:36 PDT
Attachment 88534 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style']" exit_code: 1

Source/WebCore/css/CSSParser.cpp:386:  Should only a single space after a punctuation in a comment.  [whitespace/comments] [5]
Total errors found: 1 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 11 Simon Fraser (smfr) 2011-04-06 16:42:15 PDT
http://trac.webkit.org/changeset/83122