From d1776c9fea8e69432bd5e90f66847537ef28ae9e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 4 Apr 2021 22:00:26 +0200 Subject: [PATCH] feat(config): convert id generator to local storage --- espanso-config/src/counter.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/espanso-config/src/counter.rs b/espanso-config/src/counter.rs index 80400fb..3cab3db 100644 --- a/espanso-config/src/counter.rs +++ b/espanso-config/src/counter.rs @@ -17,16 +17,17 @@ * along with espanso. If not, see . */ -use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicI32, Ordering}; -static STRUCT_COUNTER: AtomicUsize = AtomicUsize::new(0); +thread_local! { + static STRUCT_COUNTER: AtomicI32 = AtomicI32::new(0); +} -pub type StructId = usize; +pub type StructId = i32; -/// For performance reasons, some structs need a unique id to be -/// compared efficiently with one another. +/// Some structs need a unique id. /// In order to generate it, we use an atomic static variable /// that is incremented for each struct. pub fn next_id() -> StructId { - STRUCT_COUNTER.fetch_add(1, Ordering::SeqCst) + STRUCT_COUNTER.with(|count| count.fetch_add(1, Ordering::SeqCst)) }