diff options
author | Andreas Kling <kling@serenityos.org> | 2021-02-23 14:13:57 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-23 17:41:18 +0100 |
commit | 31ac93d051be5be78419ed16da9fcae9783eb4e6 (patch) | |
tree | 8e422b644ce0844f3696cdcd8247efc38183bae4 /AK | |
parent | 87bb00f6abff2963b76e8c748ee784542e04c351 (diff) | |
download | serenity-31ac93d051be5be78419ed16da9fcae9783eb4e6.zip |
AK: Optimize StringView::operator==(const char*) a little bit
Don't compute the strlen() of the string we're comparing against first.
This can save a lot of time if we're comparing against something that
already fails to match in the first few characters.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/StringView.h | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/AK/StringView.h b/AK/StringView.h index 7ff37437c3..6f6f9058de 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -144,11 +144,15 @@ public: return !cstring; if (!cstring) return false; - size_t other_length = __builtin_strlen(cstring); - if (m_length != other_length) - return false; - return !__builtin_memcmp(m_characters, cstring, m_length); + // NOTE: `m_characters` is not guaranteed to be null-terminated, but `cstring` is. + const char* cp = cstring; + for (size_t i = 0; i < m_length; ++i) { + if (m_characters[i] != *(cp++)) + return false; + } + return !*cp; } + bool operator!=(const char* cstring) const { return !(*this == cstring); |