[libcxx-commits] [libcxx] [libc++] Optimize std::swap of locales (PR #209760)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 15 06:54:28 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
Currently we copy construct `locale`s whenever we swap, which is quite expensive for locales. We can just swap the pointers instead.
---
Full diff: https://github.com/llvm/llvm-project/pull/209760.diff
5 Files Affected:
- (modified) libcxx/include/__locale (+6)
- (renamed) libcxx/test/benchmarks/text/localization/ctype.bench.cpp ()
- (added) libcxx/test/benchmarks/text/localization/locale.bench.cpp (+28)
- (renamed) libcxx/test/benchmarks/text/localization/num_get.bench.cpp ()
- (renamed) libcxx/test/benchmarks/text/localization/num_put.bench.cpp ()
``````````diff
diff --git a/libcxx/include/__locale b/libcxx/include/__locale
index bb8f8e7bbf5d9..8f29d713909cc 100644
--- a/libcxx/include/__locale
+++ b/libcxx/include/__locale
@@ -143,8 +143,14 @@ private:
friend bool has_facet(const locale&) _NOEXCEPT;
template <class _Facet>
friend const _Facet& use_facet(const locale&);
+
+ friend void swap(locale&, locale&);
};
+inline void swap(locale& __lhs, locale& __rhs) {
+ std::swap(__lhs.__locale_, __rhs.__locale_);
+}
+
class _LIBCPP_EXPORTED_FROM_ABI locale::facet : public __shared_count {
protected:
_LIBCPP_HIDE_FROM_ABI explicit facet(size_t __refs = 0) : __shared_count(static_cast<long>(__refs) - 1) {}
diff --git a/libcxx/test/benchmarks/locale/ctype.bench.cpp b/libcxx/test/benchmarks/text/localization/ctype.bench.cpp
similarity index 100%
rename from libcxx/test/benchmarks/locale/ctype.bench.cpp
rename to libcxx/test/benchmarks/text/localization/ctype.bench.cpp
diff --git a/libcxx/test/benchmarks/text/localization/locale.bench.cpp b/libcxx/test/benchmarks/text/localization/locale.bench.cpp
new file mode 100644
index 0000000000000..60bc541bc053f
--- /dev/null
+++ b/libcxx/test/benchmarks/text/localization/locale.bench.cpp
@@ -0,0 +1,28 @@
+
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+#include <ios>
+#include <locale>
+
+#include <benchmark/benchmark.h>
+
+static void BM_num_get(benchmark::State& state) {
+ std::locale loc1, loc2;
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(loc1);
+ benchmark::DoNotOptimize(loc2);
+ std::swap(loc1, loc2);
+ }
+}
+BENCHMARK(BM_num_get)->Name("std::swap(std::locale&, std::locale&)");
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/benchmarks/locale/num_get.bench.cpp b/libcxx/test/benchmarks/text/localization/num_get.bench.cpp
similarity index 100%
rename from libcxx/test/benchmarks/locale/num_get.bench.cpp
rename to libcxx/test/benchmarks/text/localization/num_get.bench.cpp
diff --git a/libcxx/test/benchmarks/locale/num_put.bench.cpp b/libcxx/test/benchmarks/text/localization/num_put.bench.cpp
similarity index 100%
rename from libcxx/test/benchmarks/locale/num_put.bench.cpp
rename to libcxx/test/benchmarks/text/localization/num_put.bench.cpp
``````````
</details>
https://github.com/llvm/llvm-project/pull/209760
More information about the libcxx-commits
mailing list