Skip to content

Commit 362abfe

Browse files
committed
Avoid concurrent generation of the html document within a razor code document.
I see about 40% of edits 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
1 parent 08529a5 commit 362abfe

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 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,14 @@ internal RazorHtmlDocument GetHtmlDocument()
182183
return result;
183184
}
184185

185-
result = RazorHtmlWriter.GetHtmlDocument(this);
186-
_properties.HtmlDocument.SetValue(result);
186+
lock (_htmlDocumentLock)
187+
{
188+
if (!_properties.HtmlDocument.TryGetValue(out result))
189+
{
190+
result = RazorHtmlWriter.GetHtmlDocument(this);
191+
_properties.HtmlDocument.SetValue(result);
192+
}
193+
}
187194

188195
return result;
189196
}

0 commit comments

Comments
 (0)