Modernize QualifiedName by wrapping gNameCache in a function and using more early...
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jul 2013 05:07:45 +0000 (05:07 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jul 2013 05:07:45 +0000 (05:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=118299

Reviewed by Andreas Kling.

Did cleanups.

* dom/QualifiedName.cpp:
(WebCore::qualifiedNameCache): Added to wrap gNameCache.
(WebCore::QualifiedName::QualifiedName):
(WebCore::QualifiedName::QualifiedNameImpl::~QualifiedNameImpl):
(WebCore::QualifiedName::toString): Use early exit and StringBuilder.
(WebCore::QualifiedName::init): Use early exit.

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

Source/WebCore/ChangeLog
Source/WebCore/dom/QualifiedName.cpp

index 77f72366e37ce6e298780008a2a3f0e104325472..4682d90d70d55d3909deb7032017083991986775 100644 (file)
@@ -1,3 +1,19 @@
+2013-07-02  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Modernize QualifiedName by wrapping gNameCache in a function and using more early exits
+        https://bugs.webkit.org/show_bug.cgi?id=118299
+
+        Reviewed by Andreas Kling.
+
+        Did cleanups.
+
+        * dom/QualifiedName.cpp:
+        (WebCore::qualifiedNameCache): Added to wrap gNameCache.
+        (WebCore::QualifiedName::QualifiedName):
+        (WebCore::QualifiedName::QualifiedNameImpl::~QualifiedNameImpl):
+        (WebCore::QualifiedName::toString): Use early exit and StringBuilder.
+        (WebCore::QualifiedName::init): Use early exit.
+
 2013-07-02  Simon Fraser  <simon.fraser@apple.com>
 
         Don't set z-index: 0 on lots of elements with -webkit-overflow-scrolling: touch
index 6a81507adb3d2e321d4fea292bd9cec45cb1811c..587dad2e14a1cb67adc912426daef5ed37c10f11 100644 (file)
@@ -33,6 +33,7 @@
 #include <wtf/Assertions.h>
 #include <wtf/HashSet.h>
 #include <wtf/StaticConstructors.h>
+#include <wtf/text/StringBuilder.h>
 
 #if ENABLE(MATHML)
 #include "MathMLNames.h"
@@ -76,14 +77,16 @@ struct QNameComponentsTranslator {
     }
 };
 
-static QNameSet* gNameCache;
+static inline QNameSet& qualifiedNameCache()
+{
+    DEFINE_STATIC_LOCAL(QNameSet, nameCache, ());
+    return nameCache;
+}
 
 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
 {
-    if (!gNameCache)
-        gNameCache = new QNameSet;
     QualifiedNameComponents components = { p.impl(), l.impl(), n.isEmpty() ? nullAtom.impl() : n.impl() };
-    QNameSet::AddResult addResult = gNameCache->add<QNameComponentsTranslator>(components);
+    QNameSet::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(components);
     m_impl = *addResult.iterator;
     if (!addResult.isNewEntry)
         m_impl->ref();
@@ -106,19 +109,19 @@ void QualifiedName::deref()
 
 QualifiedName::QualifiedNameImpl::~QualifiedNameImpl()
 {
-    gNameCache->remove(this);
+    qualifiedNameCache().remove(this);
 }
 
 String QualifiedName::toString() const
 {
-    String local = localName();
-    if (hasPrefix()) {
-        String result = prefix().string();
-        result.append(":");
-        result.append(local);
-        return result;
-    }
-    return local;
+    if (!hasPrefix())
+        return localName();
+
+    StringBuilder result;
+    result.append(prefix());
+    result.append(':');
+    result.append(localName());
+    return result.toString();
 }
 
 // Global init routines
@@ -126,14 +129,14 @@ DEFINE_GLOBAL(QualifiedName, anyName, nullAtom, starAtom, starAtom)
 
 void QualifiedName::init()
 {
-    static bool initialized;
-    if (!initialized) {
-        // Use placement new to initialize the globals.
-        
-        AtomicString::init();
-        new (NotNull, (void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom);
-        initialized = true;
-    }
+    static bool initialized = false;
+    if (initialized)
+        return;
+
+    // Use placement new to initialize the globals.
+    AtomicString::init();
+    new (NotNull, (void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom);
+    initialized = true;
 }
 
 const QualifiedName& nullQName()