Skip to content

Commit

Permalink
Use std::span in ASCIIFastPath
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271509
rdar://125271340

Reviewed by Chris Dumez.

* Source/JavaScriptCore/API/JSScript.mm:
(+[JSScript scriptOfType:memoryMappedFromASCIIFile:withSourceURL:andBytecodeCache:inVirtualMachine:error:]):
Use span.

* Source/JavaScriptCore/runtime/StringPrototype.cpp:
(JSC::normalize): Use StringView::containsOnlyASCII.

* Source/JavaScriptCore/wasm/WasmParser.h:
(JSC::Wasm::Parser<SuccessType>::consumeUTF8String): Use span.

* Source/WTF/wtf/text/ASCIIFastPath.h:
(WTF::charactersAreAllASCII): Take span.
(WTF::charactersAreAllLatin1): Ditto.

* Source/WTF/wtf/text/AdaptiveStringSearcher.h:
(WTF::AdaptiveStringSearcher::AdaptiveStringSearcher): Use span.
* Source/WTF/wtf/text/StringImpl.h:
(WTF::StringImpl::createStaticStringImpl): Use span.
(WTF::StringImpl::containsOnlyASCII const): Ditto.
* Source/WTF/wtf/text/StringView.h:
(WTF::StringView::containsOnlyASCII const): Ditto.
* Source/WTF/wtf/text/WTFString.cpp:
(WTF::fromUTF8Impl): Ditto.
* Source/WebCore/bindings/js/ScriptBufferSourceProvider.h: Ditto.
* Source/WebCore/loader/cache/CachedScript.cpp:
(WebCore::CachedScript::script): Ditto.

* Source/WebCore/testing/MockCDMFactory.cpp:
(WebCore::MockCDM::sanitizeResponse const): Use span and StringView::split.

Canonical link: https://commits.webkit.org/276587@main
  • Loading branch information
darinadler committed Mar 23, 2024
1 parent 594c0f1 commit 4caac85
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/API/JSScript.mm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ + (instancetype)scriptOfType:(JSScriptType)type memoryMappedFromASCIIFile:(NSURL
if (!success)
return createError([NSString stringWithFormat:@"File at path %@ could not be mapped.", static_cast<NSString *>(systemPath)], error);

if (!charactersAreAllASCII(reinterpret_cast<const LChar*>(fileData.data()), fileData.size()))
if (!charactersAreAllASCII(fileData.span()))
return createError([NSString stringWithFormat:@"Not all characters in file at %@ are ASCII.", static_cast<NSString *>(systemPath)], error);

auto result = adoptNS([[JSScript alloc] init]);
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/runtime/StringPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ static JSValue normalize(JSGlobalObject* globalObject, JSString* string, Normali
// Latin-1 characters (U+0000..U+00FF) are left unaffected by NFC.
// ASCII characters (U+0000..U+007F) are left unaffected by all of the Normalization Forms
// https://unicode.org/reports/tr15/#Description_Norm
if (view.is8Bit() && (form == NormalizationForm::NFC || charactersAreAllASCII(view.characters8(), view.length())))
if (view.is8Bit() && (form == NormalizationForm::NFC || view.containsOnlyASCII()))
RELEASE_AND_RETURN(scope, string);

const UNormalizer2* normalizer = JSC::normalizer(form);
Expand Down
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/wasm/WasmParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ ALWAYS_INLINE bool Parser<SuccessType>::consumeUTF8String(Name& result, size_t s
if (!result.tryReserveCapacity(stringLength))
return false;

const uint8_t* stringStart = source() + m_offset;
auto* stringStart = source() + m_offset;

// We don't cache the UTF-16 characters since it seems likely the string is ASCII.
if (UNLIKELY(!charactersAreAllASCII(stringStart, stringLength))) {
if (UNLIKELY(!charactersAreAllASCII(std::span { stringStart, stringLength }))) {
Vector<UChar, 1024> buffer(stringLength);
UChar* bufferStart = buffer.data();

Expand Down
10 changes: 6 additions & 4 deletions Source/WTF/wtf/text/ASCIIFastPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ inline bool containsOnlyASCII(MachineWord word)
// Note: This function assume the input is likely all ASCII, and
// does not leave early if it is not the case.
template<typename CharacterType>
inline bool charactersAreAllASCII(const CharacterType* characters, size_t length)
inline bool charactersAreAllASCII(std::span<const CharacterType> span)
{
MachineWord allCharBits = 0;
const CharacterType* end = characters + length;
auto* characters = span.data();
auto* end = characters + span.size();

// Prologue: align the input.
while (!isAlignedToMachineWord(characters) && characters != end) {
Expand Down Expand Up @@ -116,13 +117,14 @@ inline bool charactersAreAllASCII(const CharacterType* characters, size_t length
// Note: This function assume the input is likely all Latin1, and
// does not leave early if it is not the case.
template<typename CharacterType>
inline bool charactersAreAllLatin1(const CharacterType* characters, size_t length)
inline bool charactersAreAllLatin1(std::span<const CharacterType> span)
{
if constexpr (sizeof(CharacterType) == 1)
return true;
else {
MachineWord allCharBits = 0;
const CharacterType* end = characters + length;
auto* characters = span.data();
auto* end = characters + span.size();

// Prologue: align the input.
while (!isAlignedToMachineWord(characters) && characters != end) {
Expand Down
2 changes: 1 addition & 1 deletion Source/WTF/wtf/text/AdaptiveStringSearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class AdaptiveStringSearcher : private AdaptiveStringSearcherBase {
, m_start(std::max<int>(0, pattern.size() - bmMaxShift))
{
if (sizeof(PatternChar) > sizeof(SubjectChar)) {
if (!charactersAreAllLatin1(m_pattern.data(), m_pattern.size())) {
if (!charactersAreAllLatin1(m_pattern)) {
m_strategy = &failSearch;
return;
}
Expand Down
6 changes: 3 additions & 3 deletions Source/WTF/wtf/text/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class StringImpl : private StringImplShape {

static Ref<StringImpl> createStaticStringImpl(const char* characters, unsigned length)
{
ASSERT(charactersAreAllASCII(bitwise_cast<const LChar*>(characters), length));
ASSERT(charactersAreAllASCII(std::span { bitwise_cast<const LChar*>(characters), static_cast<size_t>(length) }));
return createStaticStringImpl(bitwise_cast<const LChar*>(characters), length);
}
WTF_EXPORT_PRIVATE static Ref<StringImpl> createStaticStringImpl(const LChar*, unsigned length);
Expand Down Expand Up @@ -874,8 +874,8 @@ inline Ref<StringImpl> StringImpl::isolatedCopy() const
inline bool StringImpl::containsOnlyASCII() const
{
if (is8Bit())
return charactersAreAllASCII(characters8(), length());
return charactersAreAllASCII(characters16(), length());
return charactersAreAllASCII(span8());
return charactersAreAllASCII(span16());
}

inline bool StringImpl::containsOnlyLatin1() const
Expand Down
4 changes: 2 additions & 2 deletions Source/WTF/wtf/text/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ template<> ALWAYS_INLINE const UChar* StringView::characters<UChar>() const
inline bool StringView::containsOnlyASCII() const
{
if (is8Bit())
return charactersAreAllASCII(characters8(), length());
return charactersAreAllASCII(characters16(), length());
return charactersAreAllASCII(span8());
return charactersAreAllASCII(span16());
}

template<size_t N>
Expand Down
2 changes: 1 addition & 1 deletion Source/WTF/wtf/text/WTFString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ String fromUTF8Impl(const LChar* stringStart, size_t length)
if (!length)
return emptyString();

if (charactersAreAllASCII(stringStart, length))
if (charactersAreAllASCII(std::span { stringStart, length }))
return StringImpl::create(std::span { stringStart, length });

Vector<UChar, 1024> buffer(length);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/js/ScriptBufferSourceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ScriptBufferSourceProvider final : public JSC::SourceProvider, public Abst
if (!m_contiguousBuffer && (!m_containsOnlyASCII || *m_containsOnlyASCII))
m_contiguousBuffer = m_scriptBuffer.buffer()->makeContiguous();
if (!m_containsOnlyASCII) {
m_containsOnlyASCII = charactersAreAllASCII(m_contiguousBuffer->data(), m_contiguousBuffer->size());
m_containsOnlyASCII = charactersAreAllASCII(m_contiguousBuffer->span());
if (*m_containsOnlyASCII)
m_scriptHash = StringHasher::computeHashAndMaskTop8Bits(m_contiguousBuffer->span());
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/loader/cache/CachedScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ StringView CachedScript::script(ShouldDecodeAsUTF8Only shouldDecodeAsUTF8Only)
if (m_decodingState == NeverDecoded
&& PAL::TextEncoding(encoding()).isByteBasedEncoding()
&& contiguousData->size()
&& charactersAreAllASCII(contiguousData->data(), contiguousData->size())) {
&& charactersAreAllASCII(contiguousData->span())) {

m_decodingState = DataAndDecodedStringHaveSameBytes;

Expand Down
14 changes: 7 additions & 7 deletions Source/WebCore/testing/MockCDMFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ bool MockCDM::supportsInitData(const AtomString& initDataType, const SharedBuffe

RefPtr<SharedBuffer> MockCDM::sanitizeResponse(const SharedBuffer& response) const
{
auto buffer = response.makeContiguous();
if (!charactersAreAllASCII(buffer->data(), response.size()))
auto contiguousResponse = response.makeContiguous();
if (!charactersAreAllASCII(contiguousResponse->span()))
return nullptr;

Vector<String> responseArray = String(buffer->data(), response.size()).split(' ');

if (!responseArray.contains(String("valid-response"_s)))
return nullptr;
for (auto word : StringView(contiguousResponse->span()).split(' ')) {
if (word == "valid-response"_s)
return contiguousResponse;
}

return response.makeContiguous();
return nullptr;
}

std::optional<String> MockCDM::sanitizeSessionId(const String& sessionId) const
Expand Down

0 comments on commit 4caac85

Please sign in to comment.