about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-04-12 04:38:22 +0200
committerGitHub <noreply@github.com>2024-04-12 04:38:22 +0200
commit3758e2ffa5f14d1cb50ff6d1436489bb3749f627 (patch)
treec3c466cf5db9cde81d9d8cc631f86428f41f4cd4
parent41a294dd2be3279682b59b189beb60d18d70bfa7 (diff)
parent1170d73007c1547dc07dadf247bb74bff4c35294 (diff)
downloadrust-3758e2ffa5f14d1cb50ff6d1436489bb3749f627.tar.gz
rust-3758e2ffa5f14d1cb50ff6d1436489bb3749f627.zip
Rollup merge of #123826 - kornelski:one-in-a-quintillion, r=Amanieu
Move rare overflow error to a cold function

`scoped.spawn()` generates unnecessary inlined panic-formatting code for a branch that will never be taken.
-rw-r--r--library/std/src/thread/scoped.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/library/std/src/thread/scoped.rs b/library/std/src/thread/scoped.rs
index 7b11e7c17b1..e2e22e5194f 100644
--- a/library/std/src/thread/scoped.rs
+++ b/library/std/src/thread/scoped.rs
@@ -47,10 +47,16 @@ impl ScopeData {
         // chance it overflows to 0, which would result in unsoundness.
         if self.num_running_threads.fetch_add(1, Ordering::Relaxed) > usize::MAX / 2 {
             // This can only reasonably happen by mem::forget()'ing a lot of ScopedJoinHandles.
-            self.decrement_num_running_threads(false);
-            panic!("too many running threads in thread scope");
+            self.overflow();
         }
     }
+
+    #[cold]
+    fn overflow(&self) {
+        self.decrement_num_running_threads(false);
+        panic!("too many running threads in thread scope");
+    }
+
     pub(super) fn decrement_num_running_threads(&self, panic: bool) {
         if panic {
             self.a_thread_panicked.store(true, Ordering::Relaxed);