Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Rework Registry #686

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ jobs:
files: lcov.info
fail_ci_if_error: true

miri:
name: Test with Miri
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
submodules: true

- uses: dtolnay/rust-toolchain@nightly
with:
components: miri

- uses: Swatinem/rust-cache@v2
with:
cache-all-crates: "true"

- name: Run tests with Miri
run: cargo miri test -p referencing --lib

lints-python:
name: Python lints
runs-on: ubuntu-22.04
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Performance

- Significantly improved validator compilation speed by using pointer-based references to schema fragments instead of cloning them during traversal.

## [0.28.3] - 2025-01-24

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ test-py-no-rebuild *FLAGS:
bench-py *FLAGS:
uvx --with="crates/jsonschema-py[bench]" --refresh pytest crates/jsonschema-py/benches/bench.py --benchmark-columns=min {{FLAGS}}

miri:
cargo +nightly miri test -p referencing
4 changes: 4 additions & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Performance

- Significantly improved validator compilation speed by using pointer-based references to schema fragments instead of cloning them during traversal.

## [0.28.3] - 2025-01-24

### Fixed
Expand Down
1 change: 1 addition & 0 deletions crates/jsonschema-referencing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license.workspace = true
ahash.workspace = true
fluent-uri = { version = "0.3.2", features = ["serde"] }
once_cell = "1.20.1"
parking_lot = "0.12.3"
percent-encoding = "2.3.1"
serde_json.workspace = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/meta/format-annotation",
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/format-annotation": true
},
"$dynamicAnchor": "meta",

"title": "Format vocabulary meta-schema for annotation results",
"type": ["object", "boolean"],
"properties": {
"format": { "type": "string" }
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/meta/format-annotation",
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/format-annotation": true
},
"$dynamicAnchor": "meta",
"title": "Format vocabulary meta-schema for annotation results",
"type": [
"object",
"boolean"
],
"properties": {
"format": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/meta/format-assertion",
"$dynamicAnchor": "meta",
"title": "Format vocabulary meta-schema for assertion results",
"type": [
"object",
"boolean"
],
"properties": {
"format": {
"type": "string"
}
}
}
11 changes: 7 additions & 4 deletions crates/jsonschema-referencing/src/anchors/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
use std::{
borrow::Borrow,
hash::{Hash, Hasher},
sync::Arc,
};

use fluent_uri::Uri;

use super::AnchorName;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct AnchorKey {
uri: Uri<String>,
name: String,
uri: Arc<Uri<String>>,
name: AnchorName,
}

impl AnchorKey {
pub(crate) fn new(uri: Uri<String>, name: String) -> Self {
pub(crate) fn new(uri: Arc<Uri<String>>, name: AnchorName) -> Self {
Self { uri, name }
}
}
Expand All @@ -50,7 +53,7 @@ pub(crate) trait BorrowDyn {

impl BorrowDyn for AnchorKey {
fn borrowed_key(&self) -> AnchorKeyRef {
AnchorKeyRef::new(&self.uri, &self.name)
AnchorKeyRef::new(&self.uri, self.name.as_str())
}
}

Expand Down
113 changes: 80 additions & 33 deletions crates/jsonschema-referencing/src/anchors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,82 @@
use std::sync::Arc;
use std::{
hash::Hash,
sync::atomic::{AtomicPtr, Ordering},
};

use serde_json::Value;

mod keys;

use crate::{Draft, Error, Resolved, Resolver, Resource};
use crate::{resource::InnerResourcePtr, Draft, Error, Resolved, Resolver};
pub(crate) use keys::{AnchorKey, AnchorKeyRef};

#[derive(Debug)]
pub(crate) struct AnchorName {
ptr: AtomicPtr<u8>,
len: usize,
}

impl AnchorName {
fn new(s: &str) -> Self {
Self {
ptr: AtomicPtr::new(s.as_ptr().cast_mut()),
len: s.len(),
}
}

#[allow(unsafe_code)]
fn as_str(&self) -> &str {
// SAFETY: The pointer is valid as long as the registry exists
unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
self.ptr.load(Ordering::Relaxed),
self.len,
))
}
}
}

impl Clone for AnchorName {
fn clone(&self) -> Self {
Self {
Stranger6667 marked this conversation as resolved.
Show resolved Hide resolved
ptr: AtomicPtr::new(self.ptr.load(Ordering::Relaxed)),
len: self.len,
}
}
}

impl Hash for AnchorName {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
Stranger6667 marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl PartialEq for AnchorName {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}

impl Eq for AnchorName {}

/// An anchor within a resource.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub(crate) enum Anchor {
Default {
draft: Draft,
name: String,
resource: Arc<Resource>,
name: AnchorName,
Stranger6667 marked this conversation as resolved.
Show resolved Hide resolved
resource: InnerResourcePtr,
},
/// Dynamic anchors from Draft 2020-12.
Dynamic {
draft: Draft,
name: String,
resource: Arc<Resource>,
name: AnchorName,
resource: InnerResourcePtr,
},
}

impl Anchor {
/// Anchor's name.
pub(crate) fn name(&self) -> &str {
pub(crate) fn name(&self) -> AnchorName {
match self {
Anchor::Default { name, .. } | Anchor::Dynamic { name, .. } => name,
Anchor::Default { name, .. } | Anchor::Dynamic { name, .. } => name.clone(),
}
Stranger6667 marked this conversation as resolved.
Show resolved Hide resolved
}
/// Get the resource for this anchor.
Expand All @@ -38,10 +87,10 @@ impl Anchor {
resolver,
resource.draft(),
)),
Anchor::Dynamic { name, resource, .. } => {
Anchor::Dynamic { name, resource } => {
let mut last = resource;
for uri in &resolver.dynamic_scope() {
match resolver.registry.anchor(uri, name) {
match resolver.registry.anchor(uri, name.as_str()) {
Ok(anchor) => {
if let Anchor::Dynamic { resource, .. } = anchor {
last = resource;
Expand All @@ -53,7 +102,7 @@ impl Anchor {
}
Ok(Resolved::new(
last.contents(),
resolver.in_subresource((**last).as_ref())?,
resolver.in_subresource_inner(last)?,
last.draft(),
))
}
Expand All @@ -68,18 +117,16 @@ pub(crate) fn anchor(draft: Draft, contents: &Value) -> Box<dyn Iterator<Item =
.get("$anchor")
.and_then(Value::as_str)
.map(|name| Anchor::Default {
draft,
name: name.to_string(),
resource: Arc::new(draft.create_resource(contents.clone())),
name: AnchorName::new(name),
resource: InnerResourcePtr::new(contents, draft),
});

let dynamic_anchor = schema
.get("$dynamicAnchor")
.and_then(Value::as_str)
.map(|name| Anchor::Dynamic {
draft,
name: name.to_string(),
resource: Arc::new(draft.create_resource(contents.clone())),
name: AnchorName::new(name),
resource: InnerResourcePtr::new(contents, draft),
});

default_anchor.into_iter().chain(dynamic_anchor)
Expand All @@ -90,11 +137,11 @@ pub(crate) fn anchor_2019(draft: Draft, contents: &Value) -> Box<dyn Iterator<It
Box::new(
contents
.as_object()
.and_then(|schema| schema.get("$anchor").and_then(Value::as_str))
.and_then(|schema| schema.get("$anchor"))
.and_then(Value::as_str)
.map(move |name| Anchor::Default {
draft,
name: name.to_string(),
resource: Arc::new(draft.create_resource(contents.clone())),
name: AnchorName::new(name),
resource: InnerResourcePtr::new(contents, draft),
})
.into_iter(),
)
Expand All @@ -107,12 +154,12 @@ pub(crate) fn legacy_anchor_in_dollar_id(
Box::new(
contents
.as_object()
.and_then(|schema| schema.get("$id").and_then(Value::as_str))
.and_then(|schema| schema.get("$id"))
.and_then(Value::as_str)
.and_then(|id| id.strip_prefix('#'))
.map(move |id| Anchor::Default {
draft,
name: id.to_string(),
resource: Arc::new(draft.create_resource(contents.clone())),
name: AnchorName::new(id),
resource: InnerResourcePtr::new(contents, draft),
})
.into_iter(),
)
Expand All @@ -125,12 +172,12 @@ pub(crate) fn legacy_anchor_in_id<'a>(
Box::new(
contents
.as_object()
.and_then(|schema| schema.get("id").and_then(Value::as_str))
.and_then(|schema| schema.get("id"))
.and_then(Value::as_str)
.and_then(|id| id.strip_prefix('#'))
.map(move |id| Anchor::Default {
draft,
name: id.to_string(),
resource: Arc::new(draft.create_resource(contents.clone())),
name: AnchorName::new(id),
resource: InnerResourcePtr::new(contents, draft),
})
.into_iter(),
)
Expand Down
Loading
Loading