diff --git a/framework_crates/bones_schema/macros/src/lib.rs b/framework_crates/bones_schema/macros/src/lib.rs index 27d19e943a..8fbb6f1d3f 100644 --- a/framework_crates/bones_schema/macros/src/lib.rs +++ b/framework_crates/bones_schema/macros/src/lib.rs @@ -282,9 +282,8 @@ pub fn derive_has_schema(input: TokenStream) -> TokenStream { let register_schema = if input.generic_params().is_some() { quote! { - static S: OnceLock>> = OnceLock::new(); let schema = { - S.get_or_init(Default::default) + #schema_mod::registry::GENERIC_SCHEMA_CACHE .read() .get(&TypeId::of::()) .copied() @@ -292,7 +291,7 @@ pub fn derive_has_schema(input: TokenStream) -> TokenStream { schema.unwrap_or_else(|| { let schema = compute_schema(); - S.get_or_init(Default::default) + #schema_mod::registry::GENERIC_SCHEMA_CACHE .write() .insert(TypeId::of::(), schema); @@ -395,13 +394,9 @@ pub fn derive_has_schema(input: TokenStream) -> TokenStream { quote! { unsafe impl<#impl_bounds> #schema_mod::HasSchema for #name<#struct_params> { fn schema() -> &'static #schema_mod::Schema { - use ::std::sync::OnceLock; - use ::std::any::TypeId; - use bones_utils::HashMap; - use parking_lot::RwLock; - static S: OnceLock>> = OnceLock::new(); + use ::std::{any::TypeId, sync::OnceLock}; let schema = { - S.get_or_init(Default::default) + #schema_mod::registry::GENERIC_SCHEMA_CACHE .read() .get(&TypeId::of::()) .copied() @@ -409,13 +404,12 @@ pub fn derive_has_schema(input: TokenStream) -> TokenStream { schema.unwrap_or_else(|| { let schema = #schema_register; - S.get_or_init(Default::default) + #schema_mod::registry::GENERIC_SCHEMA_CACHE .write() .insert(TypeId::of::(), schema); schema }) - } } } diff --git a/framework_crates/bones_schema/src/registry.rs b/framework_crates/bones_schema/src/registry.rs index 76b6dcd7e3..ab7c2f6f72 100644 --- a/framework_crates/bones_schema/src/registry.rs +++ b/framework_crates/bones_schema/src/registry.rs @@ -2,11 +2,16 @@ use std::{ alloc::Layout, - sync::atomic::{AtomicU32, Ordering::SeqCst}, + any::TypeId, + sync::{ + atomic::{AtomicU32, Ordering::SeqCst}, + LazyLock, + }, }; use append_only_vec::AppendOnlyVec; -use bones_utils::Deref; +use bones_utils::{Deref, HashMap}; +use parking_lot::RwLock; use crate::prelude::*; @@ -143,6 +148,10 @@ pub static SCHEMA_REGISTRY: SchemaRegistry = SchemaRegistry { schemas: AppendOnlyVec::new(), }; +#[doc(hidden)] +pub static GENERIC_SCHEMA_CACHE: LazyLock>> = + LazyLock::new(Default::default); + #[cfg(test)] mod test { use bones_utils::default;