[libcxx-commits] [PATCH] D155820: [libc++] Optimize internal function in <system_error>

Edo via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 20 05:23:54 PDT 2023


diamante0018 created this revision.
diamante0018 added a reviewer: ldionne.
Herald added a project: All.
diamante0018 requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

In the event the internal function __init is called with an empty string the code will take unnecessary extra steps, in addition, the code generated might be overall greater because, to my understanding, when initializing a string with an empty `const char*` "" (like in this case), the compiler might be unable to deduce the string is indeed empty at compile time and more code is generated.

The goal of this patch is to make a new internal function that will accept just an error code skipping the empty string argument. It should skip the unnecessary steps and in the event `if (ec)` is `false`, it will return an empty string using the correct ctor, avoiding any extra code generation issues.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155820

Files:
  libcxx/include/__system_error/system_error.h
  libcxx/src/system_error.cpp


Index: libcxx/src/system_error.cpp
===================================================================
--- libcxx/src/system_error.cpp
+++ libcxx/src/system_error.cpp
@@ -243,6 +243,16 @@
     return what_arg;
 }
 
+string
+system_error::__init(const error_code& ec)
+{
+    if (ec)
+    {
+        return ec.message();
+    }
+    return string();
+}
+
 system_error::system_error(error_code ec, const string& what_arg)
     : runtime_error(__init(ec, what_arg)),
       __ec_(ec)
@@ -256,7 +266,7 @@
 }
 
 system_error::system_error(error_code ec)
-    : runtime_error(__init(ec, "")),
+    : runtime_error(__init(ec)),
       __ec_(ec)
 {
 }
@@ -274,7 +284,7 @@
 }
 
 system_error::system_error(int ev, const error_category& ecat)
-    : runtime_error(__init(error_code(ev, ecat), "")),
+    : runtime_error(__init(error_code(ev, ecat))),
       __ec_(error_code(ev, ecat))
 {
 }
Index: libcxx/include/__system_error/system_error.h
===================================================================
--- libcxx/include/__system_error/system_error.h
+++ libcxx/include/__system_error/system_error.h
@@ -39,6 +39,7 @@
 
 private:
   static string __init(const error_code&, string);
+  static string __init(const error_code&);
 };
 
 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_system_error(int __ev, const char* __what_arg);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155820.542440.patch
Type: text/x-patch
Size: 1338 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230720/d7692319/attachment.bin>


More information about the libcxx-commits mailing list