Skip to content

Commit

Permalink
Improve JsonSchema.InitializeSchemaCollection performance (#1760)
Browse files Browse the repository at this point in the history
* don't enumerate if no data
* removal is rare so don't create separate traversing array
  • Loading branch information
lahma authored Nov 28, 2024
1 parent c740804 commit 55fd77e
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/NJsonSchema/JsonSchema.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ internal object? DiscriminatorRaw
{
if (value is string)
{
Discriminator = (string)value;
Discriminator = (string) value;
}
else if (value != null)
{
DiscriminatorObject = ((JObject)value).ToObject<OpenApiDiscriminator>();
DiscriminatorObject = ((JObject) value).ToObject<OpenApiDiscriminator>();
}
}
}
Expand All @@ -139,12 +139,12 @@ internal object? DiscriminatorRaw
[JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object? ExclusiveMaximumRaw
{
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object)true : null);
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object) true : null);
set
{
if (value is bool)
{
IsExclusiveMaximum = (bool)value;
IsExclusiveMaximum = (bool) value;
}
else if (value != null && (value.Equals("true") || value.Equals("false")))
{
Expand All @@ -161,12 +161,12 @@ internal object? ExclusiveMaximumRaw
[JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object? ExclusiveMinimumRaw
{
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object)true : null);
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object) true : null);
set
{
if (value is bool)
{
IsExclusiveMinimum = (bool)value;
IsExclusiveMinimum = (bool) value;
}
else if (value != null && (value.Equals("true") || value.Equals("false")))
{
Expand Down Expand Up @@ -200,7 +200,7 @@ internal object? AdditionalItemsRaw
{
if (value is bool)
{
AllowAdditionalItems = (bool)value;
AllowAdditionalItems = (bool) value;
}
else if (value != null && (value.Equals("true") || value.Equals("false")))
{
Expand Down Expand Up @@ -254,7 +254,7 @@ internal object? AdditionalPropertiesRaw
{
if (value is bool)
{
AllowAdditionalProperties = (bool)value;
AllowAdditionalProperties = (bool) value;
}
else if (value != null && (value.Equals("true") || value.Equals("false")))
{
Expand Down Expand Up @@ -288,7 +288,7 @@ internal object? ItemsRaw
{
if (value is JArray)
{
Items = new ObservableCollection<JsonSchema>(((JArray)value).Select(FromJsonWithCurrentSettings));
Items = new ObservableCollection<JsonSchema>(((JArray) value).Select(FromJsonWithCurrentSettings));
}
else if (value != null)
{
Expand All @@ -315,7 +315,7 @@ internal object? TypeRaw
{
if (value is JArray)
{
Type = ((JArray)value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()));
Type = ((JArray) value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()));
}
else
{
Expand Down Expand Up @@ -367,8 +367,8 @@ internal IDictionary<string, JsonSchemaProperty>? PropertiesRaw
internal IDictionary<string, JsonSchemaProperty>? PatternPropertiesRaw
{
get => _patternProperties is { Count: > 0 }
? PatternProperties.ToDictionary(p => p.Key, p => p.Value)
: null;
? PatternProperties.ToDictionary(p => p.Key, p => p.Value)
: null;
set => PatternProperties = value != null ? new ObservableDictionary<string, JsonSchemaProperty>(value!) : [];
}

Expand Down Expand Up @@ -460,35 +460,46 @@ private void RegisterSchemaCollection(ObservableCollection<JsonSchema>? oldColle

private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedEventArgs? args)
{
if (sender is ObservableDictionary<string, JsonSchemaProperty> properties)
if (sender is ObservableDictionary<string, JsonSchemaProperty> { Count: > 0 } properties)
{
foreach (var property in properties)
{
property.Value!.Name = property.Key;
property.Value.Parent = this;
}
}
else if (sender is ObservableCollection<JsonSchema> items)
else if (sender is ObservableCollection<JsonSchema> { Count: > 0 } items)
{
foreach (var item in items)
{
item.Parent = this;
}
}
else if (sender is ObservableDictionary<string, JsonSchema> collection)
else if (sender is ObservableDictionary<string, JsonSchema> { Count: > 0 } collection)
{
foreach (var pair in collection.ToArray())
List<string>? keysToRemove = null;
foreach (var pair in collection)
{
if (pair.Value == null)
{
collection.Remove(pair.Key);
keysToRemove ??= [];
keysToRemove.Add(pair.Key);
}
else
{
pair.Value.Parent = this;
}
}

if (keysToRemove != null)
{
for (var i = 0; i < keysToRemove.Count; i++)
{
var key = keysToRemove[i];
collection.Remove(key);
}
}
}
}
}
}
}

0 comments on commit 55fd77e

Please sign in to comment.