[llvm] efcb07b - [llvm][Support] Avoid intermediate heap allocations in `StringSaver`
Jan Svoboda via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 3 11:12:12 PDT 2023
Author: Jan Svoboda
Date: 2023-08-03T11:12:05-07:00
New Revision: efcb07bf6e5fb0ff12e5855ab0ad86cd534d5578
URL: https://github.com/llvm/llvm-project/commit/efcb07bf6e5fb0ff12e5855ab0ad86cd534d5578
DIFF: https://github.com/llvm/llvm-project/commit/efcb07bf6e5fb0ff12e5855ab0ad86cd534d5578.diff
LOG: [llvm][Support] Avoid intermediate heap allocations in `StringSaver`
The `Twine::str()` function currently always allocates heap memory via `std::string`. However, some instances of `Twine` don't need an intermediate buffer at all, and the rest can attempt to print into a stack buffer first.
This is intentionally not making use of `Twine::isSingleStringLiteral()` from D157010 to skip saving the string in the bump-pointer allocator, since the `StringSaver` documentation suggests that MUST happen for every given string.
Reviewed By: benlangmuir
Differential Revision: https://reviews.llvm.org/D157015
Added:
Modified:
llvm/include/llvm/Support/StringSaver.h
llvm/lib/Support/StringSaver.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/StringSaver.h b/llvm/include/llvm/Support/StringSaver.h
index fa9db30eae30c9..95ace0e0e29a02 100644
--- a/llvm/include/llvm/Support/StringSaver.h
+++ b/llvm/include/llvm/Support/StringSaver.h
@@ -29,7 +29,7 @@ class StringSaver final {
// All returned strings are null-terminated: *save(S).end() == 0.
StringRef save(const char *S) { return save(StringRef(S)); }
StringRef save(StringRef S);
- StringRef save(const Twine &S) { return save(StringRef(S.str())); }
+ StringRef save(const Twine &S);
StringRef save(const std::string &S) { return save(StringRef(S)); }
};
@@ -51,7 +51,7 @@ class UniqueStringSaver final {
// All returned strings are null-terminated: *save(S).end() == 0.
StringRef save(const char *S) { return save(StringRef(S)); }
StringRef save(StringRef S);
- StringRef save(const Twine &S) { return save(StringRef(S.str())); }
+ StringRef save(const Twine &S);
StringRef save(const std::string &S) { return save(StringRef(S)); }
};
diff --git a/llvm/lib/Support/StringSaver.cpp b/llvm/lib/Support/StringSaver.cpp
index f7ccfb97ea7987..4a616d7348320b 100644
--- a/llvm/lib/Support/StringSaver.cpp
+++ b/llvm/lib/Support/StringSaver.cpp
@@ -8,6 +8,8 @@
#include "llvm/Support/StringSaver.h"
+#include "llvm/ADT/SmallString.h"
+
using namespace llvm;
StringRef StringSaver::save(StringRef S) {
@@ -18,9 +20,19 @@ StringRef StringSaver::save(StringRef S) {
return StringRef(P, S.size());
}
+StringRef StringSaver::save(const Twine &S) {
+ SmallString<128> Storage;
+ return save(S.toStringRef(Storage));
+}
+
StringRef UniqueStringSaver::save(StringRef S) {
auto R = Unique.insert(S);
if (R.second) // cache miss, need to actually save the string
*R.first = Strings.save(S); // safe replacement with equal value
return *R.first;
}
+
+StringRef UniqueStringSaver::save(const Twine &S) {
+ SmallString<128> Storage;
+ return save(S.toStringRef(Storage));
+}
More information about the llvm-commits
mailing list