Skip to content

Commit 41b3b34

Browse files
committed
Use Lazy instead of lock
1 parent 362abfe commit 41b3b34

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +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();
27+
private readonly Lazy<RazorHtmlDocument> _lazyHtmlDocument;
2828

2929
private RazorCodeDocument(
3030
RazorSourceDocument source,
@@ -40,6 +40,7 @@ private RazorCodeDocument(
4040
CodeGenerationOptions = codeGenerationOptions ?? RazorCodeGenerationOptions.Default;
4141

4242
_properties = properties ?? new();
43+
_lazyHtmlDocument = new(() => CreateHtmlDocument());
4344
}
4445

4546
public static RazorCodeDocument Create(
@@ -177,20 +178,17 @@ internal void SetCSharpDocument(RazorCSharpDocument value)
177178
}
178179

179180
internal RazorHtmlDocument GetHtmlDocument()
181+
=> _lazyHtmlDocument.Value;
182+
183+
private RazorHtmlDocument CreateHtmlDocument()
180184
{
181185
if (_properties.HtmlDocument.TryGetValue(out var result))
182186
{
183187
return result;
184188
}
185189

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

195193
return result;
196194
}

0 commit comments

Comments
 (0)