Changeset 127366 in webkit


Ignore:
Timestamp:
Sep 1, 2012 1:20:01 AM (12 years ago)
Author:
abarth@webkit.org
Message:

Remove all-but-one use of WTF::String::operator+= from WebCore
https://bugs.webkit.org/show_bug.cgi?id=95508

Reviewed by Benjamin Poulain.

This patch removes all the uses of WTF::String::operator+= from
WebCore, except those in WorkerScriptLoader (which need a more delicate
patch). There were actually a handful of legitimate use cases for += in
this group. I've replaced them with calls to String::append rather than
+= so that we can remove += and encourage most contributors to use
more efficient string idioms.

(There are likely some more uses in WebCore hiding in port-specific
code---this patch covers only those call sites found when compiling the
chromium-mac port.)

  • inspector/InspectorStyleTextEditor.cpp:

(WebCore::InspectorStyleTextEditor::insertProperty):

  • This complicated function looks really inefficient, but I didn't have the heart to rewrite it.
  • inspector/NetworkResourcesData.cpp:

(WebCore::NetworkResourcesData::ResourceData::decodeDataToContent):

  • loader/cache/CachedCSSStyleSheet.cpp:

(WebCore::CachedCSSStyleSheet::sheetText):
(WebCore::CachedCSSStyleSheet::data):

  • loader/cache/CachedFont.cpp:

(WebCore::CachedFont::ensureSVGFontData):

  • loader/cache/CachedScript.cpp:

(WebCore::CachedScript::script):

  • loader/cache/CachedXSLStyleSheet.cpp:

(WebCore::CachedXSLStyleSheet::data):

  • This decoder/flush pattern can probably be improved by combining the decode and flush operations, but I didn't do that in this patch.
  • page/FrameTree.cpp:

(WebCore::FrameTree::uniqueChildName):

  • I found this code very amusing. It's worried enough about efficiency to give a big speech about why snprintf is safe, but then it implicitly performs a large number of mallocs and memcpy operations.
  • page/Page.cpp:

(WebCore::Page::userStyleSheet):

  • platform/chromium/support/WebHTTPLoadInfo.cpp:

(WebKit::addHeader):

  • platform/chromium/support/WebURLResponse.cpp:

(WebKit::WebURLResponse::addHTTPHeaderField):

  • This header-appending idiom looks like a reasonable use case for String::append.
  • xml/XMLHttpRequest.cpp:

(WebCore::XMLHttpRequest::send):
(WebCore::XMLHttpRequest::setRequestHeaderInternal):

  • xml/XPathFunctions.cpp:

(WebCore::XPath::FunTranslate::evaluate):

  • Fixes 6 year old FIXME.
  • xml/parser/XMLDocumentParser.cpp:

(WebCore::XMLDocumentParser::append):

  • xml/parser/XMLDocumentParser.h:

(XMLDocumentParser):

  • xml/parser/XMLDocumentParserLibxml2.cpp:

(WebCore::XMLDocumentParser::doEnd):

  • xml/parser/XMLDocumentParserQt.cpp:

(WebCore::XMLDocumentParser::doEnd):

  • Changed m_originalSourceForTransform to a SegmentedString so that we don't need to malloc and copy the entire document every time we get more data from the network.
Location:
trunk/Source/WebCore
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r127365 r127366  
     12012-09-01  Adam Barth  <abarth@webkit.org>
     2
     3        Remove all-but-one use of WTF::String::operator+= from WebCore
     4        https://bugs.webkit.org/show_bug.cgi?id=95508
     5
     6        Reviewed by Benjamin Poulain.
     7
     8        This patch removes all the uses of WTF::String::operator+= from
     9        WebCore, except those in WorkerScriptLoader (which need a more delicate
     10        patch). There were actually a handful of legitimate use cases for += in
     11        this group. I've replaced them with calls to String::append rather than
     12        += so that we can remove += and encourage most contributors to use
     13        more efficient string idioms.
     14
     15        (There are likely some more uses in WebCore hiding in port-specific
     16        code---this patch covers only those call sites found when compiling the
     17        chromium-mac port.)
     18
     19        * inspector/InspectorStyleTextEditor.cpp:
     20        (WebCore::InspectorStyleTextEditor::insertProperty):
     21            - This complicated function looks really inefficient, but I didn't
     22              have the heart to rewrite it.
     23        * inspector/NetworkResourcesData.cpp:
     24        (WebCore::NetworkResourcesData::ResourceData::decodeDataToContent):
     25        * loader/cache/CachedCSSStyleSheet.cpp:
     26        (WebCore::CachedCSSStyleSheet::sheetText):
     27        (WebCore::CachedCSSStyleSheet::data):
     28        * loader/cache/CachedFont.cpp:
     29        (WebCore::CachedFont::ensureSVGFontData):
     30        * loader/cache/CachedScript.cpp:
     31        (WebCore::CachedScript::script):
     32        * loader/cache/CachedXSLStyleSheet.cpp:
     33        (WebCore::CachedXSLStyleSheet::data):
     34            - This decoder/flush pattern can probably be improved by combining
     35              the decode and flush operations, but I didn't do that in this
     36              patch.
     37        * page/FrameTree.cpp:
     38        (WebCore::FrameTree::uniqueChildName):
     39            - I found this code very amusing. It's worried enough about
     40              efficiency to give a big speech about why snprintf is safe, but
     41              then it implicitly performs a large number of mallocs and memcpy
     42              operations.
     43        * page/Page.cpp:
     44        (WebCore::Page::userStyleSheet):
     45        * platform/chromium/support/WebHTTPLoadInfo.cpp:
     46        (WebKit::addHeader):
     47        * platform/chromium/support/WebURLResponse.cpp:
     48        (WebKit::WebURLResponse::addHTTPHeaderField):
     49            - This header-appending idiom looks like a reasonable use case for
     50              String::append.
     51        * xml/XMLHttpRequest.cpp:
     52        (WebCore::XMLHttpRequest::send):
     53        (WebCore::XMLHttpRequest::setRequestHeaderInternal):
     54        * xml/XPathFunctions.cpp:
     55        (WebCore::XPath::FunTranslate::evaluate):
     56            - Fixes 6 year old FIXME.
     57        * xml/parser/XMLDocumentParser.cpp:
     58        (WebCore::XMLDocumentParser::append):
     59        * xml/parser/XMLDocumentParser.h:
     60        (XMLDocumentParser):
     61        * xml/parser/XMLDocumentParserLibxml2.cpp:
     62        (WebCore::XMLDocumentParser::doEnd):
     63        * xml/parser/XMLDocumentParserQt.cpp:
     64        (WebCore::XMLDocumentParser::doEnd):
     65            - Changed m_originalSourceForTransform to a SegmentedString so that
     66              we don't need to malloc and copy the entire document every time
     67              we get more data from the network.
     68
    1692012-09-01  Tommy Widenflycht  <tommyw@google.com>
    270
  • trunk/Source/WebCore/inspector/InspectorStyleTextEditor.cpp

    r110854 r127366  
    105105        }
    106106        if (!isHTMLLineBreak(m_styleText[propertyStart]))
    107             textToSet += formatLineFeed;
     107            textToSet.append(formatLineFeed);
    108108    } else {
    109109        String fullPrefix = formatLineFeed + formatPropertyPrefix;
    110110        long fullPrefixLength = fullPrefix.length();
    111         textToSet += fullPrefix;
     111        textToSet.append(fullPrefix);
    112112        if (insertFirstInSource && (propertyStart < fullPrefixLength || m_styleText.substring(propertyStart - fullPrefixLength, fullPrefixLength) != fullPrefix))
    113113            textToSet.insert(fullPrefix, formattingPrependOffset);
  • trunk/Source/WebCore/inspector/NetworkResourcesData.cpp

    r126926 r127366  
    114114    size_t dataLength = m_dataBuffer->size();
    115115    m_content = m_decoder->decode(m_dataBuffer->data(), m_dataBuffer->size());
    116     m_content += m_decoder->flush();
     116    m_content.append(m_decoder->flush());
    117117    m_dataBuffer = nullptr;
    118118    return contentSizeInBytes(m_content) - dataLength;
  • trunk/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp

    r127123 r127366  
    9191    // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
    9292    String sheetText = m_decoder->decode(m_data->data(), m_data->size());
    93     sheetText += m_decoder->flush();
     93    sheetText.append(m_decoder->flush());
    9494    return sheetText;
    9595}
     
    105105    if (m_data) {
    106106        m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
    107         m_decodedSheetText += m_decoder->flush();
     107        m_decodedSheetText.append(m_decoder->flush());
    108108    }
    109109    setLoading(false);
  • trunk/Source/WebCore/loader/cache/CachedFont.cpp

    r124884 r127366  
    138138        RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml");
    139139        String svgSource = decoder->decode(m_data->data(), m_data->size());
    140         svgSource += decoder->flush();
     140        svgSource.append(decoder->flush());
    141141       
    142142        m_externalSVGDocument->setContent(svgSource);
  • trunk/Source/WebCore/loader/cache/CachedScript.cpp

    r126154 r127366  
    7272    if (!m_script && m_data) {
    7373        m_script = m_decoder->decode(m_data->data(), encodedSize());
    74         m_script += m_decoder->flush();
     74        m_script.append(m_decoder->flush());
    7575        setDecodedSize(m_script.sizeInBytes());
    7676    }
  • trunk/Source/WebCore/loader/cache/CachedXSLStyleSheet.cpp

    r126154 r127366  
    7070        return;
    7171
    72     m_data = data;     
     72    m_data = data;
    7373    setEncodedSize(m_data.get() ? m_data->size() : 0);
    7474    if (m_data.get()) {
    75         m_sheet = String(m_decoder->decode(m_data->data(), encodedSize()));
    76         m_sheet += m_decoder->flush();
     75        m_sheet = m_decoder->decode(m_data->data(), encodedSize());
     76        m_sheet.append(m_decoder->flush());
    7777    }
    7878    setLoading(false);
  • trunk/Source/WebCore/page/FrameTree.cpp

    r120005 r127366  
    3131#include <wtf/Vector.h>
    3232#include <wtf/text/CString.h>
     33#include <wtf/text/StringBuilder.h>
    3334
    3435using std::swap;
     
    151152        chain.append(frame);
    152153    }
    153     String name;
    154     name += framePathPrefix;
    155     if (frame)
    156         name += frame->tree()->uniqueName().string().substring(framePathPrefixLength,
    157             frame->tree()->uniqueName().length() - framePathPrefixLength - framePathSuffixLength);
     154    StringBuilder name;
     155    name.append(framePathPrefix);
     156    if (frame) {
     157        name.append(frame->tree()->uniqueName().string().substring(framePathPrefixLength,
     158            frame->tree()->uniqueName().length() - framePathPrefixLength - framePathSuffixLength));
     159    }
    158160    for (int i = chain.size() - 1; i >= 0; --i) {
    159161        frame = chain[i];
    160         name += "/";
    161         name += frame->tree()->uniqueName();
    162     }
    163 
    164     // Suffix buffer has more than enough space for:
    165     //     10 characters before the number
    166     //     a number (20 digits for the largest 64-bit integer)
    167     //     6 characters after the number
    168     //     trailing null byte
    169     // But we still use snprintf just to be extra-safe.
    170     char suffix[40];
    171     snprintf(suffix, sizeof(suffix), "/<!--frame%u-->-->", childCount());
    172 
    173     name += suffix;
    174 
    175     return AtomicString(name);
     162        name.append('/');
     163        name.append(frame->tree()->uniqueName());
     164    }
     165
     166    name.appendLiteral("/<!--frame");
     167    name.append(String::number(childCount()));
     168    name.appendLiteral("-->-->");
     169
     170    return name.toAtomicString();
    176171}
    177172
  • trunk/Source/WebCore/page/Page.cpp

    r126968 r127366  
    838838    RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/css");
    839839    m_userStyleSheet = decoder->decode(data->data(), data->size());
    840     m_userStyleSheet += decoder->flush();
     840    m_userStyleSheet.append(decoder->flush());
    841841
    842842    return m_userStyleSheet;
  • trunk/Source/WebCore/platform/chromium/support/WebHTTPLoadInfo.cpp

    r126926 r127366  
    107107    // It is important that values are separated by '\n', not comma, otherwise Set-Cookie header is not parseable.
    108108    if (!result.isNewEntry)
    109         result.iterator->second += "\n" + String(value);
     109        result.iterator->second.append("\n" + String(value));
    110110}
    111111
  • trunk/Source/WebCore/platform/chromium/support/WebURLResponse.cpp

    r126926 r127366  
    262262        const_cast<HTTPHeaderMap*>(&map)->add(name, valueStr);
    263263    if (!result.isNewEntry)
    264         result.iterator->second += ", " + valueStr;
     264        result.iterator->second.append(", " + valueStr);
    265265}
    266266
  • trunk/Source/WebCore/xml/XMLHttpRequest.cpp

    r126926 r127366  
    645645        String contentType = getRequestHeader("Content-Type");
    646646        if (contentType.isEmpty()) {
    647             contentType = "multipart/form-data; boundary=";
    648             contentType += m_requestEntityBody->boundary().data();
     647            contentType = makeString("multipart/form-data; boundary=", m_requestEntityBody->boundary().data());
    649648            setRequestHeaderInternal("Content-Type", contentType);
    650649        }
     
    921920    HTTPHeaderMap::AddResult result = m_requestHeaders.add(name, value);
    922921    if (!result.isNewEntry)
    923         result.iterator->second += ", " + value;
     922        result.iterator->second.append(", " + value);
    924923}
    925924
  • trunk/Source/WebCore/xml/XPathFunctions.cpp

    r126926 r127366  
    551551    String s2 = arg(1)->evaluate().toString();
    552552    String s3 = arg(2)->evaluate().toString();
    553     String newString;
    554 
    555     // FIXME: Building a String a character at a time is quite slow.
     553    StringBuilder result;
     554
    556555    for (unsigned i1 = 0; i1 < s1.length(); ++i1) {
    557556        UChar ch = s1[i1];
     
    559558       
    560559        if (i2 == notFound)
    561             newString += String(&ch, 1);
    562         else if (i2 < s3.length()) {
    563             UChar c2 = s3[i2];
    564             newString += String(&c2, 1);
    565         }
    566     }
    567 
    568     return newString;
     560            result.append(ch);
     561        else if (i2 < s3.length())
     562            result.append(s3[i2]);
     563    }
     564
     565    return result.toString();
    569566}
    570567
  • trunk/Source/WebCore/xml/parser/XMLDocumentParser.cpp

    r121560 r127366  
    113113}
    114114
    115 void XMLDocumentParser::append(const SegmentedString& s)
    116 {
    117     String parseString = s.toString();
    118 
     115void XMLDocumentParser::append(const SegmentedString& source)
     116{
    119117    if (m_sawXSLTransform || !m_sawFirstElement)
    120         m_originalSourceForTransform += parseString;
     118        m_originalSourceForTransform.append(source);
    121119
    122120    if (isStopped() || m_sawXSLTransform)
     
    124122
    125123    if (m_parserPaused) {
    126         m_pendingSrc.append(s);
    127         return;
    128     }
    129 
    130     doWrite(parseString);
     124        m_pendingSrc.append(source);
     125        return;
     126    }
     127
     128    doWrite(source.toString());
    131129
    132130    // After parsing, go ahead and dispatch image beforeload events.
  • trunk/Source/WebCore/xml/parser/XMLDocumentParser.h

    r121564 r127366  
    178178        FrameView* m_view;
    179179
    180         String m_originalSourceForTransform;
     180        SegmentedString m_originalSourceForTransform;
    181181
    182182#if USE(QXMLSTREAM)
  • trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp

    r123788 r127366  
    13331333
    13341334    if (m_sawXSLTransform) {
    1335         void* doc = xmlDocPtrForString(document()->cachedResourceLoader(), m_originalSourceForTransform, document()->url().string());
     1335        void* doc = xmlDocPtrForString(document()->cachedResourceLoader(), m_originalSourceForTransform.toString(), document()->url().string());
    13361336        document()->setTransformSource(adoptPtr(new TransformSource(doc)));
    13371337
  • trunk/Source/WebCore/xml/parser/XMLDocumentParserQt.cpp

    r123839 r127366  
    202202#if ENABLE(XSLT)
    203203    if (m_sawXSLTransform) {
    204         document()->setTransformSource(adoptPtr(new TransformSource(m_originalSourceForTransform)));
     204        document()->setTransformSource(adoptPtr(new TransformSource(m_originalSourceForTransform.toString())));
    205205        document()->setParsing(false); // Make the doc think it's done, so it will apply xsl sheets.
    206206        document()->styleResolverChanged(RecalcStyleImmediately);
Note: See TracChangeset for help on using the changeset viewer.