[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 10:24:49 PDT 2023


diamante0018 updated this revision to Diff 542576.
diamante0018 edited the summary of this revision.
diamante0018 added a comment.

After the conversation about this patch matured in the libcxx channel on the LLVM Discord server, the patch was analyzed quickly with "Compiler Explorer" and other tools and it was discovered that it does indeed reduce the amount of code generated when using the latest stable clang version (16) which in turn produces faster code.
In this updated diff, a new benchmark test file was added.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155820/new/

https://reviews.llvm.org/D155820

Files:
  libcxx/benchmarks/CMakeLists.txt
  libcxx/benchmarks/system_error.bench.cpp
  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);
Index: libcxx/benchmarks/system_error.bench.cpp
===================================================================
--- /dev/null
+++ libcxx/benchmarks/system_error.bench.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <string>
+#include <system_error>
+
+#include "Utilities.h"
+#include "benchmark/benchmark.h"
+
+static void BM_SystemErrorWithMessage(benchmark::State& state) {
+  for (auto _ : state) {
+    std::error_code ec{};
+    benchmark::DoNotOptimize(std::system_error(ec, ""));
+  }
+}
+BENCHMARK(BM_SystemErrorWithMessage);
+
+static void BM_SystemErrorWithoutMessage(benchmark::State& state) {
+  for (auto _ : state) {
+    std::error_code ec{};
+    benchmark::DoNotOptimize(std::system_error(ec));
+  }
+}
+BENCHMARK(BM_SystemErrorWithoutMessage);
+
+BENCHMARK_MAIN();
Index: libcxx/benchmarks/CMakeLists.txt
===================================================================
--- libcxx/benchmarks/CMakeLists.txt
+++ libcxx/benchmarks/CMakeLists.txt
@@ -192,6 +192,7 @@
     std_format_spec_string_unicode.bench.cpp
     string.bench.cpp
     stringstream.bench.cpp
+    system_error.bench.cpp
     to_chars.bench.cpp
     unordered_set_operations.bench.cpp
     util_smartptr.bench.cpp


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


More information about the libcxx-commits mailing list