WebKitCSSTransformValue::customCssText() should always allocate once
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Jan 2013 20:23:32 +0000 (20:23 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Jan 2013 20:23:32 +0000 (20:23 +0000)
https://bugs.webkit.org/show_bug.cgi?id=105445

Reviewed by Alexis Menard.

When converting a WebKitCSSTransformValue to a String representation,
it was typically done with 2 memory allocations due to the string size in StringBuilder.

This patch changes the code to use the String Concatenate operations in order
to create the string at once.

The opening parenthesis is also moved in the prefix name in order to maximize
the range copied with memcopy.

* css/WebKitCSSTransformValue.cpp:
(WebCore::transformValueToCssString): New generic function for boxing values.
(WebCore::WebKitCSSTransformValue::customCssText):
(WebCore::WebKitCSSTransformValue::customSerializeResolvingVariables):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@138829 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/css/WebKitCSSTransformValue.cpp

index 92b35a8..9b57d12 100644 (file)
@@ -1,3 +1,24 @@
+2013-01-04  Benjamin Poulain  <benjamin@webkit.org>
+
+        WebKitCSSTransformValue::customCssText() should always allocate once
+        https://bugs.webkit.org/show_bug.cgi?id=105445
+
+        Reviewed by Alexis Menard.
+
+        When converting a WebKitCSSTransformValue to a String representation,
+        it was typically done with 2 memory allocations due to the string size in StringBuilder.
+
+        This patch changes the code to use the String Concatenate operations in order
+        to create the string at once.
+
+        The opening parenthesis is also moved in the prefix name in order to maximize
+        the range copied with memcopy.
+
+        * css/WebKitCSSTransformValue.cpp:
+        (WebCore::transformValueToCssString): New generic function for boxing values.
+        (WebCore::WebKitCSSTransformValue::customCssText):
+        (WebCore::WebKitCSSTransformValue::customSerializeResolvingVariables):
+
 2013-01-04  James Robinson  <jamesr@chromium.org>
 
         [chromium] WebTransformationMatrix shouldn't alias memory for TransformationMatrix
 2013-01-04  James Robinson  <jamesr@chromium.org>
 
         [chromium] WebTransformationMatrix shouldn't alias memory for TransformationMatrix
index 2f86ad9..364eb02 100644 (file)
 #include "CSSValueList.h"
 #include "WebCoreMemoryInstrumentation.h"
 #include <wtf/PassRefPtr.h>
 #include "CSSValueList.h"
 #include "WebCoreMemoryInstrumentation.h"
 #include <wtf/PassRefPtr.h>
-#include <wtf/text/StringBuilder.h>
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
 // These names must be kept in sync with TransformOperationType.
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
 // These names must be kept in sync with TransformOperationType.
-const char* const transformName[] = {
-     0,
-     "translate",
-     "translateX",
-     "translateY",
-     "rotate",
-     "scale",
-     "scaleX",
-     "scaleY",
-     "skew",
-     "skewX",
-     "skewY",
-     "matrix",
-     "translateZ",
-     "translate3d",
-     "rotateX",
-     "rotateY",
-     "rotateZ",
-     "rotate3d",
-     "scaleZ",
-     "scale3d",
-     "perspective",
-     "matrix3d"
+const char* const transformNamePrefixes[] = {
+    0,
+    "translate(",
+    "translateX(",
+    "translateY(",
+    "rotate(",
+    "scale(",
+    "scaleX(",
+    "scaleY(",
+    "skew(",
+    "skewX(",
+    "skewY(",
+    "matrix(",
+    "translateZ(",
+    "translate3d(",
+    "rotateX(",
+    "rotateY(",
+    "rotateZ(",
+    "rotate3d(",
+    "scaleZ(",
+    "scale3d(",
+    "perspective(",
+    "matrix3d("
 };
 
 };
 
+static inline String transformValueToCssString(WebKitCSSTransformValue::TransformOperationType operation, const String& value)
+{
+    if (operation != WebKitCSSTransformValue::UnknownTransformOperation) {
+        ASSERT(static_cast<size_t>(operation) < WTF_ARRAY_LENGTH(transformNamePrefixes));
+        return makeString(transformNamePrefixes[operation], value, ')');
+    }
+    return String();
+}
+
 WebKitCSSTransformValue::WebKitCSSTransformValue(TransformOperationType op)
     : CSSValueList(WebKitCSSTransformClass, CommaSeparator)
     , m_type(op)
 WebKitCSSTransformValue::WebKitCSSTransformValue(TransformOperationType op)
     : CSSValueList(WebKitCSSTransformClass, CommaSeparator)
     , m_type(op)
@@ -68,29 +76,13 @@ WebKitCSSTransformValue::WebKitCSSTransformValue(TransformOperationType op)
 
 String WebKitCSSTransformValue::customCssText() const
 {
 
 String WebKitCSSTransformValue::customCssText() const
 {
-    StringBuilder result;
-    if (m_type != UnknownTransformOperation) {
-        ASSERT(static_cast<size_t>(m_type) < WTF_ARRAY_LENGTH(transformName));
-        result.append(transformName[m_type]);
-        result.append('(');
-        result.append(CSSValueList::customCssText());
-        result.append(')');
-    }
-    return result.toString();
+    return transformValueToCssString(m_type, CSSValueList::customCssText());
 }
 
 #if ENABLE(CSS_VARIABLES)
 String WebKitCSSTransformValue::customSerializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
 {
 }
 
 #if ENABLE(CSS_VARIABLES)
 String WebKitCSSTransformValue::customSerializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
 {
-    StringBuilder result;
-    if (m_type != UnknownTransformOperation) {
-        ASSERT(static_cast<size_t>(m_type) < WTF_ARRAY_LENGTH(transformName));
-        result.append(transformName[m_type]);
-        result.append('(');
-        result.append(CSSValueList::customSerializeResolvingVariables(variables));
-        result.append(')');
-    }
-    return result.toString();
+    return transformValueToCssString(m_type, CSSValueList::customSerializeResolvingVariables(variables));
 }
 #endif
 
 }
 #endif