jspg performance optimizations

This commit is contained in:
2026-02-18 01:39:00 -05:00
parent e55977c11b
commit 53a40d1099
6 changed files with 230 additions and 156 deletions

View File

@ -25,7 +25,7 @@ struct TestCase {
expected: Option<serde_json::Value>,
}
use crate::registry::REGISTRY;
// use crate::registry::REGISTRY; // No longer used directly for tests!
use crate::validator::Validator;
use serde_json::Value;
@ -38,12 +38,6 @@ where
}
pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
// Clear registry to ensure isolation
// {
// let mut registry = REGISTRY.write().unwrap();
// registry.clear();
// }
let content =
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path));
let suite: Vec<TestSuite> = serde_json::from_str(&content)
@ -56,6 +50,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
let group = &suite[index];
let mut failures = Vec::<String>::new();
// Create Local Registry for this test group
let mut registry = crate::registry::Registry::new();
// Helper to register items with 'schemas'
@ -69,12 +64,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
if let Ok(schema) =
serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
{
// Clone ID upfront to avoid borrow issues
if let Some(id_clone) = schema.obj.id.clone() {
let compiled =
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
registry.insert(id_clone, compiled);
}
registry.add(schema);
}
}
}
@ -118,8 +108,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
});
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
registry.insert(id, compiled);
registry.add(schema);
}
}
}
@ -134,20 +123,16 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
// Some tests use a raw 'schema' or 'schemas' field at the group level
if let Some(schema_val) = &group.schema {
match serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) {
Ok(schema) => {
let id = schema
.obj
.id
.clone()
.or_else(|| {
// Fallback ID if none provided in schema
Some(format!("test:{}:{}", path, index))
})
.unwrap();
let mut registry_ref = &mut registry;
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
registry_ref.insert(id, compiled);
Ok(mut schema) => {
let id_clone = schema.obj.id.clone();
if id_clone.is_some() {
registry.add(schema);
} else {
// Fallback ID if none provided in schema
let id = format!("test:{}:{}", path, index);
schema.obj.id = Some(id);
registry.add(schema);
}
}
Err(e) => {
eprintln!(
@ -158,6 +143,9 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
}
}
// Create Validator Instance (Takes ownership of registry)
let validator = Validator::new(registry);
// 4. Run Tests
for (_test_index, test) in group.tests.iter().enumerate() {
let mut schema_id = test.schema_id.clone();
@ -193,7 +181,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
}
if let Some(sid) = schema_id {
let result = Validator::validate_with_registry(&sid, &test.data, &registry);
let result = validator.validate(&sid, &test.data);
if !result.errors.is_empty() != !test.valid {
failures.push(format!(
@ -227,8 +215,11 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
let mut failures = Vec::<String>::new();
for (group_index, group) in suite.into_iter().enumerate() {
// Create Isolated Registry for this test group
let mut registry = crate::registry::Registry::new();
// Helper to register items with 'schemas'
let register_schemas = |items_val: Option<Value>| {
let register_schemas = |registry: &mut crate::registry::Registry, items_val: Option<Value>| {
if let Some(val) = items_val {
if let Value::Array(arr) = val {
for item in arr {
@ -238,14 +229,7 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
if let Ok(schema) =
serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
{
// Clone ID upfront to avoid borrow issues
if let Some(id_clone) = schema.obj.id.clone() {
let mut registry = REGISTRY.write().unwrap();
// Utilize the new compile method which handles strictness
let compiled =
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
registry.insert(id_clone, compiled);
}
registry.add(schema);
}
}
}
@ -291,18 +275,16 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
});
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
let mut registry = REGISTRY.write().unwrap();
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
registry.insert(id, compiled);
registry.add(schema);
}
}
}
}
// Register 'types', 'enums', and 'puncs' if present (JSPG style)
register_schemas(group.types);
register_schemas(group.enums);
register_schemas(group.puncs);
register_schemas(&mut registry, group.types);
register_schemas(&mut registry, group.enums);
register_schemas(&mut registry, group.puncs);
// Register main 'schema' if present (Standard style)
// Ensure ID is a valid URI to avoid Url::parse errors in Compiler
@ -310,18 +292,25 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
// Register main 'schema' if present (Standard style)
if let Some(ref schema_val) = group.schema {
let mut registry = REGISTRY.write().unwrap();
let schema: crate::schema::Schema =
let mut schema: crate::schema::Schema =
serde_json::from_value(schema_val.clone()).expect("Failed to parse test schema");
let compiled = crate::compiler::Compiler::compile(schema, Some(unique_id.clone()));
registry.insert(unique_id.clone(), compiled);
// If schema has no ID, assign unique_id and use add() or manual insert?
// Compiler needs ID. Registry::add needs ID.
if schema.obj.id.is_none() {
schema.obj.id = Some(unique_id.clone());
}
registry.add(schema);
}
// Create Instance (Takes Ownership)
let validator = Validator::new(registry);
for test in group.tests {
// Use explicit schema_id from test, or default to unique_id
let schema_id = test.schema_id.as_deref().unwrap_or(&unique_id).to_string();
let drop = Validator::validate(&schema_id, &test.data);
let drop = validator.validate(&schema_id, &test.data);
if test.valid {
if !drop.errors.is_empty() {