Skip to content

Commit 09069a3

Browse files
committed
Improve performance by skip additional loops
Previous each schema class would need to look at every other schema class. Now that processing is done once and then checked again via a HashSet. Similarly with properties, each property was iterated for every class - now that happens once with each class only looping over its properties.
1 parent 530bf38 commit 09069a3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Tools/Schema.NET.Tool/Services/SchemaService.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public async Task<IEnumerable<GeneratorSchemaObject>> GetObjectsAsync()
4040
schemaClasses.Where(c => c.IsEnum).Select(c => c.Label),
4141
StringComparer.Ordinal);
4242

43+
var knownSchemaClasses = new HashSet<Uri>(
44+
schemaClasses.Select(c => c.Id));
45+
46+
var classPropertyMap = schemaProperties
47+
.SelectMany(p => p.DomainIncludes.Select(i => (Id: i, Property: p)))
48+
.GroupBy(x => x.Id)
49+
.ToDictionary(g => g.Key, g => g.Select(p => p.Property).ToArray());
50+
4351
var enumerations = new List<GeneratorSchemaEnumeration>();
4452
var classes = new List<GeneratorSchemaClass>();
4553
foreach (var schemaClass in schemaClasses
@@ -52,7 +60,12 @@ public async Task<IEnumerable<GeneratorSchemaObject>> GetObjectsAsync()
5260
}
5361
else
5462
{
55-
var @class = TranslateClass(schemaClass, schemaClasses, schemaProperties, isEnumMap);
63+
if (!classPropertyMap.TryGetValue(schemaClass.Id, out var classProperties))
64+
{
65+
classProperties = Array.Empty<Models.SchemaProperty>();
66+
}
67+
68+
var @class = TranslateClass(schemaClass, schemaClasses, classProperties, isEnumMap, knownSchemaClasses);
5669
classes.Add(@class);
5770
}
5871
}
@@ -222,7 +235,8 @@ private static GeneratorSchemaClass TranslateClass(
222235
Models.SchemaClass schemaClass,
223236
IEnumerable<Models.SchemaClass> schemaClasses,
224237
IEnumerable<Models.SchemaProperty> schemaProperties,
225-
HashSet<string> isEnumMap)
238+
HashSet<string> isEnumMap,
239+
HashSet<Uri> knownSchemaClasses)
226240
{
227241
var @class = new GeneratorSchemaClass()
228242
{
@@ -232,13 +246,11 @@ private static GeneratorSchemaClass TranslateClass(
232246
Name = StartsWithNumber.IsMatch(schemaClass.Label) ? $"_{schemaClass.Label}" : schemaClass.Label,
233247
};
234248

235-
@class.Parents.AddRange(schemaClasses
236-
.Where(x => schemaClass.SubClassOfIds.Contains(x.Id))
237-
.Where(x => !x.IsPending)
238-
.Select(x => new GeneratorSchemaClass() { Id = x.Id }));
249+
@class.Parents.AddRange(schemaClass.SubClassOfIds
250+
.Where(id => knownSchemaClasses.Contains(id))
251+
.Select(id => new GeneratorSchemaClass() { Id = id }));
239252

240253
var properties = schemaProperties
241-
.Where(x => x.DomainIncludes.Contains(schemaClass.Id))
242254
.Select(x =>
243255
{
244256
var propertyName = GetPropertyName(x.Label);

0 commit comments

Comments
 (0)