Title: [295007] trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp
Revision
295007
Author
[email protected]
Date
2022-05-28 19:03:33 -0700 (Sat, 28 May 2022)

Log Message

Simplify / Optimize StringBuilder::appendQuotedJSONString()
https://bugs.webkit.org/show_bug.cgi?id=240955

Reviewed by Darin Adler.

This simplifies the code, makes it more consistent with the test of StringBuilder,
and appears to be a ~0.3% progression on Speedometer on Apple Silicon.

* Source/WTF/wtf/text/StringBuilderJSON.cpp:
(WTF::StringBuilder::appendQuotedJSONString):

Canonical link: https://commits.webkit.org/251102@main

Modified Paths

Diff

Modified: trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp (295006 => 295007)


--- trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp	2022-05-29 00:11:43 UTC (rev 295006)
+++ trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp	2022-05-29 02:03:33 UTC (rev 295007)
@@ -111,53 +111,37 @@
     // The 2 is for the '"' quotes on each end.
     // The 6 is the worst case for a single code unit that could be encoded as \uNNNN.
     CheckedUint32 stringLength = string.length();
-    auto maximumCapacityRequired = m_length + 2 + stringLength * 6;
-    if (maximumCapacityRequired.hasOverflowed()) {
+    stringLength *= 6;
+    stringLength += 2;
+    if (stringLength.hasOverflowed()) {
         didOverflow();
         return;
     }
 
-    // We need to check maximum length before calling roundUpPowerOfTwo because that function returns 0 for values in the range [2^31, 2^32-2].
-    // FIXME: Instead, roundUpToPowerOfTwo should be fixed to do something more useful in those cases, perhaps using checked or saturated arithmetic.
-    // https://bugs.webkit.org/show_bug.cgi?id=176086
-    auto allocationSize = maximumCapacityRequired.value();
-    if (allocationSize > String::MaxLength) {
-        didOverflow();
-        return;
-    }
-    allocationSize = roundUpToPowerOfTwo(allocationSize);
-    if (allocationSize > String::MaxLength) {
-        didOverflow();
-        return;
-    }
+    auto stringLengthValue = stringLength.value();
 
-    // FIXME: Consider switching to extendBufferForAppending/shrink instead to share more code with the rest of StringBuilder.
-    if (is8Bit() && !string.is8Bit())
-        allocateBuffer<UChar>(characters<LChar>(), allocationSize);
-    else
-        reserveCapacity(allocationSize);
-    if (UNLIKELY(hasOverflowed()))
-        return;
-
-    if (m_buffer->is8Bit()) {
-        auto characters = const_cast<LChar*>(m_buffer->characters<LChar>());
-        auto output = characters + m_length;
-        *output++ = '"';
-        appendQuotedJSONStringInternal(output, string.characters8(), string.length());
-        *output++ = '"';
-        m_length = output - characters;
+    if (is8Bit() && string.is8Bit()) {
+        if (auto* output = extendBufferForAppending<LChar>(saturatedSum<uint32_t>(m_length, stringLengthValue))) {
+            auto* end = output + stringLengthValue;
+            *output++ = '"';
+            appendQuotedJSONStringInternal(output, string.characters8(), string.length());
+            *output++ = '"';
+            if (output < end)
+                shrink(m_length - (end - output));
+        }
     } else {
-        auto characters = const_cast<UChar*>(m_buffer->characters<UChar>());
-        auto output = characters + m_length;
-        *output++ = '"';
-        if (string.is8Bit())
-            appendQuotedJSONStringInternal(output, string.characters8(), string.length());
-        else
-            appendQuotedJSONStringInternal(output, string.characters16(), string.length());
-        *output++ = '"';
-        m_length = output - characters;
+        if (auto* output = extendBufferForAppendingWithUpconvert(saturatedSum<uint32_t>(m_length, stringLengthValue))) {
+            auto* end = output + stringLengthValue;
+            *output++ = '"';
+            if (string.is8Bit())
+                appendQuotedJSONStringInternal(output, string.characters8(), string.length());
+            else
+                appendQuotedJSONStringInternal(output, string.characters16(), string.length());
+            *output++ = '"';
+            if (output < end)
+                shrink(m_length - (end - output));
+        }
     }
-    ASSERT(m_buffer->length() >= m_length);
 }
 
 } // namespace WTF
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to