[Webkit-unassigned] [Bug 227244] New: Replace builder.append(String::fromUTF8(...)) uses with a more efficient new StringTypeAdapter

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Mon Jun 21 16:53:31 PDT 2021


https://bugs.webkit.org/show_bug.cgi?id=227244

            Bug ID: 227244
           Summary: Replace builder.append(String::fromUTF8(...)) uses
                    with a more efficient new StringTypeAdapter
           Product: WebKit
           Version: Other
          Hardware: Unspecified
                OS: Unspecified
            Status: NEW
          Severity: Normal
          Priority: P2
         Component: Web Template Framework
          Assignee: webkit-unassigned at lists.webkit.org
          Reporter: sam at webkit.org

A few places in WebCore use builder.append(String::fromUTF8(...)) to add UTF8 content to a StringBuilder. This makes an extra String allocation per call and can be improved by adding a StringTypeAdapter for this use case to directly convert into the StringBuilder's buffer. Something like:

struct FromUTF8Converter {
    Span<const std::byte> buffer;
};

inline FromUTF8Converter fromUTF8(Span<const std::byte> buffer)
{
    return { buffer };
}

template<> class StringTypeAdapter<FromUTF8Converter, void> {
public:
    StringTypeAdapter(const FromUTF8Converter& converter)
        : m_converter { converter }
    {
        auto [utf16StringLength, isAllASCII] = computeUTF16StringLengthAreAllASCII(m_converter.buffer.data(), m_converter.buffer.size()) }

        m_utf16StringLength = utf16StringLength;
        m_isAllASCII = isAllASCII;
    }

    unsigned length() const
    { 
        if (m_isAllASCII)
            return m_converter.buffer.size();
        return m_utf16StringLength;
    }

    bool is8Bit() const { return m_isAllASCII; }

    void writeTo(LChar* destination) const
    {
        ASSERT(m_isAllASCII);
        memcpy(destination, m_converter.buffer.data(), m_converter.buffer.data() + m_converter.buffer.size(), );
    }

    void writeTo(UChar* destination) const
    {
        convertUTF8ToUTF16(m_converter.buffer.data(), m_converter.buffer.size(), destination, destination + m_utf16StringLength);
    }

private:
    const FromUTF8Converter& m_converter;
    unsigned m_utf16StringLength;
    bool m_isAllASCII;
};


...


builder.append(fromUTF8(data));

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20210621/daa6002a/attachment.htm>


More information about the webkit-unassigned mailing list