Skip to content

Commit 55fd77e

Browse files
authored
Improve JsonSchema.InitializeSchemaCollection performance (#1760)
* don't enumerate if no data * removal is rare so don't create separate traversing array
1 parent c740804 commit 55fd77e

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/NJsonSchema/JsonSchema.Serialization.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ internal object? DiscriminatorRaw
122122
{
123123
if (value is string)
124124
{
125-
Discriminator = (string)value;
125+
Discriminator = (string) value;
126126
}
127127
else if (value != null)
128128
{
129-
DiscriminatorObject = ((JObject)value).ToObject<OpenApiDiscriminator>();
129+
DiscriminatorObject = ((JObject) value).ToObject<OpenApiDiscriminator>();
130130
}
131131
}
132132
}
@@ -139,12 +139,12 @@ internal object? DiscriminatorRaw
139139
[JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
140140
internal object? ExclusiveMaximumRaw
141141
{
142-
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object)true : null);
142+
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object) true : null);
143143
set
144144
{
145145
if (value is bool)
146146
{
147-
IsExclusiveMaximum = (bool)value;
147+
IsExclusiveMaximum = (bool) value;
148148
}
149149
else if (value != null && (value.Equals("true") || value.Equals("false")))
150150
{
@@ -161,12 +161,12 @@ internal object? ExclusiveMaximumRaw
161161
[JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
162162
internal object? ExclusiveMinimumRaw
163163
{
164-
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object)true : null);
164+
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object) true : null);
165165
set
166166
{
167167
if (value is bool)
168168
{
169-
IsExclusiveMinimum = (bool)value;
169+
IsExclusiveMinimum = (bool) value;
170170
}
171171
else if (value != null && (value.Equals("true") || value.Equals("false")))
172172
{
@@ -200,7 +200,7 @@ internal object? AdditionalItemsRaw
200200
{
201201
if (value is bool)
202202
{
203-
AllowAdditionalItems = (bool)value;
203+
AllowAdditionalItems = (bool) value;
204204
}
205205
else if (value != null && (value.Equals("true") || value.Equals("false")))
206206
{
@@ -254,7 +254,7 @@ internal object? AdditionalPropertiesRaw
254254
{
255255
if (value is bool)
256256
{
257-
AllowAdditionalProperties = (bool)value;
257+
AllowAdditionalProperties = (bool) value;
258258
}
259259
else if (value != null && (value.Equals("true") || value.Equals("false")))
260260
{
@@ -288,7 +288,7 @@ internal object? ItemsRaw
288288
{
289289
if (value is JArray)
290290
{
291-
Items = new ObservableCollection<JsonSchema>(((JArray)value).Select(FromJsonWithCurrentSettings));
291+
Items = new ObservableCollection<JsonSchema>(((JArray) value).Select(FromJsonWithCurrentSettings));
292292
}
293293
else if (value != null)
294294
{
@@ -315,7 +315,7 @@ internal object? TypeRaw
315315
{
316316
if (value is JArray)
317317
{
318-
Type = ((JArray)value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()));
318+
Type = ((JArray) value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()));
319319
}
320320
else
321321
{
@@ -367,8 +367,8 @@ internal IDictionary<string, JsonSchemaProperty>? PropertiesRaw
367367
internal IDictionary<string, JsonSchemaProperty>? PatternPropertiesRaw
368368
{
369369
get => _patternProperties is { Count: > 0 }
370-
? PatternProperties.ToDictionary(p => p.Key, p => p.Value)
371-
: null;
370+
? PatternProperties.ToDictionary(p => p.Key, p => p.Value)
371+
: null;
372372
set => PatternProperties = value != null ? new ObservableDictionary<string, JsonSchemaProperty>(value!) : [];
373373
}
374374

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

461461
private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedEventArgs? args)
462462
{
463-
if (sender is ObservableDictionary<string, JsonSchemaProperty> properties)
463+
if (sender is ObservableDictionary<string, JsonSchemaProperty> { Count: > 0 } properties)
464464
{
465465
foreach (var property in properties)
466466
{
467467
property.Value!.Name = property.Key;
468468
property.Value.Parent = this;
469469
}
470470
}
471-
else if (sender is ObservableCollection<JsonSchema> items)
471+
else if (sender is ObservableCollection<JsonSchema> { Count: > 0 } items)
472472
{
473473
foreach (var item in items)
474474
{
475475
item.Parent = this;
476476
}
477477
}
478-
else if (sender is ObservableDictionary<string, JsonSchema> collection)
478+
else if (sender is ObservableDictionary<string, JsonSchema> { Count: > 0 } collection)
479479
{
480-
foreach (var pair in collection.ToArray())
480+
List<string>? keysToRemove = null;
481+
foreach (var pair in collection)
481482
{
482483
if (pair.Value == null)
483484
{
484-
collection.Remove(pair.Key);
485+
keysToRemove ??= [];
486+
keysToRemove.Add(pair.Key);
485487
}
486488
else
487489
{
488490
pair.Value.Parent = this;
489491
}
490492
}
493+
494+
if (keysToRemove != null)
495+
{
496+
for (var i = 0; i < keysToRemove.Count; i++)
497+
{
498+
var key = keysToRemove[i];
499+
collection.Remove(key);
500+
}
501+
}
491502
}
492503
}
493504
}
494-
}
505+
}

0 commit comments

Comments
 (0)