Skip to content

Commit 58de0c1

Browse files
authored
Avoid concurrent generation of the html document within a razor code document. (#11986)
I see about 40% of edits within an html scope generate the html document multiple times, primarily due to concurrent RazorCodeCode.GetHtmlDocument requests from the callstacks below (measured on release bits without a debugger attached). As these requests are on the same code document, they can be collapsed into a single request. *** callstack 1 *** RazorCodeDocument.GetHtmlDocument RazorCodeDocumentExtensions.GetHtmlSourceText GeneratedDocumentSynchronizer.DocumentProcessed OpenDocumentGenerator.ProcessBatchAsync *** callstack 2 *** RazorCodeDocument.GetHtmlDocument IDocumentMappingServiceExtensions.GetPositionInfo DelegatedCompletionListProvider.GetCompletionListAsync CompletionListProvider.GetCompletionListCoreAsync
2 parents 3bf6ae4 + 5d49e58 commit 58de0c1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCodeDocument.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed partial class RazorCodeDocument
2424
public RazorFileKind FileKind => ParserOptions.FileKind;
2525

2626
private readonly PropertyTable _properties = new();
27+
private readonly object _htmlDocumentLock = new();
2728

2829
private RazorCodeDocument(
2930
RazorSourceDocument source,
@@ -182,8 +183,15 @@ internal RazorHtmlDocument GetHtmlDocument()
182183
return result;
183184
}
184185

185-
result = RazorHtmlWriter.GetHtmlDocument(this);
186-
_properties.HtmlDocument.SetValue(result);
186+
// Perf: Avoid concurrent requests generating the same html document
187+
lock (_htmlDocumentLock)
188+
{
189+
if (!_properties.HtmlDocument.TryGetValue(out result))
190+
{
191+
result = RazorHtmlWriter.GetHtmlDocument(this);
192+
_properties.HtmlDocument.SetValue(result);
193+
}
194+
}
187195

188196
return result;
189197
}

0 commit comments

Comments
 (0)