[llvm] [llvm] Improve implementation of StringRef::find_last_of and cie (PR #71865)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 9 12:47:00 PST 2023
https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/71865
Using a simple LUT implies less operations than a bitset, at the expense of slightly more stack usage.
Mandatory performance report:
https://llvm-compile-time-tracker.com/compare.php?from=3f9d385e5844f2f1f144305037cfc904789c6187&to=86f685e9552acaf8d5db630161e91752348422a1&stat=instructions:u
>From 852a30c4fd4e4251d9234518e850d14d5abe586a Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Thu, 9 Nov 2023 20:41:40 +0100
Subject: [PATCH] [llvm] Improve implementation of StringRef::find_last_of and
cie
Using a simple LUT implies less operations than a bitset, at the expense
of slightly more stack usage.
Mandatory performance report:
https://llvm-compile-time-tracker.com/compare.php?from=3f9d385e5844f2f1f144305037cfc904789c6187&to=86f685e9552acaf8d5db630161e91752348422a1&stat=instructions:u
---
llvm/lib/Support/StringRef.cpp | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index feee47ca693b251..be6e340ff0d5901 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -13,7 +13,6 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/edit_distance.h"
#include "llvm/Support/Error.h"
-#include <bitset>
using namespace llvm;
@@ -236,12 +235,12 @@ size_t StringRef::rfind_insensitive(StringRef Str) const {
/// Note: O(size() + Chars.size())
StringRef::size_type StringRef::find_first_of(StringRef Chars,
size_t From) const {
- std::bitset<1 << CHAR_BIT> CharBits;
+ bool CharBits[1 << CHAR_BIT] = {false};
for (char C : Chars)
- CharBits.set((unsigned char)C);
+ CharBits[(unsigned)C] = true;
for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
- if (CharBits.test((unsigned char)Data[i]))
+ if (CharBits[(unsigned char)Data[i]])
return i;
return npos;
}
@@ -258,12 +257,12 @@ StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
/// 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;
+ bool CharBits[1 << CHAR_BIT] = {false};
for (char C : Chars)
- CharBits.set((unsigned char)C);
+ CharBits[(unsigned)C] = true;
for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
- if (!CharBits.test((unsigned char)Data[i]))
+ if (!CharBits[(unsigned char)Data[i]])
return i;
return npos;
}
@@ -274,12 +273,12 @@ StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
/// Note: O(size() + Chars.size())
StringRef::size_type StringRef::find_last_of(StringRef Chars,
size_t From) const {
- std::bitset<1 << CHAR_BIT> CharBits;
+ bool CharBits[1 << CHAR_BIT] = {false};
for (char C : Chars)
- CharBits.set((unsigned char)C);
+ CharBits[(unsigned)C] = true;
for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
- if (CharBits.test((unsigned char)Data[i]))
+ if (CharBits[(unsigned char)Data[i]])
return i;
return npos;
}
@@ -299,12 +298,12 @@ StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const {
/// Note: O(size() + Chars.size())
StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
size_t From) const {
- std::bitset<1 << CHAR_BIT> CharBits;
+ bool CharBits[1 << CHAR_BIT] = {false};
for (char C : Chars)
- CharBits.set((unsigned char)C);
+ CharBits[(unsigned)C] = true;
for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
- if (!CharBits.test((unsigned char)Data[i]))
+ if (!CharBits[(unsigned char)Data[i]])
return i;
return npos;
}
More information about the llvm-commits
mailing list