From 627168dac20c46b8fc2baaa0a3019dc5a37513af Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Tue, 7 Nov 2017 08:31:35 -0800 Subject: [PATCH] Address PR feedback and use singleton for null instance --- .../Portable/Operations/CSharpOperationFactory.cs | 11 +++++++---- .../Operations/VisualBasicOperationFactory.vb | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs b/src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs index d9b23a6f84a..da70a853b25 100644 --- a/src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs +++ b/src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs @@ -18,6 +18,9 @@ internal sealed partial class CSharpOperationFactory private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(concurrencyLevel: 2, capacity: 10); + private readonly Lazy _lazyNullOperation = + new Lazy(() => null); + private readonly SemanticModel _semanticModel; public CSharpOperationFactory(SemanticModel semanticModel) @@ -479,7 +482,7 @@ private IAnonymousObjectCreationOperation CreateBoundAnonymousObjectCreationExpr private IPropertyReferenceOperation CreateBoundAnonymousPropertyDeclarationOperation(BoundAnonymousPropertyDeclaration boundAnonymousPropertyDeclaration) { PropertySymbol property = boundAnonymousPropertyDeclaration.Property; - Lazy instance = new Lazy(() => null); + Lazy instance = _lazyNullOperation; Lazy> arguments = new Lazy>(() => ImmutableArray.Empty); SyntaxNode syntax = boundAnonymousPropertyDeclaration.Syntax; ITypeSymbol type = boundAnonymousPropertyDeclaration.Type; @@ -1309,7 +1312,7 @@ private IWhileLoopOperation CreateBoundWhileStatementOperation(BoundWhileStateme { Lazy condition = new Lazy(() => Create(boundWhileStatement.Condition)); Lazy body = new Lazy(() => Create(boundWhileStatement.Body)); - Lazy ignoredCondition = new Lazy(() => null); + Lazy ignoredCondition = _lazyNullOperation; ImmutableArray locals = boundWhileStatement.Locals.As(); bool conditionIsTop = true; bool conditionIsUntil = false; @@ -1324,7 +1327,7 @@ private IWhileLoopOperation CreateBoundDoStatementOperation(BoundDoStatement bou { Lazy condition = new Lazy(() => Create(boundDoStatement.Condition)); Lazy body = new Lazy(() => Create(boundDoStatement.Body)); - Lazy ignoredCondition = new Lazy(() => null); + Lazy ignoredCondition = _lazyNullOperation; bool conditionIsTop = false; bool conditionIsUntil = false; ImmutableArray locals = boundDoStatement.Locals.As(); @@ -1366,7 +1369,7 @@ private IForEachLoopOperation CreateBoundForEachStatementOperation(BoundForEachS } else { - loopControlVariable = new Lazy(() => null); + loopControlVariable = _lazyNullOperation; } Lazy collection = new Lazy(() => Create(boundForEachStatement.Expression)); diff --git a/src/Compilers/VisualBasic/Portable/Operations/VisualBasicOperationFactory.vb b/src/Compilers/VisualBasic/Portable/Operations/VisualBasicOperationFactory.vb index 93499115847..7730f9a8516 100644 --- a/src/Compilers/VisualBasic/Portable/Operations/VisualBasicOperationFactory.vb +++ b/src/Compilers/VisualBasic/Portable/Operations/VisualBasicOperationFactory.vb @@ -13,6 +13,9 @@ Namespace Microsoft.CodeAnalysis.Operations Private ReadOnly _cache As ConcurrentDictionary(Of BoundNode, IOperation) = New ConcurrentDictionary(Of BoundNode, IOperation)(concurrencyLevel:=2, capacity:=10) + Private ReadOnly _lazyNothingOperation As Lazy(Of IOperation) = + New Lazy(Of IOperation)(Function() Nothing) + Private ReadOnly _semanticModel As SemanticModel Public Sub New(semanticModel As SemanticModel) @@ -1215,7 +1218,7 @@ Namespace Microsoft.CodeAnalysis.Operations Private Function CreateBoundWhileStatementOperation(boundWhileStatement As BoundWhileStatement) As IWhileLoopOperation Dim condition As Lazy(Of IOperation) = New Lazy(Of IOperation)(Function() Create(boundWhileStatement.Condition)) Dim body As Lazy(Of IOperation) = New Lazy(Of IOperation)(Function() Create(boundWhileStatement.Body)) - Dim ignoredCondition As Lazy(Of IOperation) = New Lazy(Of IOperation)(Function() Nothing) + Dim ignoredCondition As Lazy(Of IOperation) = _lazyNothingOperation Dim locals As ImmutableArray(Of ILocalSymbol) = ImmutableArray(Of ILocalSymbol).Empty Dim conditionIsTop As Boolean = True Dim conditionIsUntil As Boolean = False @@ -1246,7 +1249,7 @@ Namespace Microsoft.CodeAnalysis.Operations Private Function CreateBoundLabelStatementOperation(boundLabelStatement As BoundLabelStatement) As ILabeledOperation Dim label As ILabelSymbol = boundLabelStatement.Label - Dim statement As Lazy(Of IOperation) = New Lazy(Of IOperation)(Function() Nothing) + Dim statement As Lazy(Of IOperation) = _lazyNothingOperation Dim syntax As SyntaxNode = boundLabelStatement.Syntax Dim type As ITypeSymbol = Nothing Dim constantValue As [Optional](Of Object) = New [Optional](Of Object)() @@ -1493,7 +1496,7 @@ Namespace Microsoft.CodeAnalysis.Operations End Function Private Function CreateBoundAnonymousTypePropertyAccessOperation(boundAnonymousTypePropertyAccess As BoundAnonymousTypePropertyAccess) As IPropertyReferenceOperation - Dim instance As Lazy(Of IOperation) = New Lazy(Of IOperation)(Function() Nothing) + Dim instance As Lazy(Of IOperation) = _lazyNothingOperation Dim [property] As IPropertySymbol = DirectCast(boundAnonymousTypePropertyAccess.ExpressionSymbol, IPropertySymbol) Dim arguments As Lazy(Of ImmutableArray(Of IArgumentOperation)) = New Lazy(Of ImmutableArray(Of IArgumentOperation))(Function() ImmutableArray(Of IArgumentOperation).Empty) Dim syntax As SyntaxNode = boundAnonymousTypePropertyAccess.Syntax -- GitLab