Skip to content

Commit e0f3773

Browse files
committed
feat: use lazy get for collection initialization to reduce resource allocation
1 parent 5d99cc4 commit e0f3773

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,64 @@ public decimal? Minimum
172172
/// <inheritdoc />
173173
public bool WriteOnly { get; set; }
174174

175+
private IList<IOpenApiSchema>? _allOf;
175176
/// <inheritdoc />
176-
public IList<IOpenApiSchema>? AllOf { get; set; } = [];
177+
public IList<IOpenApiSchema>? AllOf
178+
{
179+
get => _allOf ??= [];
180+
set => _allOf = value;
181+
}
182+
183+
private IList<IOpenApiSchema>? _oneOf;
184+
private bool _isExplicitlyNull;
177185

178186
/// <inheritdoc />
179-
public IList<IOpenApiSchema>? OneOf { get; set; } = [];
187+
public IList<IOpenApiSchema>? OneOf
188+
{
189+
get
190+
{
191+
if (_isExplicitlyNull)
192+
{
193+
return null;
194+
}
195+
return _oneOf ??= [];
196+
}
197+
set
198+
{
199+
_oneOf = value;
200+
_isExplicitlyNull = value is null;
201+
}
202+
}
180203

204+
private IList<IOpenApiSchema>? _anyOf;
181205
/// <inheritdoc />
182-
public IList<IOpenApiSchema>? AnyOf { get; set; } = [];
206+
public IList<IOpenApiSchema>? AnyOf
207+
{
208+
get
209+
{
210+
if (_isExplicitlyNull)
211+
{
212+
return null;
213+
}
214+
return _anyOf ??= [];
215+
}
216+
set
217+
{
218+
_anyOf = value;
219+
_isExplicitlyNull = value is null;
220+
}
221+
}
183222

184223
/// <inheritdoc />
185224
public IOpenApiSchema? Not { get; set; }
186225

226+
private ISet<string>? _required;
187227
/// <inheritdoc />
188-
public ISet<string>? Required { get; set; } = new HashSet<string>();
228+
public ISet<string>? Required
229+
{
230+
get => _required ??= new HashSet<string>();
231+
set => _required = value;
232+
}
189233

190234
/// <inheritdoc />
191235
public IOpenApiSchema? Items { get; set; }
@@ -199,11 +243,21 @@ public decimal? Minimum
199243
/// <inheritdoc />
200244
public bool? UniqueItems { get; set; }
201245

246+
private IDictionary<string, IOpenApiSchema>? _properties;
202247
/// <inheritdoc />
203-
public IDictionary<string, IOpenApiSchema>? Properties { get; set; } = new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal);
248+
public IDictionary<string, IOpenApiSchema>? Properties
249+
{
250+
get => _properties ??= new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal);
251+
set => _properties = value;
252+
}
204253

254+
private IDictionary<string, IOpenApiSchema>? _patternProperties;
205255
/// <inheritdoc />
206-
public IDictionary<string, IOpenApiSchema>? PatternProperties { get; set; } = new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal);
256+
public IDictionary<string, IOpenApiSchema>? PatternProperties
257+
{
258+
get => _patternProperties ??= new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal);
259+
set => _patternProperties = value;
260+
}
207261

208262
/// <inheritdoc />
209263
public int? MaxProperties { get; set; }
@@ -226,8 +280,13 @@ public decimal? Minimum
226280
/// <inheritdoc />
227281
public IList<JsonNode>? Examples { get; set; }
228282

283+
private IList<JsonNode>? _enum;
229284
/// <inheritdoc />
230-
public IList<JsonNode>? Enum { get; set; } = new List<JsonNode>();
285+
public IList<JsonNode>? Enum
286+
{
287+
get => _enum ??= new List<JsonNode>();
288+
set => _enum = value;
289+
}
231290

232291
/// <inheritdoc />
233292
public bool UnevaluatedProperties { get; set;}
@@ -241,17 +300,32 @@ public decimal? Minimum
241300
/// <inheritdoc />
242301
public OpenApiXml? Xml { get; set; }
243302

303+
private IDictionary<string, IOpenApiExtension>? _extensions;
244304
/// <inheritdoc />
245-
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
305+
public IDictionary<string, IOpenApiExtension>? Extensions
306+
{
307+
get => _extensions ??= new Dictionary<string, IOpenApiExtension>(StringComparer.Ordinal);
308+
set => _extensions = value;
309+
}
246310

311+
private IDictionary<string, JsonNode>? _unrecognizedKeywords;
247312
/// <inheritdoc />
248-
public IDictionary<string, JsonNode>? UnrecognizedKeywords { get; set; } = new Dictionary<string, JsonNode>();
313+
public IDictionary<string, JsonNode>? UnrecognizedKeywords
314+
{
315+
get => _unrecognizedKeywords ??= new Dictionary<string, JsonNode>(StringComparer.Ordinal);
316+
set => _unrecognizedKeywords = value;
317+
}
249318

250319
/// <inheritdoc />
251320
public IDictionary<string, object>? Annotations { get; set; }
252321

322+
private IDictionary<string, ISet<string>>? _dependentRequired;
253323
/// <inheritdoc />
254-
public IDictionary<string, ISet<string>>? DependentRequired { get; set; } = new Dictionary<string, ISet<string>>();
324+
public IDictionary<string, ISet<string>>? DependentRequired
325+
{
326+
get => _dependentRequired ??= new Dictionary<string, ISet<string>>(StringComparer.Ordinal);
327+
set => _dependentRequired = value;
328+
}
255329

256330
/// <summary>
257331
/// Parameterless constructor

0 commit comments

Comments
 (0)