mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-26 08:56:06 +00:00
[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
This commit is contained in:
parent
43dfe0f08e
commit
efcb07bf6e
@ -29,7 +29,7 @@ public:
|
||||
// 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 @@ public:
|
||||
// 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)); }
|
||||
};
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user