Skip to content

Commit 9223ccb

Browse files
Avoid std::string -> (char *) roundtrip in createStringError() (NFC) (#93242)
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.
1 parent dc78329 commit 9223ccb

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,10 @@ class StringError : public ErrorInfo<StringError> {
12361236
public:
12371237
static char ID;
12381238

1239-
// Prints EC + S and converts to EC
1239+
StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly);
1240+
/// Prints EC + S and converts to EC.
12401241
StringError(std::error_code EC, const Twine &S = Twine());
1241-
1242-
// Prints S and converts to EC
1242+
/// Prints S and converts to EC.
12431243
StringError(const Twine &S, std::error_code EC);
12441244

12451245
void log(raw_ostream &OS) const override;
@@ -1258,15 +1258,18 @@ template <typename... Ts>
12581258
inline Error createStringError(std::error_code EC, char const *Fmt,
12591259
const Ts &... Vals) {
12601260
std::string Buffer;
1261-
raw_string_ostream Stream(Buffer);
1262-
Stream << format(Fmt, Vals...);
1263-
return make_error<StringError>(Stream.str(), EC);
1261+
raw_string_ostream(Buffer) << format(Fmt, Vals...);
1262+
return make_error<StringError>(Buffer, EC);
12641263
}
12651264

1266-
Error createStringError(std::error_code EC, char const *Msg);
1265+
Error createStringError(std::string &&Msg, std::error_code EC);
1266+
1267+
inline Error createStringError(std::error_code EC, const char *S) {
1268+
return createStringError(std::string(S), EC);
1269+
}
12671270

12681271
inline Error createStringError(std::error_code EC, const Twine &S) {
1269-
return createStringError(EC, S.str().c_str());
1272+
return createStringError(S.str(), EC);
12701273
}
12711274

12721275
/// Create a StringError with an inconvertible error code.

llvm/lib/Support/Error.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ StringError::StringError(std::error_code EC, const Twine &S)
135135
StringError::StringError(const Twine &S, std::error_code EC)
136136
: Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
137137

138+
StringError::StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
139+
: Msg(S), EC(EC), PrintMsgOnly(PrintMsgOnly) {}
140+
138141
void StringError::log(raw_ostream &OS) const {
139142
if (PrintMsgOnly) {
140143
OS << Msg;
@@ -149,7 +152,7 @@ std::error_code StringError::convertToErrorCode() const {
149152
return EC;
150153
}
151154

152-
Error createStringError(std::error_code EC, char const *Msg) {
155+
Error createStringError(std::string &&Msg, std::error_code EC) {
153156
return make_error<StringError>(Msg, EC);
154157
}
155158

0 commit comments

Comments
 (0)