Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/EntityFramework/Core/Mapping/ViewGeneration/ViewGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ private ErrorLog GenerateDirectionalViews(ViewTarget viewTarget, CqlIdentifiers
// Generate views for each extent in parallel
var errorBag = new ConcurrentBag<ErrorLog>();
var viewBag = new ConcurrentBag<KeyValuePair<EntitySetBase, GeneratedView>>();
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };

Parallel.ForEach(extents, extent =>
Parallel.ForEach(extents, parallelOptions, extent =>
{
// Each thread uses its own local ViewSet to avoid contention
var localViews = new ViewSet(EqualityComparer<EntitySetBase>.Default);
Expand Down Expand Up @@ -337,12 +338,13 @@ private ErrorLog GenerateDirectionalViews(ViewTarget viewTarget, CqlIdentifiers
});

// Merge results on the calling thread (single-threaded, safe)
// Sort by EntitySetBase name for deterministic output ordering
var errorLog = new ErrorLog();
foreach (var log in errorBag)
{
errorLog.Merge(log);
}
foreach (var kvp in viewBag)
foreach (var kvp in viewBag.OrderBy(v => v.Key.Name, StringComparer.Ordinal))
{
views.Add(kvp.Key, kvp.Value);
}
Expand Down Expand Up @@ -407,6 +409,16 @@ private ErrorLog GenerateDirectionalViewsSequential(
errorLog.Merge(exception.ErrorLog);
}
}

if (isQueryView)
{
m_config.SetTimeForFinishedActivity(PerfType.QueryViews);
}
else
{
m_config.SetTimeForFinishedActivity(PerfType.UpdateViews);
}

return errorLog;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ private static ViewGenResults GenerateViewsFromCells(
// so they can be processed concurrently with thread-local state.
var errorBag = new ConcurrentBag<ErrorLog>();
var viewBag = new ConcurrentBag<KeyValuePair<EntitySetBase, GeneratedView>>();
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };

Parallel.ForEach(cellGroups, cellGroup =>
Parallel.ForEach(cellGroups, parallelOptions, cellGroup =>
{
// Each thread gets its own config copy (Stopwatch is not thread-safe)
var localConfig = config.CreateCopy();
Expand Down Expand Up @@ -254,11 +255,12 @@ private static ViewGenResults GenerateViewsFromCells(
});

// Merge results on the calling thread (single-threaded, safe)
// Sort by EntitySetBase name for deterministic output ordering
foreach (var log in errorBag)
{
viewGenResults.AddErrors(log);
}
foreach (var kvp in viewBag)
foreach (var kvp in viewBag.OrderBy(v => v.Key.Name, StringComparer.Ordinal))
{
viewGenResults.Views.Add(kvp.Key, kvp.Value);
}
Expand Down