[webkit-changes] [WebKit/WebKit] 7fd3de: Add support for easy serialization of container/ra...
Sam Weinig
noreply at github.com
Wed Jun 5 14:51:37 PDT 2024
Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: 7fd3deadf8b1a134082e4420ac140a3227d68d34
https://github.com/WebKit/WebKit/commit/7fd3deadf8b1a134082e4420ac140a3227d68d34
Author: Sam Weinig <weinig at apple.com>
Date: 2024-06-05 (Wed, 05 Jun 2024)
Changed paths:
M Source/WTF/wtf/text/StringBuilder.h
M Source/WTF/wtf/text/StringConcatenate.h
M Source/WTF/wtf/text/StringView.h
M Source/WebCore/css/CSSGradientValue.cpp
M Source/WebCore/css/CSSSelector.cpp
M Source/WebCore/css/CSSSelectorList.cpp
M Source/WebCore/css/CSSSelectorList.h
M Source/WebCore/css/CSSValueList.cpp
M Source/WebCore/css/calc/CSSCalcOperationNode.cpp
M Source/WebCore/css/query/MediaQueryParser.cpp
M Source/WebCore/css/typedom/transform/CSSTransformValue.cpp
M Source/WebCore/platform/text/TextFlags.cpp
M Source/WebCore/testing/Internals.cpp
M Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp
Log Message:
-----------
Add support for easy serialization of container/ranges via StringBuilder
https://bugs.webkit.org/show_bug.cgi?id=275062
Reviewed by Darin Adler.
Introduced a new `StringTypeAdapter`, `Interleave`, which takes
a container/range, a joining string, and an optional mapping function.
`Interleaves` can currently only be used with StringBuilder, but can
be extended to makeString() as well with a bit of header factoring.
`Interleave` replaces common hand written loops when trying to
serialize container/ranges. For example, the following code:
StringBuilder builder;
...
builder.append('(');
bool wroteFirstItem = false;
for (auto& item : container) {
if (wroteFirstItem)
builder.append(", "_s);
wroteFirstItem = true;
builder.append(item->value);
}
builder.append(')');
can be replaced with:
StringBuilder builder;
...
builder.append('(', interleave(container, [](auto& item) { return item->value; }, ", "_s), ')');
`Interleave` works via a new kind of `StringTypeAdapter` that doesn't
know the length/8-bit-ness of its value before hand, causing the
builder to go down a new "slow" path. This "slow" path is just as
efficient as the old hand written loop, but a bit more ergonomic.
In addition to introducing `Interleave`, it has been adopted in places
in WebCore where it can be deployed (that I found), but has not been
adopted in places where adopting it would have turned the builder usage
into a one-liner. Those will be become makeString() calls when added.
* Source/WTF/wtf/text/StringBuilder.h:
(WTF::StringBuilder::appendFromAdapters):
(WTF::StringBuilder::appendFromAdapterSlow):
(WTF::StringBuilder::appendFromAdaptersSlow):
- Detect use of a "slow" `StringTypeAdapter` and use the
alternative path, iteratively appending each adapter.
The current strategy checks if any of the remaining
`StringTypeAdapters` are "slow" and goes down the
alternative path, peeling off the first adapter and
appending it, and then recursing back to the beginning
with the remainder of the adapters. This means that
if an append() contains:
append('(', 10, interleave(range, ' '), hex(10), ')'); [ aka "fast", "fast", "slow", "fast", "fast"]
We will do the equivalent of:
append('(');
append(10);
append(interleave(range, ' '));
append(10, ')');
This can be improved to find fast prefixes as well in
the future.
* Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp:
(TestWebKitAPI::TEST(StringBuilderTest, Interleave)):
- Add tests for the three forms of interleave.
* Source/WTF/wtf/text/StringConcatenate.h:
(WTF::Interleave::writeUsing const):
(WTF::interleave):
- Adds Interleave struct and introducing function.
(WTF::are8Bit):
- Simplify to a single function using a fold.
(WTF::hasKnownLength):
(WTF::haveKnownLength):
- Add helper to determine if the StringTypeAdapter has a known
length to determine fast vs slow cases.
* Source/WebCore/css/CSSGradientValue.cpp:
(WebCore::CSSLinearGradientValue::customCSSText const):
(WebCore::CSSRadialGradientValue::customCSSText const):
(WebCore::CSSConicGradientValue::customCSSText const):
* Source/WebCore/css/CSSSelector.cpp:
(WebCore::appendLangArgument):
(WebCore::appendLangArgumentList):
* Source/WebCore/css/CSSSelectorList.cpp:
(WebCore::CSSSelectorList::buildSelectorsText const):
* Source/WebCore/css/CSSSelectorList.h:
(WebCore::CSSSelectorList::const_iterator::operator!= const): Deleted.
* Source/WebCore/css/CSSValueList.cpp:
(WebCore::CSSValueContainingVector::serializeItems const):
* Source/WebCore/css/calc/CSSCalcOperationNode.cpp:
(WebCore::CSSCalcOperationNode::buildCSSTextRecursive):
* Source/WebCore/css/query/MediaQueryParser.cpp:
(WebCore::MQ::serialize):
* Source/WebCore/css/typedom/transform/CSSTransformValue.cpp:
(WebCore::CSSTransformValue::serialize const):
* Source/WebCore/platform/text/TextFlags.cpp:
* Source/WebCore/testing/Internals.cpp:
(WebCore::serializeOffset):
(WebCore::serializeOffsets):
(WebCore::Internals::scrollSnapOffsets):
(WebCore::appendOffsets): Deleted.
- Adopt interleave() for container serialization.
Canonical link: https://commits.webkit.org/279755@main
To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications
More information about the webkit-changes
mailing list