@@ -15,7 +15,7 @@ use crate::{
1515    cache:: { SharedUriCache ,  UriCache } , 
1616    hasher:: BuildNoHashHasher , 
1717    list:: List , 
18-     meta, 
18+     meta:: { self ,  metas_for_draft } , 
1919    resource:: { unescape_segment,  InnerResourcePtr ,  JsonSchemaResource } , 
2020    uri, 
2121    vocabularies:: { self ,  VocabularySet } , 
@@ -44,34 +44,8 @@ type DocumentStore = AHashMap<Arc<Uri<String>>, Pin<Arc<ValueWrapper>>>;
4444type  ResourceMap  = AHashMap < Arc < Uri < String > > ,  InnerResourcePtr > ; 
4545
4646/// Pre-loaded registry containing all JSON Schema meta-schemas and their vocabularies 
47- pub  static  SPECIFICATIONS :  Lazy < Registry >  = Lazy :: new ( || { 
48-     let  pairs = meta:: META_SCHEMAS . into_iter ( ) . map ( |( uri,  schema) | { 
49-         ( 
50-             uri, 
51-             ResourceRef :: from_contents ( schema) . expect ( "Invalid resource" ) , 
52-         ) 
53-     } ) ; 
54- 
55-     // The capacity is known upfront 
56-     let  mut  documents = DocumentStore :: with_capacity ( 18 ) ; 
57-     let  mut  resources = ResourceMap :: with_capacity ( 18 ) ; 
58-     let  mut  anchors = AHashMap :: with_capacity ( 8 ) ; 
59-     let  mut  resolution_cache = UriCache :: with_capacity ( 35 ) ; 
60-     process_meta_schemas ( 
61-         pairs, 
62-         & mut  documents, 
63-         & mut  resources, 
64-         & mut  anchors, 
65-         & mut  resolution_cache, 
66-     ) 
67-     . expect ( "Failed to process meta schemas" ) ; 
68-     Registry  { 
69-         documents, 
70-         resources, 
71-         anchors, 
72-         resolution_cache :  resolution_cache. into_shared ( ) , 
73-     } 
74- } ) ; 
47+ pub  static  SPECIFICATIONS :  Lazy < Registry >  =
48+     Lazy :: new ( || Registry :: build_from_meta_schemas ( meta:: META_SCHEMAS_ALL . as_slice ( ) ) ) ; 
7549
7650/// A registry of JSON Schema resources, each identified by their canonical URIs. 
7751/// 
@@ -563,6 +537,42 @@ impl Registry {
563537            _ => unreachable ! ( ) , 
564538        } 
565539    } 
540+ 
541+     /// Build a registry with all the given meta-schemas from specs. 
542+ pub ( crate )  fn  build_from_meta_schemas ( schemas :  & [ ( & ' static  str ,  & ' static  Value ) ] )  -> Self  { 
543+         let  schemas_count = schemas. len ( ) ; 
544+         let  pairs = schemas. into_iter ( ) . map ( |( uri,  schema) | { 
545+             ( 
546+                 uri, 
547+                 ResourceRef :: from_contents ( schema) . expect ( "Invalid resource" ) , 
548+             ) 
549+         } ) ; 
550+ 
551+         let  mut  documents = DocumentStore :: with_capacity ( schemas_count) ; 
552+         let  mut  resources = ResourceMap :: with_capacity ( schemas_count) ; 
553+ 
554+         // The actual number of anchors and cache-entries varies across 
555+         // drafts. We overshoot here to avoid reallocations, using the sum 
556+         // over all specifications. 
557+         let  mut  anchors = AHashMap :: with_capacity ( 8 ) ; 
558+         let  mut  resolution_cache = UriCache :: with_capacity ( 35 ) ; 
559+ 
560+         process_meta_schemas ( 
561+             pairs, 
562+             & mut  documents, 
563+             & mut  resources, 
564+             & mut  anchors, 
565+             & mut  resolution_cache, 
566+         ) 
567+         . expect ( "Failed to process meta schemas" ) ; 
568+ 
569+         Self  { 
570+             documents, 
571+             resources, 
572+             anchors, 
573+             resolution_cache :  resolution_cache. into_shared ( ) , 
574+         } 
575+     } 
566576} 
567577
568578fn  process_meta_schemas ( 
@@ -706,15 +716,18 @@ fn handle_metaschemas(
706716    refers_metaschemas :  bool , 
707717    resources :  & mut  ResourceMap , 
708718    anchors :  & mut  AHashMap < AnchorKey ,  Anchor > , 
719+     draft_version :  Draft , 
709720)  { 
710721    if  refers_metaschemas { 
711-         resources. reserve ( SPECIFICATIONS . resources . len ( ) ) ; 
712-         for  ( key,  resource)  in  & SPECIFICATIONS . resources  { 
713-             resources. insert ( Arc :: clone ( key) ,  resource. clone ( ) ) ; 
722+         let  schemas = metas_for_draft ( draft_version) ; 
723+         let  draft_registry = Registry :: build_from_meta_schemas ( schemas) ; 
724+         resources. reserve ( draft_registry. resources . len ( ) ) ; 
725+         for  ( key,  resource)  in  draft_registry. resources . into_iter ( )  { 
726+             resources. insert ( key,  resource. clone ( ) ) ; 
714727        } 
715-         anchors. reserve ( SPECIFICATIONS . anchors . len ( ) ) ; 
716-         for  ( key,  anchor)  in  & SPECIFICATIONS . anchors  { 
717-             anchors. insert ( key. clone ( ) ,  anchor. clone ( ) ) ; 
728+         anchors. reserve ( draft_registry . anchors . len ( ) ) ; 
729+         for  ( key,  anchor)  in  draft_registry . anchors . into_iter ( )  { 
730+             anchors. insert ( key,  anchor) ; 
718731        } 
719732    } 
720733} 
@@ -783,7 +796,7 @@ fn process_resources(
783796        } 
784797    } 
785798
786-     handle_metaschemas ( state. refers_metaschemas ,  resources,  anchors) ; 
799+     handle_metaschemas ( state. refers_metaschemas ,  resources,  anchors,  default_draft ) ; 
787800
788801    Ok ( ( ) ) 
789802} 
@@ -860,7 +873,7 @@ async fn process_resources_async(
860873        } 
861874    } 
862875
863-     handle_metaschemas ( state. refers_metaschemas ,  resources,  anchors) ; 
876+     handle_metaschemas ( state. refers_metaschemas ,  resources,  anchors,  default_draft ) ; 
864877
865878    Ok ( ( ) ) 
866879} 
0 commit comments