Skip to content

Commit

Permalink
Merge pull request #220 from Lombiq/issue/NEST-462
Browse files Browse the repository at this point in the history
NEST-462: Improving memory usage
  • Loading branch information
DemeSzabolcs authored Sep 30, 2023
2 parents c3b7320 + a634d46 commit 52d067a
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,24 @@ public async Task<ShapeTable> GetShapeTableAsync(string themeId)
enabledAndOrderedFeatureIds.Add(_hostingEnvironment.ApplicationName);
}

// Create a Dictionary for faster lookup operations.
var featureIdIndexLookup = enabledAndOrderedFeatureIds
.Select((id, index) => new { id, index })
.ToDictionary(item => item.id, item => item.index);

var concurrentShapeDescriptors = new ConcurrentDictionary<string, FeatureShapeDescriptor>(shapeDescriptors);

var descriptors = shapeDescriptors
.Where(sd => enabledAndOrderedFeatureIds.Contains(sd.Value.Feature.Id))
.Where(sd => IsModuleOrRequestedTheme(sd.Value.Feature, themeId))
.OrderBy(sd => enabledAndOrderedFeatureIds.IndexOf(sd.Value.Feature.Id))
.GroupBy(sd => sd.Value.ShapeType, StringComparer.OrdinalIgnoreCase)
// Filtering using the dictionary for O(1) lookup instead of O(n) in a list.
.Where(shapeDescriptor => featureIdIndexLookup.ContainsKey(shapeDescriptor.Value.Feature.Id &&
IsModuleOrRequestedTheme(shapeDescriptor.Value.Feature, themeId))
// Using the dictionary for O(1) index retrieval instead of O(n) in a list.
.OrderBy(shapeDescriptor => featureIdIndexLookup[shapeDescriptor.Value.Feature.Id])
.GroupBy(shapeDescriptor => shapeDescriptor.Value.ShapeType, StringComparer.OrdinalIgnoreCase)
.Select(group => new ShapeDescriptorIndex(
shapeType: group.Key,
alterationKeys: group.Select(kv => kv.Key),
descriptors: new ConcurrentDictionary<string, FeatureShapeDescriptor>(shapeDescriptors)
descriptors: concurrentShapeDescriptors
))
.ToList();

Expand Down

0 comments on commit 52d067a

Please sign in to comment.