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 Index: libcxx/benchmarks/system_error.bench.cpp =================================================================== --- /dev/null +++ libcxx/benchmarks/system_error.bench.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#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/include/__system_error/system_error.h =================================================================== --- libcxx/include/__system_error/system_error.h +++ libcxx/include/__system_error/system_error.h @@ -36,9 +36,6 @@ ~system_error() _NOEXCEPT override; _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; } - -private: - static string __init(const error_code&, string); }; _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_system_error(int __ev, const char* __what_arg); Index: libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -1062,7 +1062,6 @@ {'is_defined': True, 'name': '__ZNSt3__112strstreambufD0Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112strstreambufD1Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112strstreambufD2Ev', 'type': 'FUNC'} -{'is_defined': True, 'name': '__ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeE', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} Index: libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -393,7 +393,6 @@ {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD0Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD2Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} -{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} Index: libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -393,7 +393,6 @@ {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD0Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112strstreambufD2Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'} -{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'storage_mapping_class': 'DS', 'type': 'FUNC'} {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'storage_mapping_class': 'DS', 'type': 'FUNC'} Index: libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -1062,7 +1062,6 @@ {'is_defined': True, 'name': '__ZNSt3__112strstreambufD0Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112strstreambufD1Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112strstreambufD2Ev', 'type': 'FUNC'} -{'is_defined': True, 'name': '__ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeE', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'type': 'FUNC'} {'is_defined': True, 'name': '__ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} Index: libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -757,7 +757,6 @@ {'is_defined': True, 'name': '_ZNSt3__112strstreambufD0Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD1Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD2Ev', 'type': 'FUNC'} -{'is_defined': True, 'name': '_ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} Index: libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist +++ libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist @@ -755,7 +755,6 @@ {'is_defined': True, 'name': '_ZNSt3__112strstreambufD0Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD1Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD2Ev', 'type': 'FUNC'} -{'is_defined': True, 'name': '_ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} Index: libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.noexceptions.nonew.abilist =================================================================== --- libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.noexceptions.nonew.abilist +++ libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.noexceptions.nonew.abilist @@ -727,7 +727,6 @@ {'is_defined': True, 'name': '_ZNSt3__112strstreambufD0Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD1Ev', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112strstreambufD2Ev', 'type': 'FUNC'} -{'is_defined': True, 'name': '_ZNSt3__112system_error6__initERKNS_10error_codeENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeE', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeEPKc', 'type': 'FUNC'} {'is_defined': True, 'name': '_ZNSt3__112system_errorC1ENS_10error_codeERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'type': 'FUNC'} Index: libcxx/src/system_error.cpp =================================================================== --- libcxx/src/system_error.cpp +++ libcxx/src/system_error.cpp @@ -58,8 +58,8 @@ return *this == code.category() && code.value() == condition; } -#if !defined(_LIBCPP_HAS_NO_THREADS) namespace { +#if !defined(_LIBCPP_HAS_NO_THREADS) // GLIBC also uses 1024 as the maximum buffer size internally. constexpr size_t strerror_buff_size = 1024; @@ -127,8 +127,26 @@ return string(error_message); } #endif + +#endif // !defined(_LIBCPP_HAS_NO_THREADS) + +string make_error_str(const error_code& ec, string what_arg) { + if (ec) { + if (!what_arg.empty()) { + what_arg += ": "; + } + what_arg += ec.message(); + } + return what_arg; +} + +string make_error_str(const error_code& ec) { + if (ec) { + return ec.message(); + } + return string(); +} } // end namespace -#endif string __do_message::message(int ev) const @@ -231,50 +249,38 @@ // system_error -string -system_error::__init(const error_code& ec, string what_arg) -{ - if (ec) - { - if (!what_arg.empty()) - what_arg += ": "; - what_arg += ec.message(); - } - return what_arg; -} - system_error::system_error(error_code ec, const string& what_arg) - : runtime_error(__init(ec, what_arg)), + : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) { } system_error::system_error(error_code ec, const char* what_arg) - : runtime_error(__init(ec, what_arg)), + : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) { } system_error::system_error(error_code ec) - : runtime_error(__init(ec, "")), + : runtime_error(make_error_str(ec)), __ec_(ec) { } system_error::system_error(int ev, const error_category& ecat, const string& what_arg) - : runtime_error(__init(error_code(ev, ecat), what_arg)), + : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) { } system_error::system_error(int ev, const error_category& ecat, const char* what_arg) - : runtime_error(__init(error_code(ev, ecat), what_arg)), + : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) { } system_error::system_error(int ev, const error_category& ecat) - : runtime_error(__init(error_code(ev, ecat), "")), + : runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) { }