Title: [153005] trunk/Source/WebCore
Revision
153005
Author
[email protected]
Date
2013-07-22 15:23:14 -0700 (Mon, 22 Jul 2013)

Log Message

Do not allocate 2 AtomicString just to do a comparison in HTMLAnchorElement::setRel()
https://bugs.webkit.org/show_bug.cgi?id=118941

Reviewed by Gavin Barraclough.

Currently, the only type of link relation supported by HTMLAnchorElement is RelationNoReferrer.

To find the value, we create a SpaceSplitString with the input value of the attribute (which
create one or more AtomicString depending on the input). Then we create a new AtomicString for
the literal "noreferrer". Finally, we compare the pointers and throw away all the AtomicStrings.

This causes a lot of memory operations for something really simple.

This patch adds a little helper method to SpaceSplitString to find a literal in the input. The only
allocation happens if we need to foldCase(). The following operations are done without allocating
new buffer and without hashing the input.

* dom/SpaceSplitString.cpp:
(WebCore::tokenizeSpaceSplitString):
(WebCore::AppendTokenToVectorTokenProcessor::AppendTokenToVectorTokenProcessor):
(WebCore::AppendTokenToVectorTokenProcessor::processToken):
(WebCore::SpaceSplitStringData::createVector):
(WebCore::TokenIsEqualToCStringTokenProcessor::TokenIsEqualToCStringTokenProcessor):
(WebCore::TokenIsEqualToCStringTokenProcessor::processToken):
(WebCore::TokenIsEqualToCStringTokenProcessor::referenceStringWasFound):
(WebCore::SpaceSplitString::spaceSplitStringContainsValue):
* dom/SpaceSplitString.h:
(WebCore::SpaceSplitString::spaceSplitStringContainsValue):
* html/HTMLAnchorElement.cpp:
(WebCore::HTMLAnchorElement::setRel):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (153004 => 153005)


--- trunk/Source/WebCore/ChangeLog	2013-07-22 22:17:02 UTC (rev 153004)
+++ trunk/Source/WebCore/ChangeLog	2013-07-22 22:23:14 UTC (rev 153005)
@@ -1,3 +1,36 @@
+2013-07-22  Benjamin Poulain  <[email protected]>
+
+        Do not allocate 2 AtomicString just to do a comparison in HTMLAnchorElement::setRel()
+        https://bugs.webkit.org/show_bug.cgi?id=118941
+
+        Reviewed by Gavin Barraclough.
+
+        Currently, the only type of link relation supported by HTMLAnchorElement is RelationNoReferrer.
+
+        To find the value, we create a SpaceSplitString with the input value of the attribute (which
+        create one or more AtomicString depending on the input). Then we create a new AtomicString for
+        the literal "noreferrer". Finally, we compare the pointers and throw away all the AtomicStrings.
+
+        This causes a lot of memory operations for something really simple.
+
+        This patch adds a little helper method to SpaceSplitString to find a literal in the input. The only
+        allocation happens if we need to foldCase(). The following operations are done without allocating
+        new buffer and without hashing the input.
+
+        * dom/SpaceSplitString.cpp:
+        (WebCore::tokenizeSpaceSplitString):
+        (WebCore::AppendTokenToVectorTokenProcessor::AppendTokenToVectorTokenProcessor):
+        (WebCore::AppendTokenToVectorTokenProcessor::processToken):
+        (WebCore::SpaceSplitStringData::createVector):
+        (WebCore::TokenIsEqualToCStringTokenProcessor::TokenIsEqualToCStringTokenProcessor):
+        (WebCore::TokenIsEqualToCStringTokenProcessor::processToken):
+        (WebCore::TokenIsEqualToCStringTokenProcessor::referenceStringWasFound):
+        (WebCore::SpaceSplitString::spaceSplitStringContainsValue):
+        * dom/SpaceSplitString.h:
+        (WebCore::SpaceSplitString::spaceSplitStringContainsValue):
+        * html/HTMLAnchorElement.cpp:
+        (WebCore::HTMLAnchorElement::setRel):
+
 2013-07-22  Zalan Bujtas  <[email protected]>
 
         segfault in RenderLayerCompositor when the iframe's position attribute is changed and it embeds <object>.

Modified: trunk/Source/WebCore/dom/SpaceSplitString.cpp (153004 => 153005)


--- trunk/Source/WebCore/dom/SpaceSplitString.cpp	2013-07-22 22:17:02 UTC (rev 153004)
+++ trunk/Source/WebCore/dom/SpaceSplitString.cpp	2013-07-22 22:23:14 UTC (rev 153005)
@@ -53,8 +53,8 @@
     return hasNonASCIIOrUpper(string.characters16(), length);
 }
 
-template <typename CharacterType>
-inline void SpaceSplitStringData::createVector(const CharacterType* characters, unsigned length)
+template <typename CharacterType, typename TokenProcessor>
+static inline void tokenizeSpaceSplitString(TokenProcessor& tokenProcessor, const CharacterType* characters, unsigned length)
 {
     unsigned start = 0;
     while (true) {
@@ -66,22 +66,38 @@
         while (end < length && isNotHTMLSpace(characters[end]))
             ++end;
 
-        m_vector.append(AtomicString(characters + start, end - start));
+        if (!tokenProcessor.processToken(characters + start, end - start))
+            return;
 
         start = end + 1;
     }
 }
 
+class AppendTokenToVectorTokenProcessor {
+public:
+    AppendTokenToVectorTokenProcessor(Vector<AtomicString, 4>& vector) : m_vector(vector) { }
+
+    template <typename CharacterType>
+    bool processToken(const CharacterType* characters, unsigned length)
+    {
+        m_vector.append(AtomicString(characters, length));
+        return true;
+    }
+private:
+    Vector<AtomicString, 4>& m_vector;
+};
+
 void SpaceSplitStringData::createVector(const String& string)
 {
     unsigned length = string.length();
 
+    AppendTokenToVectorTokenProcessor tokenProcessor(m_vector);
     if (string.is8Bit()) {
-        createVector(string.characters8(), length);
+        tokenizeSpaceSplitString(tokenProcessor, string.characters8(), length);
         return;
     }
 
-    createVector(string.characters16(), length);
+    tokenizeSpaceSplitString(tokenProcessor, string.characters16(), length);
 }
 
 bool SpaceSplitStringData::containsAll(SpaceSplitStringData& other)
@@ -168,6 +184,48 @@
     m_data = SpaceSplitStringData::create(string);
 }
 
+class TokenIsEqualToCStringTokenProcessor {
+public:
+    TokenIsEqualToCStringTokenProcessor(const char* referenceString, unsigned referenceStringLength)
+        : m_referenceString(referenceString)
+        , m_referenceStringLength(referenceStringLength)
+        , m_referenceStringWasFound(false)
+    {
+    }
+
+    template <typename CharacterType>
+    bool processToken(const CharacterType* characters, unsigned length)
+    {
+        if (length == m_referenceStringLength && equal(characters, reinterpret_cast<const LChar*>(m_referenceString), length)) {
+            m_referenceStringWasFound = true;
+            return false;
+        }
+        return true;
+    }
+
+    bool referenceStringWasFound() const { return m_referenceStringWasFound; }
+
+private:
+    const char* m_referenceString;
+    unsigned m_referenceStringLength;
+    bool m_referenceStringWasFound;
+};
+
+bool SpaceSplitString::spaceSplitStringContainsValue(const String& inputString, const char* value, unsigned valueLength, bool shouldFoldCase)
+{
+    String string = inputString;
+    if (shouldFoldCase && hasNonASCIIOrUpper(string))
+        string = string.foldCase();
+
+    TokenIsEqualToCStringTokenProcessor tokenProcessor(value, valueLength);
+    unsigned length = string.length();
+    if (string.is8Bit())
+        tokenizeSpaceSplitString(tokenProcessor, string.characters8(), length);
+    else
+        tokenizeSpaceSplitString(tokenProcessor, string.characters16(), length);
+    return tokenProcessor.referenceStringWasFound();
+}
+
 SpaceSplitStringData::~SpaceSplitStringData()
 {
     if (!m_keyString.isNull())

Modified: trunk/Source/WebCore/dom/SpaceSplitString.h (153004 => 153005)


--- trunk/Source/WebCore/dom/SpaceSplitString.h	2013-07-22 22:17:02 UTC (rev 153004)
+++ trunk/Source/WebCore/dom/SpaceSplitString.h	2013-07-22 22:23:14 UTC (rev 153005)
@@ -84,6 +84,13 @@
         bool isNull() const { return !m_data; }
         const AtomicString& operator[](size_t i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return (*m_data)[i]; }
 
+        static bool spaceSplitStringContainsValue(const String& spaceSplitString, const char* value, unsigned length, bool shouldFoldCase);
+        template<size_t length>
+        static bool spaceSplitStringContainsValue(const String& spaceSplitString, const char (&value)[length], bool shouldFoldCase)
+        {
+            return spaceSplitStringContainsValue(spaceSplitString, value, length - 1, shouldFoldCase);
+        }
+
     private:
         void ensureUnique()
         {

Modified: trunk/Source/WebCore/html/HTMLAnchorElement.cpp (153004 => 153005)


--- trunk/Source/WebCore/html/HTMLAnchorElement.cpp	2013-07-22 22:17:02 UTC (rev 153004)
+++ trunk/Source/WebCore/html/HTMLAnchorElement.cpp	2013-07-22 22:23:14 UTC (rev 153005)
@@ -310,10 +310,7 @@
 
 void HTMLAnchorElement::setRel(const String& value)
 {
-    m_linkRelations = 0;
-    SpaceSplitString newLinkRelations(value, true);
-    // FIXME: Add link relations as they are implemented
-    if (newLinkRelations.contains("noreferrer"))
+    if (SpaceSplitString::spaceSplitStringContainsValue(value, "noreferrer", true))
         m_linkRelations |= RelationNoReferrer;
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to