Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use find8 and find16 in AdaptiveStringSearcher #28403

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 17 additions & 38 deletions Source/WTF/wtf/text/AdaptiveStringSearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,30 @@ class AdaptiveStringSearcherBase {
static constexpr bool exceedsOneByte(LChar) { return false; }
static constexpr bool exceedsOneByte(UChar c) { return c > 0xff; }

template <typename T, typename U>
static inline T alignDown(T value, U alignment)
{
return reinterpret_cast<T>((reinterpret_cast<uintptr_t>(value) & ~(alignment - 1)));
}

static constexpr uint8_t getHighestValueByte(LChar character) { return character; }

static constexpr uint8_t getHighestValueByte(UChar character)
{
return std::max<unsigned>(static_cast<uint8_t>(character & 0xFF), static_cast<uint8_t>(character >> 8));
}

template <typename PatternChar, typename SubjectChar>
static inline int findFirstCharacter(std::span<const PatternChar> pattern, std::span<const SubjectChar> subject, int index)
{
const auto* subjectPtr = subject.data();
const PatternChar patternFirstChar = pattern[0];
const int maxN = (subject.size() - pattern.size() + 1);
PatternChar patternFirstChar = pattern[0];

if (sizeof(SubjectChar) == 2 && !patternFirstChar) {
// Special-case looking for the 0 char in other than one-byte strings.
// memchr mostly fails in this case due to every other byte being 0 in text
// that is mostly ascii characters.
for (int i = index; i < maxN; ++i) {
if (!subjectPtr[i])
return i;
}
Comment on lines -89 to -95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this no longer apply? Or is it just not worth the upfront runtime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer applies since we use find16 instead of memchr.

return -1;
}
const uint8_t searchByte = getHighestValueByte(patternFirstChar);
const SubjectChar searchChar = static_cast<SubjectChar>(patternFirstChar);
int pos = index;
do {
ASSERT(maxN - pos >= 0);
const SubjectChar* charPos = reinterpret_cast<const SubjectChar*>(memchr(subjectPtr + pos, searchByte, (maxN - pos) * sizeof(SubjectChar)));
if (charPos == nullptr)
if constexpr (sizeof(PatternChar) == 2 && sizeof(SubjectChar) == 1) {
if (!isLatin1(patternFirstChar))
Comment on lines +74 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why could we handle this case before and not now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the previous one was using memchr and a hack for both 16 / 8 bit cases. Now the logic is completely different.

return -1;
charPos = alignDown(charPos, sizeof(SubjectChar));
pos = static_cast<int>(charPos - subjectPtr);
if (subjectPtr[pos] == searchChar)
return pos;
} while (++pos < maxN);
}

return -1;
const int maxN = (subject.size() - pattern.size() + 1);
const SubjectChar searchCharacter = static_cast<SubjectChar>(patternFirstChar);
const auto* start = subjectPtr + index;
const auto searchLength = maxN - index;
const SubjectChar* charPos = nullptr;
ASSERT(maxN - index >= 0);
if constexpr (sizeof(SubjectChar) == 2)
charPos = bitwise_cast<const SubjectChar*>(find16(bitwise_cast<const uint16_t*>(start), searchCharacter, searchLength));
else
charPos = bitwise_cast<const SubjectChar*>(find8(bitwise_cast<const uint8_t*>(start), searchCharacter, searchLength));
if (charPos == nullptr)
return -1;
return static_cast<int>(charPos - subjectPtr);
}
};

Expand Down