[libcxx-commits] [libcxx] [libc++] Optimize std::swap of locales (PR #209760)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 3 02:29:19 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/209760

>From aea60e2d44b924dd4eccf69a9d985c217973bfc1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 15 Jul 2026 14:55:14 +0200
Subject: [PATCH] [libc++] Optimize std::swap of locales

---
 libcxx/include/__locale                       |  6 ++++
 libcxx/modules/std/locale.inc                 |  1 +
 .../localization}/ctype.bench.cpp             |  0
 .../text/localization/locale.bench.cpp        | 28 +++++++++++++++
 .../localization}/num_get.bench.cpp           |  0
 .../localization}/num_put.bench.cpp           |  0
 .../locale/locale.nonmembers/swap.pass.cpp    | 36 +++++++++++++++++++
 7 files changed, 71 insertions(+)
 rename libcxx/test/benchmarks/{locale => text/localization}/ctype.bench.cpp (100%)
 create mode 100644 libcxx/test/benchmarks/text/localization/locale.bench.cpp
 rename libcxx/test/benchmarks/{locale => text/localization}/num_get.bench.cpp (100%)
 rename libcxx/test/benchmarks/{locale => text/localization}/num_put.bench.cpp (100%)
 create mode 100644 libcxx/test/std/localization/locales/locale/locale.nonmembers/swap.pass.cpp

diff --git a/libcxx/include/__locale b/libcxx/include/__locale
index 15031c36ec435..07f255e65d9e9 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&);
+
+  _LIBCPP_HIDE_FROM_ABI friend void swap(locale&, locale&) _NOEXCEPT;
 };
 
+_LIBCPP_HIDE_FROM_ABI inline void swap(locale& __lhs, locale& __rhs) _NOEXCEPT {
+  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/modules/std/locale.inc b/libcxx/modules/std/locale.inc
index cdc7441ece52e..7bd915fa01781 100644
--- a/libcxx/modules/std/locale.inc
+++ b/libcxx/modules/std/locale.inc
@@ -13,6 +13,7 @@ export namespace std {
   using std::has_facet;
   using std::locale;
   using std::use_facet;
+  using std::swap;
 
   // [locale.convenience], convenience interfaces
   using std::isalnum;
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
diff --git a/libcxx/test/std/localization/locales/locale/locale.nonmembers/swap.pass.cpp b/libcxx/test/std/localization/locales/locale/locale.nonmembers/swap.pass.cpp
new file mode 100644
index 0000000000000..609d07f1c3281
--- /dev/null
+++ b/libcxx/test/std/localization/locales/locale/locale.nonmembers/swap.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <locale>
+
+// void swap(locale &, locale &) noexcept
+
+#include <cassert>
+#include <locale>
+
+#include "platform_support.h"
+
+int main(int, char**) {
+  std::locale loc1 = std::locale::classic();
+  std::locale loc2(LOCALE_en_US_UTF_8);
+
+  assert(loc1 != loc2);
+
+  std::locale expected_lhs = loc2;
+  std::locale expected_rhs = loc1;
+
+  static_assert(noexcept(swap(loc1, loc2)), "");
+
+  swap(loc1, loc2);
+  assert(loc1 == expected_lhs);
+  assert(loc2 == expected_rhs);
+
+  return 0;
+}



More information about the libcxx-commits mailing list