From a698146943bcd6df2a61bbfd56badf3018662709 Mon Sep 17 00:00:00 2001 From: Ty Overby Date: Thu, 1 Jan 2015 23:09:16 -0800 Subject: Change the way that events are represented. The bulk of this commit is changing instances of Vec to RingBuf which is optimized for the push_back() / pop_front() strategy that is used internaly in the event system. The glutin custom iterators are now just wrappers around the RingBuf iterator type. This will bring the running time of iterator traversal from O(n^2) to O(n) because shifting-on-delete won't be performed. --- src/x11/window/mod.rs | 25 +++++++++++++------------ src/x11/window/monitor.rs | 11 ++++++----- 2 files changed, 19 insertions(+), 17 deletions(-) (limited to 'src/x11/window') diff --git a/src/x11/window/mod.rs b/src/x11/window/mod.rs index fc622ea..768d480 100644 --- a/src/x11/window/mod.rs +++ b/src/x11/window/mod.rs @@ -5,6 +5,7 @@ use libc; use std::{mem, ptr}; use std::cell::Cell; use std::sync::atomic::AtomicBool; +use std::collections::RingBuf; use super::ffi; use std::sync::{Arc, Once, ONCE_INIT}; @@ -424,10 +425,10 @@ impl Window { } } - pub fn poll_events(&self) -> Vec { + pub fn poll_events(&self) -> RingBuf { use std::mem; - let mut events = Vec::new(); + let mut events = RingBuf::new(); loop { use std::num::Int; @@ -456,9 +457,9 @@ impl Window { if client_msg.l[0] == self.wm_delete_window as libc::c_long { self.is_closed.store(true, Relaxed); - events.push(Closed); + events.push_back(Closed); } else { - events.push(Awakened); + events.push_back(Awakened); } }, @@ -468,14 +469,14 @@ impl Window { let (current_width, current_height) = self.current_size.get(); if current_width != cfg_event.width || current_height != cfg_event.height { self.current_size.set((cfg_event.width, cfg_event.height)); - events.push(Resized(cfg_event.width as uint, cfg_event.height as uint)); + events.push_back(Resized(cfg_event.width as uint, cfg_event.height as uint)); } }, ffi::MotionNotify => { use events::Event::MouseMoved; let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) }; - events.push(MouseMoved((event.x as int, event.y as int))); + events.push_back(MouseMoved((event.x as int, event.y as int))); }, ffi::KeyPress | ffi::KeyRelease => { @@ -504,7 +505,7 @@ impl Window { }; for chr in written.as_slice().chars() { - events.push(ReceivedCharacter(chr)); + events.push_back(ReceivedCharacter(chr)); } let keysym = unsafe { @@ -513,7 +514,7 @@ impl Window { let vkey = events::keycode_to_element(keysym as libc::c_uint); - events.push(KeyboardInput(state, event.keycode as u8, vkey)); + events.push_back(KeyboardInput(state, event.keycode as u8, vkey)); }, ffi::ButtonPress | ffi::ButtonRelease => { @@ -530,11 +531,11 @@ impl Window { ffi::Button2 => Some(MiddleMouseButton), ffi::Button3 => Some(RightMouseButton), ffi::Button4 => { - events.push(MouseWheel(1)); + events.push_back(MouseWheel(1)); None } ffi::Button5 => { - events.push(MouseWheel(-1)); + events.push_back(MouseWheel(-1)); None } _ => None @@ -542,7 +543,7 @@ impl Window { match button { Some(button) => - events.push(MouseInput(state, button)), + events.push_back(MouseInput(state, button)), None => () }; }, @@ -554,7 +555,7 @@ impl Window { events } - pub fn wait_events(&self) -> Vec { + pub fn wait_events(&self) -> RingBuf { use std::mem; loop { diff --git a/src/x11/window/monitor.rs b/src/x11/window/monitor.rs index f62a8ef..596dca7 100644 --- a/src/x11/window/monitor.rs +++ b/src/x11/window/monitor.rs @@ -1,10 +1,11 @@ -use std::{ptr}; +use std::ptr; +use std::collections::RingBuf; use super::super::ffi; use super::ensure_thread_init; pub struct MonitorID(pub uint); -pub fn get_available_monitors() -> Vec { +pub fn get_available_monitors() -> RingBuf { ensure_thread_init(); let nb_monitors = unsafe { let display = ffi::XOpenDisplay(ptr::null()); @@ -16,9 +17,9 @@ pub fn get_available_monitors() -> Vec { nb_monitors }; - let mut vec = Vec::new(); - vec.grow_fn(nb_monitors as uint, |i| MonitorID(i)); - vec + let mut monitors = RingBuf::new(); + monitors.extend(range(0, nb_monitors).map(|i| MonitorID(i as uint))); + monitors } pub fn get_primary_monitor() -> MonitorID { -- cgit v1.2.3