gardesk/garbg / 744615f

Browse files

cap parallel frame scaling to available CPU cores

Authored by espadonne
SHA
744615f35a1210bb7cfa25fca0b875005d595737
Parents
609a151
Tree
4e3956f

1 changed file

StatusFile+-
M garbg/src/daemon/state.rs 17 9
garbg/src/daemon/state.rsmodified
@@ -103,17 +103,25 @@ impl ActiveAnimation {
103103
         screen_width: u32,
104104
         screen_height: u32,
105105
     ) -> Self {
106
-        // Scale frames in parallel using thread::scope for multi-core speedup
106
+        // Scale frames in parallel, limited to available CPU cores
107
+        let num_cpus = std::thread::available_parallelism()
108
+            .map(|n| n.get())
109
+            .unwrap_or(4);
107110
         let scaled_frames: Vec<image::RgbaImage> = std::thread::scope(|s| {
108
-            let handles: Vec<_> = frames
109
-                .iter()
110
-                .map(|frame| {
111
-                    s.spawn(move || {
112
-                        scale_image_fast(&frame.image, screen_width, screen_height, scale_mode)
111
+            let mut results = Vec::with_capacity(frames.len());
112
+            // Process in batches of num_cpus to avoid spawning too many threads
113
+            for chunk in frames.chunks(num_cpus) {
114
+                let handles: Vec<_> = chunk
115
+                    .iter()
116
+                    .map(|frame| {
117
+                        s.spawn(move || {
118
+                            scale_image_fast(&frame.image, screen_width, screen_height, scale_mode)
119
+                        })
113120
                     })
114
-                })
115
-                .collect();
116
-            handles.into_iter().map(|h| h.join().unwrap()).collect()
121
+                    .collect();
122
+                results.extend(handles.into_iter().map(|h| h.join().unwrap()));
123
+            }
124
+            results
117125
         });
118126
 
119127
         let frame_delays: Vec<Duration> = frames