In the code below I am mapping data that I got by paralelly downloading objects from AzureDevOps Rest Api. I noticed some weird bottlenecks in my code when transforming the mapDictionary to to an excel sheet in different module of the codebase. It took forever to make a simple foreach over thousand objects because the references to the mapDictionary are lazy loaded.
private Dictionary<int, IEnumerable<CodeReviewResponse>>MapToCodeReviewRequests
(IEnumerable<WorkItemBatchResponse>workItemBatchResponses,
WiqlResponse? workItemRelations)
{
Dictionary<int, IEnumerable<CodeReviewResponse>> mapDictionary = new Dictionary<int, IEnumerable<CodeReviewResponse>>();
IEnumerable<int>? codeReviewRequestIds = workItemRelations?.workItemRelations.Where(y => y.source is not null)
.Select(x => x.source.id)
.Distinct();
foreach(int codeReviewId in codeReviewRequestIds)
{
var codeReviewResponseIds = workItemRelations?.workItemRelations
.Where(x => x.source is not null && x.source.id.Equals(codeReviewId))
.Select(x => x.target.id).ToList();
if (codeReviewResponseIds is not null && codeReviewResponseIds.Count() > 0)
{
mapDictionary[codeReviewId] = workItemBatchResponses.SelectMany(x => x.value)
.Where(x => codeReviewResponseIds.Contains(x.fields.SystemId));
}
}
return mapDictionary;
}
If codeReviewResponseIds is a List of ints (as it is in the example) then my data are not lazy loaded and my iteration over those references are fast, if I store it without the .ToList() then it takes forever to compute them, why?