-
Notifications
You must be signed in to change notification settings - Fork 3
Optimize schema-comparator diffing performance #94
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
Open
swalkinshaw
wants to merge
1
commit into
main
Choose a base branch
from
schema-comparator-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| use bluejay_parser::ast::{ | ||
| definition::{DefinitionDocument, SchemaDefinition}, | ||
| Parse, | ||
| }; | ||
| use bluejay_schema_comparator::compare; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use std::sync::LazyLock; | ||
|
|
||
| fn data_path(name: &str) -> std::path::PathBuf { | ||
| std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| .join("../data") | ||
| .join(name) | ||
| } | ||
|
|
||
| fn load_schema(name: &str) -> SchemaDefinition<'static> { | ||
| let src = std::fs::read_to_string(data_path(name)).unwrap(); | ||
| let doc: DefinitionDocument = DefinitionDocument::parse(Box::leak(src.into_boxed_str())) | ||
| .result | ||
| .expect("Schema had parse errors"); | ||
| let leaked = Box::leak(Box::new(doc)); | ||
| SchemaDefinition::try_from(&*leaked).expect("Schema had errors") | ||
| } | ||
|
|
||
| static DOCS_SCHEMA: LazyLock<SchemaDefinition<'static>> = | ||
| LazyLock::new(|| load_schema("schema.docs.graphql")); | ||
|
|
||
| static ADMIN_OLD: LazyLock<SchemaDefinition<'static>> = | ||
| LazyLock::new(|| load_schema("admin_schema_2024-07_public.graphql")); | ||
|
|
||
| static ADMIN_NEW: LazyLock<SchemaDefinition<'static>> = | ||
| LazyLock::new(|| load_schema("admin_schema_2026-01_public.graphql")); | ||
|
|
||
| fn compare_identical(c: &mut Criterion) { | ||
| c.bench_function("identical docs schema (50k lines)", |b| { | ||
| b.iter(|| compare(&*DOCS_SCHEMA, &*DOCS_SCHEMA)); | ||
| }); | ||
|
|
||
| c.bench_function("identical admin schema (106k lines)", |b| { | ||
| b.iter(|| compare(&*ADMIN_OLD, &*ADMIN_OLD)); | ||
| }); | ||
| } | ||
|
|
||
| fn compare_with_changes(c: &mut Criterion) { | ||
| c.bench_function("docs schema vs itself (no changes)", |b| { | ||
| b.iter(|| compare(&*DOCS_SCHEMA, &*DOCS_SCHEMA)); | ||
| }); | ||
|
Comment on lines
+43
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused because it's called |
||
|
|
||
| c.bench_function("admin schema across versions", |b| { | ||
| b.iter(|| compare(&*ADMIN_OLD, &*ADMIN_NEW)); | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, compare_identical, compare_with_changes); | ||
| criterion_main!(benches); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,13 +10,43 @@ use bluejay_printer::value::ValuePrinter; | |
| use std::borrow::Cow; | ||
| use strum::AsRefStr; | ||
|
|
||
| #[derive(Eq, Ord, PartialEq, PartialOrd)] | ||
| #[derive(Eq, PartialEq)] | ||
| pub enum Criticality { | ||
| Breaking { reason: Cow<'static, str> }, | ||
| Dangerous { reason: Cow<'static, str> }, | ||
| Safe { reason: Cow<'static, str> }, | ||
| } | ||
|
|
||
| /// Numeric ordering: Breaking(2) > Dangerous(1) > Safe(0) | ||
| #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] | ||
| pub enum CriticalityLevel { | ||
| Safe = 0, | ||
| Dangerous = 1, | ||
| Breaking = 2, | ||
| } | ||
|
|
||
| impl Criticality { | ||
| pub fn level(&self) -> CriticalityLevel { | ||
| match self { | ||
| Self::Breaking { .. } => CriticalityLevel::Breaking, | ||
| Self::Dangerous { .. } => CriticalityLevel::Dangerous, | ||
| Self::Safe { .. } => CriticalityLevel::Safe, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Ord for Criticality { | ||
| fn cmp(&self, other: &Self) -> std::cmp::Ordering { | ||
| self.level().cmp(&other.level()) | ||
| } | ||
| } | ||
|
|
||
| impl PartialOrd for Criticality { | ||
| fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | ||
| Some(self.cmp(other)) | ||
| } | ||
| } | ||
|
|
||
| impl Criticality { | ||
| fn breaking(reason: Option<Cow<'static, str>>) -> Self { | ||
| Self::Breaking { | ||
|
|
@@ -239,15 +269,144 @@ pub enum Change<'a, S: SchemaDefinition> { | |
|
|
||
| impl<S: SchemaDefinition> Change<'_, S> { | ||
| pub fn breaking(&self) -> bool { | ||
| matches!(self.criticality(), Criticality::Breaking { .. }) | ||
| matches!(self.criticality_level(), CriticalityLevel::Breaking) | ||
| } | ||
|
|
||
| pub fn non_breaking(&self) -> bool { | ||
| matches!(self.criticality(), Criticality::Safe { .. }) | ||
| matches!(self.criticality_level(), CriticalityLevel::Safe) | ||
| } | ||
|
|
||
| pub fn dangerous(&self) -> bool { | ||
| matches!(self.criticality(), Criticality::Dangerous { .. }) | ||
| matches!(self.criticality_level(), CriticalityLevel::Dangerous) | ||
| } | ||
|
|
||
| /// Cheap criticality check without allocating reason strings. | ||
| pub fn criticality_level(&self) -> CriticalityLevel { | ||
| match self { | ||
| Self::TypeRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::TypeAdded { .. } => CriticalityLevel::Safe, | ||
| Self::TypeKindChanged { .. } => CriticalityLevel::Breaking, | ||
| Self::TypeDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::FieldAdded { .. } => CriticalityLevel::Safe, | ||
| Self::FieldRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::FieldDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::FieldTypeChanged { | ||
| type_name: _, | ||
| old_field_definition: old_field, | ||
| new_field_definition: new_field, | ||
| } => { | ||
| if is_change_safe_for_field::<S>( | ||
| old_field.r#type().as_shallow_ref(), | ||
| new_field.r#type().as_shallow_ref(), | ||
| ) { | ||
| CriticalityLevel::Safe | ||
| } else { | ||
| CriticalityLevel::Breaking | ||
| } | ||
| } | ||
| Self::FieldArgumentAdded { | ||
| type_name: _, | ||
| field_definition: _, | ||
| argument_definition: argument, | ||
| } => { | ||
| if argument.r#type().is_required() && argument.default_value().is_none() { | ||
| CriticalityLevel::Breaking | ||
| } else { | ||
| CriticalityLevel::Safe | ||
| } | ||
| } | ||
| Self::FieldArgumentRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::FieldArgumentDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::FieldArgumentDefaultValueChanged { .. } => CriticalityLevel::Dangerous, | ||
| Self::FieldArgumentTypeChanged { | ||
| type_name: _, | ||
| field_definition: _, | ||
| old_argument_definition: old_argument, | ||
| new_argument_definition: new_argument, | ||
| } => { | ||
| if is_change_safe_for_input_value::<S>( | ||
| old_argument.r#type().as_shallow_ref(), | ||
| new_argument.r#type().as_shallow_ref(), | ||
| ) { | ||
| CriticalityLevel::Safe | ||
| } else { | ||
| CriticalityLevel::Breaking | ||
| } | ||
| } | ||
| Self::ObjectInterfaceAddition { .. } => CriticalityLevel::Dangerous, | ||
| Self::ObjectInterfaceRemoval { .. } => CriticalityLevel::Breaking, | ||
| Self::EnumValueAdded { .. } => CriticalityLevel::Dangerous, | ||
| Self::EnumValueRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::EnumValueDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::UnionMemberAdded { .. } => CriticalityLevel::Dangerous, | ||
| Self::UnionMemberRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::InputFieldAdded { | ||
| input_object_type_definition: _, | ||
| added_field_definition: added_field, | ||
| } => { | ||
| if added_field.r#type().is_required() && added_field.default_value().is_none() { | ||
| CriticalityLevel::Breaking | ||
| } else { | ||
| CriticalityLevel::Safe | ||
| } | ||
| } | ||
| Self::InputFieldRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::InputFieldDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::InputFieldTypeChanged { | ||
| input_object_type_definition: _, | ||
| old_field_definition: old_field, | ||
| new_field_definition: new_field, | ||
| } => { | ||
| if is_change_safe_for_input_value::<S>( | ||
| old_field.r#type().as_shallow_ref(), | ||
| new_field.r#type().as_shallow_ref(), | ||
| ) { | ||
| CriticalityLevel::Safe | ||
| } else { | ||
| CriticalityLevel::Breaking | ||
| } | ||
| } | ||
| Self::InputFieldDefaultValueChanged { .. } => CriticalityLevel::Dangerous, | ||
| Self::DirectiveDefinitionAdded { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveDefinitionRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::DirectiveDefinitionLocationAdded { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveDefinitionLocationRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::DirectiveDefinitionDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveDefinitionArgumentAdded { | ||
| directive_definition: _, | ||
| argument_definition, | ||
| } => { | ||
| if argument_definition.is_required() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we be checking for a default value? |
||
| CriticalityLevel::Breaking | ||
| } else { | ||
| CriticalityLevel::Safe | ||
| } | ||
| } | ||
| Self::DirectiveDefinitionArgumentRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::DirectiveDefinitionArgumentDescriptionChanged { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveDefinitionArgumentTypeChanged { | ||
| directive_definition: _, | ||
| old_argument_definition, | ||
| new_argument_definition, | ||
| } => { | ||
| if is_change_safe_for_input_value::<S>( | ||
| old_argument_definition.r#type().as_shallow_ref(), | ||
| new_argument_definition.r#type().as_shallow_ref(), | ||
| ) { | ||
| CriticalityLevel::Safe | ||
| } else { | ||
| CriticalityLevel::Breaking | ||
| } | ||
| } | ||
| Self::DirectiveDefinitionArgumentDefaultValueChanged { .. } => { | ||
| CriticalityLevel::Dangerous | ||
| } | ||
| Self::DirectiveAdded { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveRemoved { .. } => CriticalityLevel::Breaking, | ||
| Self::DirectiveArgumentAdded { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveArgumentRemoved { .. } => CriticalityLevel::Safe, | ||
| Self::DirectiveArgumentValueChanged { .. } => CriticalityLevel::Safe, | ||
| } | ||
| } | ||
|
|
||
| pub fn criticality(&self) -> Criticality { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
include_str!to avoid some of the leaking? Or would that slow down compiles a lot?