[llvm] Avoid std::string -> (char *) roundtrip in createStringError() (NFC) (PR #93242)
via llvm-commits
llvm-commits at lists.llvm.org
Thu May 23 14:23:32 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Adrian Prantl (adrian-prantl)
<details>
<summary>Changes</summary>
The current API first creates a temporary std::string, then passes it as a C string, only to then convert it into a std::string for storage, thus unnecessarily computing the length of the string and copying it.
---
Full diff: https://github.com/llvm/llvm-project/pull/93242.diff
2 Files Affected:
- (modified) llvm/include/llvm/Support/Error.h (+11-8)
- (modified) llvm/lib/Support/Error.cpp (+4-1)
``````````diff
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index d085ffe6d3267..662c3ea46e3c1 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1236,10 +1236,10 @@ class StringError : public ErrorInfo<StringError> {
public:
static char ID;
- // Prints EC + S and converts to EC
+ StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly);
+ /// Prints EC + S and converts to EC.
StringError(std::error_code EC, const Twine &S = Twine());
-
- // Prints S and converts to EC
+ /// Prints S and converts to EC.
StringError(const Twine &S, std::error_code EC);
void log(raw_ostream &OS) const override;
@@ -1258,15 +1258,18 @@ template <typename... Ts>
inline Error createStringError(std::error_code EC, char const *Fmt,
const Ts &... Vals) {
std::string Buffer;
- raw_string_ostream Stream(Buffer);
- Stream << format(Fmt, Vals...);
- return make_error<StringError>(Stream.str(), EC);
+ raw_string_ostream(Buffer) << format(Fmt, Vals...);
+ return make_error<StringError>(Buffer, EC);
}
-Error createStringError(std::error_code EC, char const *Msg);
+Error createStringError(std::string &&Msg, std::error_code EC);
+
+inline Error createStringError(std::error_code EC, const char *S) {
+ return createStringError(std::string(S), EC);
+}
inline Error createStringError(std::error_code EC, const Twine &S) {
- return createStringError(EC, S.str().c_str());
+ return createStringError(S.str(), EC);
}
/// Create a StringError with an inconvertible error code.
diff --git a/llvm/lib/Support/Error.cpp b/llvm/lib/Support/Error.cpp
index 21d591530b41a..34ec31e3b833f 100644
--- a/llvm/lib/Support/Error.cpp
+++ b/llvm/lib/Support/Error.cpp
@@ -135,6 +135,9 @@ StringError::StringError(std::error_code EC, const Twine &S)
StringError::StringError(const Twine &S, std::error_code EC)
: Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
+StringError::StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
+ : Msg(S), EC(EC), PrintMsgOnly(PrintMsgOnly) {}
+
void StringError::log(raw_ostream &OS) const {
if (PrintMsgOnly) {
OS << Msg;
@@ -149,7 +152,7 @@ std::error_code StringError::convertToErrorCode() const {
return EC;
}
-Error createStringError(std::error_code EC, char const *Msg) {
+Error createStringError(std::string &&Msg, std::error_code EC) {
return make_error<StringError>(Msg, EC);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/93242
More information about the llvm-commits
mailing list