From 35b7527db61374a04756c8239f970bf8fc453f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Na=C5=9Bciszewski?= Date: Sat, 7 Oct 2017 15:44:26 +0200 Subject: [PATCH 1/2] Improved headless Servo performance Now the main thread doesn't waste 100% CPU --- ports/glutin/window.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/glutin/window.rs b/ports/glutin/window.rs index ff486efbd43..ad806f3620d 100644 --- a/ports/glutin/window.rs +++ b/ports/glutin/window.rs @@ -39,6 +39,8 @@ use std::mem; use std::os::raw::c_void; use std::ptr; use std::rc::Rc; +use std::thread; +use std::time; use style_traits::DevicePixel; use style_traits::cursor::Cursor; #[cfg(target_os = "windows")] @@ -695,6 +697,11 @@ impl Window { } events.extend(mem::replace(&mut *self.event_queue.borrow_mut(), Vec::new()).into_iter()); + + if opts::get().headless && events.is_empty() { + thread::sleep(time::Duration::from_millis(5)); + } + events } From 63d69dbe9fff332f22602a57e5740cabc06420ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Na=C5=9Bciszewski?= Date: Mon, 9 Oct 2017 20:36:08 +0200 Subject: [PATCH 2/2] Move sleeping code to `match` block and comment --- ports/glutin/window.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ports/glutin/window.rs b/ports/glutin/window.rs index ad806f3620d..cfbed72075e 100644 --- a/ports/glutin/window.rs +++ b/ports/glutin/window.rs @@ -686,7 +686,13 @@ impl Window { close_event = self.handle_window_event(event) || close_event; } } - WindowKind::Headless(..) => {} + WindowKind::Headless(..) => { + // Sleep the main thread to avoid using 100% CPU + // This can be done better, see comments in #18777 + if events.is_empty() { + thread::sleep(time::Duration::from_millis(5)); + } + } } } else { close_event = self.handle_next_event(); @@ -697,11 +703,6 @@ impl Window { } events.extend(mem::replace(&mut *self.event_queue.borrow_mut(), Vec::new()).into_iter()); - - if opts::get().headless && events.is_empty() { - thread::sleep(time::Duration::from_millis(5)); - } - events }