diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2010-08-23 18:16:08 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2010-08-23 18:16:08 +0000 |
commit | 08fd2cf26a2363bd3192f1280976724869359ef1 (patch) | |
tree | 7665fc9f9cc5528d0eafd5fd7b46905bede5ab07 /llvm/lib/Support/StringRef.cpp | |
parent | 630add39a6f50f8475cc5497edbfaea9b4340f48 (diff) | |
download | bcm5719-llvm-08fd2cf26a2363bd3192f1280976724869359ef1.tar.gz bcm5719-llvm-08fd2cf26a2363bd3192f1280976724869359ef1.zip |
Avoid O(n*m) complexity in StringRef::find_first(_not)_of(StringRef).
- Cache used characters in a bitset to reduce memory overhead to just 32 bytes.
- On my core2 this code is faster except when the checked string was very short
(smaller than the list of delimiters).
llvm-svn: 111817
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index ca0f518a88b..40b732c62db 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/APInt.h" +#include <bitset> using namespace llvm; @@ -153,11 +154,15 @@ size_t StringRef::rfind(StringRef Str) const { /// find_first_of - Find the first character in the string that is in \arg /// Chars, or npos if not found. /// -/// Note: O(size() * Chars.size()) +/// Note: O(size() + Chars.size()) StringRef::size_type StringRef::find_first_of(StringRef Chars, size_t From) const { + std::bitset<1 << CHAR_BIT> CharBits; + for (size_type i = 0; i != Chars.size(); ++i) + CharBits.set((unsigned char)Chars[i]); + for (size_type i = min(From, Length), e = Length; i != e; ++i) - if (Chars.find(Data[i]) != npos) + if (CharBits.test((unsigned char)Data[i])) return i; return npos; } @@ -174,11 +179,15 @@ StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const { /// find_first_not_of - Find the first character in the string that is not /// in the string \arg Chars, or npos if not found. /// -/// Note: O(size() * Chars.size()) +/// Note: O(size() + Chars.size()) StringRef::size_type StringRef::find_first_not_of(StringRef Chars, size_t From) const { + std::bitset<1 << CHAR_BIT> CharBits; + for (size_type i = 0; i != Chars.size(); ++i) + CharBits.set((unsigned char)Chars[i]); + for (size_type i = min(From, Length), e = Length; i != e; ++i) - if (Chars.find(Data[i]) == npos) + if (!CharBits.test((unsigned char)Data[i])) return i; return npos; } |