feat(config): convert id generator to local storage

This commit is contained in:
Federico Terzi 2021-04-04 22:00:26 +02:00
parent 3a0c397410
commit d1776c9fea

View File

@ -17,16 +17,17 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 /// Some structs need a unique id.
/// compared efficiently with one another.
/// In order to generate it, we use an atomic static variable /// In order to generate it, we use an atomic static variable
/// that is incremented for each struct. /// that is incremented for each struct.
pub fn next_id() -> StructId { pub fn next_id() -> StructId {
STRUCT_COUNTER.fetch_add(1, Ordering::SeqCst) STRUCT_COUNTER.with(|count| count.fetch_add(1, Ordering::SeqCst))
} }