⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 272752 in webkit


Ignore:
Timestamp:
Feb 11, 2021, 2:24:00 PM (6 years ago)
Author:
commit-queue@webkit.org
Message:

Reduce string copies when converting from NSString/CFStringRef to WTF::String
​https://bugs.webkit.org/show_bug.cgi?id=221766

Patch by Alex Christensen <​achristensen@webkit.org> on 2021-02-11
Reviewed by Geoff Garen.

This reduces the string copies from two to one which should speed up many things.
The cost is that for non-Latin1-encodable strings of length less than 1024, we now do an allocation
and a reallocation, whereas before we were doing just one allocation. I think even in this case, though,
the cost of a reallocation should be comparable to the cost of doing a double string copy,
and the benefit of reducing a string copy everywhere is compelling.

I also reduced duplicate code by combining the CF and NS implementations.

  • wtf/text/WTFString.h:
  • wtf/text/cf/StringCF.cpp:

(WTF::String::String):

  • wtf/text/cocoa/StringCocoa.mm:

(WTF::String::String): Deleted.

Location:
trunk/Source/WTF
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r272736 r272752  
     12021-02-11  Alex Christensen  <achristensen@webkit.org>
     2
     3        Reduce string copies when converting from NSString/CFStringRef to WTF::String
     4        https://bugs.webkit.org/show_bug.cgi?id=221766
     5
     6        Reviewed by Geoff Garen.
     7
     8        This reduces the string copies from two to one which should speed up many things.
     9        The cost is that for non-Latin1-encodable strings of length less than 1024, we now do an allocation
     10        and a reallocation, whereas before we were doing just one allocation.  I think even in this case, though,
     11        the cost of a reallocation should be comparable to the cost of doing a double string copy,
     12        and the benefit of reducing a string copy everywhere is compelling.
     13
     14        I also reduced duplicate code by combining the CF and NS implementations.
     15
     16        * wtf/text/WTFString.h:
     17        * wtf/text/cf/StringCF.cpp:
     18        (WTF::String::String):
     19        * wtf/text/cocoa/StringCocoa.mm:
     20        (WTF::String::String): Deleted.
     21
    1222021-02-11  Sam Weinig  <weinig@apple.com>
    223
  • trunk/Source/WTF/wtf/text/WTFString.h

    r271633 r272752  
    320320
    321321#ifdef __OBJC__
    322     WTF_EXPORT_PRIVATE String(NSString *);
     322    String(NSString *string)
     323        : String((__bridge CFStringRef)string) { }
    323324
    324325    // This conversion converts the null string to an empty NSString rather than to nil.
  • trunk/Source/WTF/wtf/text/cf/StringCF.cpp

    r165676 r272752  
    2626#include <CoreFoundation/CoreFoundation.h>
    2727#include <wtf/RetainPtr.h>
     28#include <wtf/text/StringBuffer.h>
    2829
    2930namespace WTF {
    … …  
    3536
    3637    CFIndex size = CFStringGetLength(str);
    37     if (size == 0)
     38    if (!size) {
    3839        m_impl = StringImpl::empty();
    39     else {
    40         Vector<LChar, 1024> lcharBuffer(size);
     40        return;
     41    }
     42
     43    {
     44        StringBuffer<LChar> buffer(size);
    4145        CFIndex usedBufLen;
    42         CFIndex convertedsize = CFStringGetBytes(str, CFRangeMake(0, size), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), size, &usedBufLen);
    43         if ((convertedsize == size) && (usedBufLen == size)) {
    44             m_impl = StringImpl::create(lcharBuffer.data(), size);
     46        CFIndex convertedSize = CFStringGetBytes(str, CFRangeMake(0, size), kCFStringEncodingISOLatin1, 0, false, buffer.characters(), size, &usedBufLen);
     47        if (convertedSize == size && usedBufLen == size) {
     48            m_impl = StringImpl::adopt(WTFMove(buffer));
    4549            return;
    4650        }
     51    }
    4752
    48         Vector<UChar, 1024> buffer(size);
    49         CFStringGetCharacters(str, CFRangeMake(0, size), (UniChar*)buffer.data());
    50         m_impl = StringImpl::create(buffer.data(), size);
    51     }
     53    StringBuffer<UChar> ucharBuffer(size);
     54    CFStringGetCharacters(str, CFRangeMake(0, size), reinterpret_cast<UniChar *>(ucharBuffer.characters()));
     55    m_impl = StringImpl::adopt(WTFMove(ucharBuffer));
    5256}
    5357
  • trunk/Source/WTF/wtf/text/cocoa/StringCocoa.mm

    r262814 r272752  
    2626namespace WTF {
    2727
    28 String::String(NSString *str)
    29 {
    30     if (!str)
    31         return;
    32 
    33     CFIndex size = CFStringGetLength((__bridge CFStringRef)str);
    34     if (!size)
    35         m_impl = StringImpl::empty();
    36     else {
    37         Vector<LChar, 1024> lcharBuffer(size);
    38         CFIndex usedBufLen;
    39         CFIndex convertedsize = CFStringGetBytes((__bridge CFStringRef)str, CFRangeMake(0, size), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), size, &usedBufLen);
    40         if ((convertedsize == size) && (usedBufLen == size)) {
    41             m_impl = StringImpl::create(lcharBuffer.data(), size);
    42             return;
    43         }
    44 
    45         Vector<UChar, 1024> ucharBuffer(size);
    46         CFStringGetCharacters((__bridge CFStringRef)str, CFRangeMake(0, size), reinterpret_cast<UniChar*>(ucharBuffer.data()));
    47         m_impl = StringImpl::create(ucharBuffer.data(), size);
    48     }
    49 }
    50 
    5128RetainPtr<id> makeNSArrayElement(const String& vectorElement)
    5229{
Note: See TracChangeset for help on using the changeset viewer.