From ce967cbed6b93d9e3ea7f95c22b6eae84ca7ef2d Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 29 Jun 2021 10:27:28 -0400 Subject: [PATCH] Stop allocating new delegates in WriterDelegate.Dispose Every call to Dispose is allocating one or two delegates. They're not unique to this instance; we can let the compiler cache them for us. --- .../System/Xaml/Primitives/WriterDelegate.cs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Primitives/WriterDelegate.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Primitives/WriterDelegate.cs index 3425ec6640a..9c4f091cc57 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Primitives/WriterDelegate.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Primitives/WriterDelegate.cs @@ -72,10 +72,11 @@ protected override void Dispose(bool disposing) if (disposing && !IsDisposed) { _addDelegate(XamlNodeType.None, XamlNode.InternalNodeType.EndOfStream); - _addDelegate = new XamlNodeAddDelegate(ThrowBecauseWriterIsClosed); - _addLineInfoDelegate = (_addLineInfoDelegate != null) - ? new XamlLineInfoAddDelegate(ThrowBecauseWriterIsClosed2) - : null; + _addDelegate = delegate { throw new XamlException(SR.Get(SRID.WriterIsClosed)); }; + if (_addLineInfoDelegate != null) + { + _addLineInfoDelegate = delegate { throw new XamlException(SR.Get(SRID.WriterIsClosed)); }; + } } } finally @@ -111,17 +112,6 @@ public bool ShouldProvideLineInfo } #endregion - - private void ThrowBecauseWriterIsClosed(XamlNodeType nodeType, object data) - { - throw new XamlException(SR.Get(SRID.WriterIsClosed)); - } - - private void ThrowBecauseWriterIsClosed2(int lineNumber, int linePosition) - { - throw new XamlException(SR.Get(SRID.WriterIsClosed)); - } - private void ThrowIsDisposed() { if (IsDisposed)