-
zimmermann@webkit.org authored
Reviewed by Darin Adler. String operator+ reallocates unnecessarily when concatting > 2 strings https://bugs.webkit.org/show_bug.cgi?id=58420 Provide a faster String append operator. Up until now, "String operator+(const String& a, const String& b)" copied String a into a temporary object, and used a.append(b), which reallocates a new buffer of aLength+bLength. When concatting N strings using operator+, this leads to N-1 reallocations. Replace this with a flexible operator+ implementation, that avoids these reallocations. When concatting a 'String' with any string type (char*, UChar, Vector<char>, String, AtomicString, etc..) a StringAppend<String, T> object is created, which holds the intermediate string objects, and delays creation of the final string, until operator String() is invoked. template<typename T> StringAppend<String, T> operator+(const String& string1, T string2) { return StringAppend<String, T>(string1, string2); } template<typename U, typename V, typename W> StringAppend<U, StringAppend<V, W> > operator+(U string1, const StringAppend<V, W>& string2) { return StringAppend<U, StringAppend<V, W> >(string1, string2); } When concatting three strings - "String a, b, c; String result = a + b + c;" following happens: first a StringAppend<String, String> object is created by operator+(const String& string1, String string2). Then operator+(String string1, const StringAppend<String, String>& string2) is invoked, which returns a StringAppend<String, StringAppend<String, String> > object. Then operator String() is invoked, which allocates a StringImpl object, once, large enough to hold the final string - it uses tryMakeString provided by StringConcatenate.h under the hoods, which guards us against too big string allocations, etc. Note that the second template, defines a recursive way to concat an arbitary number of strings into a single String with just one allocation. * GNUmakefile.list.am: Add StringOperators.h to build. * JavaScriptCore.exp: Export WTF::emptyString(). Remove no longer needed symbols. * JavaScriptCore.gypi: Add StringOperators.h to build. * JavaScriptCore.vcproj/WTF/WTF.vcproj: Ditto. * JavaScriptCore.xcodeproj/project.pbxproj: Ditto. * wtf/text/AtomicString.h: Pull in StringConcatenate.h at the end of the file. * wtf/text/StringConcatenate.h: Conditionally include AtomicString.h to avoid a cyclic dependency. Pull in StringOperators.h at the end of the file. * wtf/text/StringOperators.h: Added. This is never meant to be included directly, including either WTFString.h or AtomicString.h automatically pulls in this file. (WTF::StringAppend::StringAppend): (WTF::StringAppend::operator String): (WTF::StringAppend::operator AtomicString): (WTF::StringAppend::writeTo): (WTF::StringAppend::length): (WTF::operator+): * wtf/text/WTFString.cpp: Remove operator+ implementations that use String::append(). (WTF::emptyString): Add new shared empty string free function. * wtf/text/WTFString.h: Replace operator+ implementations by StringAppend template solution. Pull in AtomicString.h at the end of the file. 2011-05-12 Nikolas Zimmermann <nzimmermann@rim.com> Reviewed by Darin Adler. String operator+ reallocates unnecessary when concatting > 2 strings https://bugs.webkit.org/show_bug.cgi?id=58420 Provide a faster String append operator. See Source/JavaScriptCore/ChangeLog for details. * dom/XMLDocumentParserLibxml2.cpp: (WebCore::handleElementAttributes): * editing/MarkupAccumulator.cpp: (WebCore::MarkupAccumulator::shouldAddNamespaceElement): * html/HTMLAnchorElement.cpp: (WebCore::HTMLAnchorElement::hash): (WebCore::HTMLAnchorElement::search): * html/ImageInputType.cpp: (WebCore::ImageInputType::appendFormData): * html/parser/HTMLTreeBuilder.cpp: * loader/CrossOriginAccessControl.cpp: (WebCore::passesAccessControlCheck): * page/Location.cpp: (WebCore::Location::search): (WebCore::Location::hash): * page/NavigatorBase.cpp: (WebCore::NavigatorBase::platform): * platform/chromium/ClipboardChromium.cpp: (WebCore::writeImageToDataObject): * platform/gtk/PasteboardHelper.cpp: (WebCore::PasteboardHelper::fillSelectionData): * platform/network/cf/ResourceHandleCFNet.cpp: (WebCore::encodeBasicAuthorization): * platform/network/cf/SocketStreamHandleCFNet.cpp: (WebCore::SocketStreamHandle::copyCFStreamDescription): * platform/network/mac/ResourceHandleMac.mm: (WebCore::encodeBasicAuthorization): * workers/WorkerLocation.cpp: (WebCore::WorkerLocation::search): (WebCore::WorkerLocation::hash): 2011-05-12 Nikolas Zimmermann <nzimmermann@rim.com> Reviewed by Darin Adler. String operator+ reallocates unnecessarily when concatting > 2 strings https://bugs.webkit.org/show_bug.cgi?id=58420 Provide a faster String append operator. See Source/JavaScriptCore/ChangeLog for details. * src/WebAccessibilityObject.cpp: (WebKit::WebAccessibilityObject::keyboardShortcut): Cast to String first, before trying to convert to platform dependant type. * src/WebHTTPLoadInfo.cpp: (WebKit::addHeader): Don't pass WebString to makeString, explicit cast to String first. * tests/IDBLevelDBCodingTest.cpp: Cast to String first, to avoid conflicting with gtests global templatified operator+. (IDBLevelDBCoding::TEST): 2011-05-12 Nikolas Zimmermann <nzimmermann@rim.com> Reviewed by Darin Adler. String operator+ reallocates unnecessarily when concatting > 2 strings https://bugs.webkit.org/show_bug.cgi?id=58420 Provide a faster String append operator. See Source/JavaScriptCore/ChangeLog for details. * WebView/WebFrame.mm: Explicitely cast to Strings first, so operator NSString*() can be invoked. (-[WebFrame _stringWithDocumentTypeStringAndMarkupString:]): 2011-05-12 Nikolas Zimmermann <nzimmermann@rim.com> Reviewed by Darin Adler. String operator+ reallocates unnecessarily when concatting > 2 strings https://bugs.webkit.org/show_bug.cgi?id=58420 Provide a faster String append operator. See Source/JavaScriptCore/ChangeLog for details. * AccessibleBase.cpp: (AccessibleBase::get_accKeyboardShortcut): Explicitely cast to Strings first, so operator BString() can be invoked. git-svn-id: http://svn.webkit.org/repository/webkit/trunk@86330 268f45cc-cd09-0410-ab3c-d52691b4dbfc
6da15387