utils: id_gen! optimization
Some checks failed
Continuous Integration / format (push) Has been cancelled
Continuous Integration / clippy-check (push) Has been cancelled
Continuous Integration / smithay-check-features (push) Has been cancelled
Continuous Integration / check-msrv (push) Has been cancelled
Continuous Integration / check-minimal (push) Has been cancelled
Continuous Integration / smithay-tests (push) Has been cancelled
Continuous Integration / smallvil-check (push) Has been cancelled
Continuous Integration / anvil-check-features (push) Has been cancelled
Continuous Integration / WLCS: Bad Buffer Test (push) Has been cancelled
Continuous Integration / WLCS: Core tests (push) Has been cancelled
Continuous Integration / WLCS: Output tests (push) Has been cancelled
Continuous Integration / WLCS: Pointer input tests (push) Has been cancelled
Continuous Integration / Documentation on Github Pages (push) Has been cancelled

This commit is contained in:
Paraworker 2024-08-17 16:10:36 +08:00 committed by Victoria Brekenfeld
parent ef9782b854
commit ab2b48c0c9
9 changed files with 50 additions and 54 deletions

View file

@ -43,7 +43,7 @@ pub mod surface;
pub mod texture;
pub mod utils;
crate::utils::ids::id_gen!(next_external_id, EXTERNAL_ID, EXTERNAL_IDS);
crate::utils::ids::id_gen!(external_id);
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
/// A unique id for a [`RenderElement`]
@ -61,13 +61,13 @@ struct ExternalId(usize);
impl ExternalId {
fn new() -> Self {
ExternalId(next_external_id())
ExternalId(external_id::next())
}
}
impl Drop for ExternalId {
fn drop(&mut self) {
EXTERNAL_IDS.lock().unwrap().remove(&self.0);
external_id::remove(self.0);
}
}

View file

@ -73,11 +73,11 @@ pub mod ffi {
include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
}
crate::utils::ids::id_gen!(next_renderer_id, RENDERER_ID, RENDERER_IDS);
crate::utils::ids::id_gen!(renderer_id);
struct RendererId(usize);
impl Drop for RendererId {
fn drop(&mut self) {
RENDERER_IDS.lock().unwrap().remove(&self.0);
renderer_id::remove(self.0);
}
}
@ -577,7 +577,7 @@ impl GlesRenderer {
context
.user_data()
.insert_if_missing_threadsafe(|| RendererId(next_renderer_id()));
.insert_if_missing_threadsafe(|| RendererId(renderer_id::next()));
drop(_guard);
let renderer = GlesRenderer {

View file

@ -32,7 +32,7 @@ pub use self::element::*;
use self::output::*;
pub use self::utils::*;
crate::utils::ids::id_gen!(next_space_id, SPACE_ID, SPACE_IDS);
crate::utils::ids::id_gen!(space_id);
#[derive(Debug)]
struct InnerElement<E> {
@ -66,14 +66,14 @@ impl<E: SpaceElement> PartialEq for Space<E> {
impl<E: SpaceElement> Drop for Space<E> {
#[inline]
fn drop(&mut self) {
SPACE_IDS.lock().unwrap().remove(&self.id);
space_id::remove(self.id);
}
}
impl<E: SpaceElement> Default for Space<E> {
#[inline]
fn default() -> Self {
let id = next_space_id();
let id = space_id::next();
let span = debug_span!("desktop_space", id);
Self {

View file

@ -26,7 +26,7 @@ use std::{
use crate::desktop::WindowSurfaceType;
crate::utils::ids::id_gen!(next_layer_id, LAYER_ID, LAYER_IDS);
crate::utils::ids::id_gen!(layer_id);
/// Map of [`LayerSurface`]s on an [`Output`]
#[derive(Debug)]
@ -501,7 +501,7 @@ pub(crate) struct LayerSurfaceInner {
impl Drop for LayerSurfaceInner {
#[inline]
fn drop(&mut self) {
LAYER_IDS.lock().unwrap().remove(&self.id);
layer_id::remove(self.id);
}
}
@ -516,7 +516,7 @@ impl LayerSurface {
/// Create a new [`LayerSurface`] from a given [`WlrLayerSurface`] and its namespace.
pub fn new(surface: WlrLayerSurface, namespace: String) -> LayerSurface {
LayerSurface(Arc::new(LayerSurfaceInner {
id: next_layer_id(),
id: layer_id::next(),
surface,
namespace,
userdata: UserDataMap::new(),

View file

@ -25,7 +25,7 @@ use wayland_protocols::{
};
use wayland_server::protocol::wl_surface;
crate::utils::ids::id_gen!(next_window_id, WINDOW_ID, WINDOW_IDS);
crate::utils::ids::id_gen!(window_id);
/// Represents the surface of a [`Window`]
#[derive(Debug)]
@ -48,7 +48,7 @@ pub(crate) struct WindowInner {
impl Drop for WindowInner {
fn drop(&mut self) {
WINDOW_IDS.lock().unwrap().remove(&self.id);
window_id::remove(self.id);
}
}
@ -112,7 +112,7 @@ impl Window {
/// Construct a new [`Window`] from a xdg toplevel surface
pub fn new_wayland_window(toplevel: ToplevelSurface) -> Window {
let id = next_window_id();
let id = window_id::next();
Window(Arc::new(WindowInner {
id,
@ -126,7 +126,7 @@ impl Window {
/// Construct a new [`Window`] from an X11 surface
#[cfg(feature = "xwayland")]
pub fn new_x11_window(surface: X11Surface) -> Window {
let id = next_window_id();
let id = window_id::next();
Window(Arc::new(WindowInner {
id,

View file

@ -1,36 +1,32 @@
macro_rules! id_gen {
($func_name:ident, $id_name:ident, $ids_name:ident) => {
static $id_name: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
lazy_static::lazy_static! {
static ref $ids_name: std::sync::Mutex<std::collections::HashSet<usize>> =
std::sync::Mutex::new(std::collections::HashSet::new());
}
($mod_name:ident) => {
mod $mod_name {
use once_cell::sync::Lazy;
use std::{collections::HashSet, sync::Mutex};
fn $func_name() -> usize {
let mut ids = $ids_name.lock().unwrap();
if ids.len() == usize::MAX {
panic!("Out of ids");
static ID_DATA: Lazy<Mutex<(HashSet<usize>, usize)>> =
Lazy::new(|| Mutex::new((HashSet::new(), 0)));
pub(crate) fn next() -> usize {
let (id_set, counter) = &mut *ID_DATA.lock().unwrap();
if id_set.len() == usize::MAX {
panic!("Out of ids");
}
while !id_set.insert(*counter) {
*counter = counter.wrapping_add(1);
}
let new_id = *counter;
*counter = counter.wrapping_add(1);
new_id
}
let id = loop {
let new_id = $id_name.fetch_update(
std::sync::atomic::Ordering::SeqCst,
std::sync::atomic::Ordering::SeqCst,
|mut id| {
while ids.iter().any(|k| *k == id) {
id += 1;
}
id += 1;
Some(id)
},
);
if let Ok(id) = new_id {
break id;
}
};
ids.insert(id);
id
pub(crate) fn remove(id: usize) -> bool {
ID_DATA.lock().unwrap().0.remove(&id)
}
}
};
}

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
crate::utils::ids::id_gen!(next_hooks_id, HOOK_ID, HOOKS_IDS);
crate::utils::ids::id_gen!(hooks_id);
/// Unique hook identifier used to unregister commit/descruction hooks
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@ -23,7 +23,7 @@ impl<T: ?Sized> Clone for Hook<T> {
impl<T: ?Sized> Hook<T> {
pub fn new(cb: Arc<T>) -> Self {
Self {
id: HookId(next_hooks_id()),
id: HookId(hooks_id::next()),
cb,
}
}
@ -31,6 +31,6 @@ impl<T: ?Sized> Hook<T> {
impl<T: ?Sized> Drop for Hook<T> {
fn drop(&mut self) {
HOOKS_IDS.lock().unwrap().remove(&self.id.0);
hooks_id::remove(self.id.0);
}
}

View file

@ -699,7 +699,7 @@ impl DmabufState {
+ 'static,
F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static,
{
let id = next_global_id();
let id = global_id::next();
let formats = formats
.or_else(|| default_feedback.map(|f| f.main_formats()))
@ -785,7 +785,7 @@ impl DmabufState {
/// It is highly recommended you disable the global before destroying it and ensure all child objects have
/// been destroyed.
pub fn destroy_global<D: 'static>(&mut self, display: &DisplayHandle, global: DmabufGlobal) {
if DMABUF_GLOBAL_IDS.lock().unwrap().remove(&global.id) {
if global_id::remove(global.id) {
if let Some(global_state) = self.globals.remove(&global.id) {
display.remove_global::<D>(global_state.id);
}
@ -1234,4 +1234,4 @@ impl DmabufParamsData {
}
}
id_gen!(next_global_id, DMABUF_GLOBAL_ID, DMABUF_GLOBAL_IDS);
id_gen!(global_id);

View file

@ -216,7 +216,7 @@ pub use self::atoms::Atoms;
use super::XWaylandClientData;
crate::utils::ids::id_gen!(next_xwm_id, XWM_ID, XWM_IDS);
crate::utils::ids::id_gen!(xwm_id);
/// Id of an X11 WM
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -411,7 +411,7 @@ impl Drop for X11Wm {
fn drop(&mut self) {
// TODO: Not really needed for Xwayland, but maybe cleanup set root properties?
let _ = self.conn.destroy_window(self.wm_window);
XWM_IDS.lock().unwrap().remove(&self.id.0);
xwm_id::remove(self.id.0);
}
}
@ -682,7 +682,7 @@ impl X11Wm {
D: xwayland_shell::XWaylandShellHandler,
D: 'static,
{
let id = XwmId(next_xwm_id());
let id = XwmId(xwm_id::next());
let span = debug_span!("xwayland_wm", id = id.0);
let _guard = span.enter();