Skip to content

Avoid std::string -> (char *) roundtrip in createStringError() (NFC) #93242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024

Conversation

adrian-prantl
Copy link
Collaborator

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.

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.
@llvmbot
Copy link
Member

llvmbot commented May 23, 2024

@llvm/pr-subscribers-llvm-support

Author: Adrian Prantl (adrian-prantl)

Changes

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 --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);
 }
 

Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I wonder if there is a way to measure the impact of this change? 😄

@adrian-prantl adrian-prantl merged commit 9223ccb into llvm:main May 23, 2024
7 of 9 checks passed
adrian-prantl added a commit to adrian-prantl/llvm-project that referenced this pull request May 24, 2024
…lvm#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.

(cherry picked from commit 9223ccb)
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Jun 26, 2024
…9509eeb79

Local branch amd-gfx 3189509 Merged main:7c5c8b2f479fbed6afcd4072bdef76ea867577de into amd-gfx:e7c692dd6c2c
Remote branch main 9223ccb Avoid std::string -> (char *) roundtrip in createStringError() (NFC) (llvm#93242)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants