[libcxx-commits] [libcxx] [libc++] Speed up classic locale (PR #72112)

Dmitry Vyukov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 26 21:01:12 PST 2023


================
@@ -14,17 +15,73 @@ double istream_numbers() {
   double f1 = 0.0, f2 = 0.0, q = 0.0;
   for (int i = 0; i < 3; i++) {
     std::istringstream s(a[i]);
+    if (loc)
+      s.imbue(*loc);
     s >> a1 >> a2 >> a3 >> f1 >> a4 >> a5 >> f2 >> a6 >> a7;
     q += (a1 + a2 + a3 + a4 + a5 + a6 + a7 + f1 + f2) / 1000000;
   }
   return q;
 }
 
+struct LocaleSelector {
+  std::locale* imbue;
+  std::locale old;
+  static std::mutex mutex;
----------------
dvyukov wrote:

Both options work.
It does not need to be global, but a coarser-grained mutex is also fine in this case.
Benchmark threads only race in the ctor/dtor separately. No benchmark thread will run dtor, until all other threads finish initialization and reach `state.KeepRunning()`.
`KeepRunning` contains 2 implicit barriers on entry and exit:
https://github.com/google/benchmark/blob/1e96bb0ab5e758861f5bbbd4edbd0a8d9a2a7cae/include/benchmark/benchmark.h#L1018-L1031

https://github.com/llvm/llvm-project/pull/72112


More information about the libcxx-commits mailing list