⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 106724 in webkit


Ignore:
Timestamp:
Feb 3, 2012, 5:59:41 PM (15 years ago)
Author:
benjamin@webkit.org
Message:

Reduce the memory allocations of WebCore's cssPropertyName()
https://bugs.webkit.org/show_bug.cgi?id=74782

Patch by Benjamin Poulain <bpoulain@apple.com> on 2012-02-03
Reviewed by Geoffrey Garen.

Add a fast path to avoid the use of the StringBuilder.

The string builder is needed for two cases:
-CSS prefix (the character after the prefix must be uppercase)
-JavaScript CamelCase name for CSS properties

We can skip all memory allocations if the property is not in those
two cases. We start by testing the string for uppercase characters,
and just return the an identical string.

This patch create a "fast case" 2.7 times faster than previously.
The "slow case" is 2-3% slower due to the additional check at the beginning.

  • bindings/js/JSCSSStyleDeclarationCustom.cpp:

(WebCore):
(WebCore::containsASCIIUpperChar):
(WebCore::cssPropertyName):
(WebCore::isCSSPropertyName):

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r106723 r106724  
     12012-02-03  Benjamin Poulain  <bpoulain@apple.com>
     2
     3        Reduce the memory allocations of WebCore's cssPropertyName()
     4        https://bugs.webkit.org/show_bug.cgi?id=74782
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Add a fast path to avoid the use of the StringBuilder.
     9
     10        The string builder is needed for two cases:
     11        -CSS prefix (the character after the prefix must be uppercase)
     12        -JavaScript CamelCase name for CSS properties
     13
     14        We can skip all memory allocations if the property is not in those
     15        two cases. We start by testing the string for uppercase characters,
     16        and just return the an identical string.
     17
     18        This patch create a "fast case" 2.7 times faster than previously.
     19        The "slow case" is 2-3% slower due to the additional check at the beginning.
     20
     21        * bindings/js/JSCSSStyleDeclarationCustom.cpp:
     22        (WebCore):
     23        (WebCore::containsASCIIUpperChar):
     24        (WebCore::cssPropertyName):
     25        (WebCore::isCSSPropertyName):
     26
    1272012-02-03  Anders Carlsson  <andersca@apple.com>
    228
  • trunk/Source/WebCore/bindings/js/JSCSSStyleDeclarationCustom.cpp

    r106695 r106724  
    139139}
    140140
     141template<typename CharacterType>
     142static inline bool containsASCIIUpperChar(const CharacterType* string, size_t length)
     143{
     144    for (unsigned i = 0; i < length; ++i) {
     145        if (isASCIIUpper(string[i]))
     146            return true;
     147    }
     148    return false;
     149}
     150
     151static inline bool containsASCIIUpperChar(const StringImpl& string)
     152{
     153    if (string.is8Bit())
     154        return containsASCIIUpperChar(string.characters8(), string.length());
     155    return containsASCIIUpperChar(string.characters16(), string.length());
     156}
     157
    141158static String cssPropertyName(const Identifier& propertyName, bool* hadPixelOrPosPrefix = 0)
    142159{
     
    148165        return String();
    149166
     167    StringImpl* propertyNameString = propertyName.impl();
     168    // If there is no uppercase character in the propertyName, there can
     169    // be no prefix, nor extension and we can return the same string.
     170    if (!containsASCIIUpperChar(*propertyNameString))
     171        return String(propertyNameString);
     172
    150173    StringBuilder builder;
    151174    builder.reserveCapacity(length);
    152175
    153     const StringImpl* propertyNameString = propertyName.impl();
    154176    unsigned i = 0;
    155177    switch (getCSSPropertyNamePrefix(*propertyNameString)) {
     
    178200    }
    179201
    180     builder.append(toASCIILower(propertyName.characters()[i++]));
     202    builder.append(toASCIILower((*propertyNameString)[i++]));
    181203
    182204    for (; i < length; ++i) {
    183         UChar c = propertyName.characters()[i];
     205        UChar c = (*propertyNameString)[i];
    184206        if (!isASCIIUpper(c))
    185207            builder.append(c);
     
    193215static bool isCSSPropertyName(const Identifier& propertyIdentifier)
    194216{
    195     // FIXME: This mallocs a string for the property name and then throws it
    196     // away.  This shows up on peacekeeper's domDynamicCreationCreateElement.
    197217    return cssPropertyID(cssPropertyName(propertyIdentifier));
    198218}
Note: See TracChangeset for help on using the changeset viewer.