Skip to content

Commit 15e15b1

Browse files
committed
increase performance by half by using static constructor approach for initialization
1 parent f5c099b commit 15e15b1

File tree

1 file changed

+38
-50
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows

1 file changed

+38
-50
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataFormats.cs

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Security;
1818
using System.Text;
1919
using MS.Internal.PresentationCore;
20+
using System.Windows.Ink;
2021
using SecurityHelper = MS.Internal.SecurityHelper;
2122

2223
namespace System.Windows
@@ -52,9 +53,6 @@ public static DataFormat GetDataFormat(string format)
5253
if (format == string.Empty)
5354
throw new ArgumentException(SR.DataObject_EmptyFormatNotAllowed);
5455

55-
// Ensures the predefined Win32 data formats into our format list.
56-
EnsurePredefined();
57-
5856
// Lock the data format list to obtain the mutual-exclusion.
5957
lock (_formatListlock)
6058
{
@@ -300,9 +298,6 @@ public static DataFormat GetDataFormat(string format)
300298
/// </summary>
301299
private static DataFormat InternalGetDataFormat(int id)
302300
{
303-
// Ensures the predefined Win32 data formats into our format list.
304-
EnsurePredefined();
305-
306301
// Lock the data format list to obtain the mutual-exclusion.
307302
lock (_formatListlock)
308303
{
@@ -341,51 +336,44 @@ private static DataFormat InternalGetDataFormat(int id)
341336
/// Ensures that the Win32 predefined formats are setup in our format list. This
342337
/// is called anytime we need to search the list
343338
/// </summary>
344-
private static void EnsurePredefined()
339+
static DataFormats()
345340
{
346-
// Lock the data format list to obtain the mutual-exclusion.
347-
lock (_formatListlock)
341+
// Create format list for the default formats.
342+
_formatList = new List<DataFormat>(19)
348343
{
349-
if (_formatList is not null)
350-
return;
351-
352-
// Create format list for the default formats.
353-
_formatList = new List<DataFormat>(19)
354-
{
355-
new(UnicodeText, NativeMethods.CF_UNICODETEXT),
356-
new(Text, NativeMethods.CF_TEXT),
357-
new(Bitmap, NativeMethods.CF_BITMAP),
358-
new(MetafilePicture, NativeMethods.CF_METAFILEPICT),
359-
new(EnhancedMetafile, NativeMethods.CF_ENHMETAFILE),
360-
new(Dif, NativeMethods.CF_DIF),
361-
new(Tiff, NativeMethods.CF_TIFF),
362-
new(OemText, NativeMethods.CF_OEMTEXT),
363-
new(Dib, NativeMethods.CF_DIB),
364-
new(Palette, NativeMethods.CF_PALETTE),
365-
new(PenData, NativeMethods.CF_PENDATA),
366-
new(Riff, NativeMethods.CF_RIFF),
367-
new(WaveAudio, NativeMethods.CF_WAVE),
368-
new(SymbolicLink, NativeMethods.CF_SYLK),
369-
new(FileDrop, NativeMethods.CF_HDROP),
370-
new(Locale, NativeMethods.CF_LOCALE)
371-
};
372-
373-
int xamlFormatId = UnsafeNativeMethods.RegisterClipboardFormat(Xaml);
374-
if (xamlFormatId != 0)
375-
_formatList.Add(new(Xaml, xamlFormatId));
376-
377-
// This is the format to store trust boundary information. Essentially this is accompalished by storing
378-
// the permission set of the source application where the content comes from. During paste we compare this to
379-
// the permission set of the target application.
380-
int applicationTrustFormatId = UnsafeNativeMethods.RegisterClipboardFormat(DataFormats.ApplicationTrust);
381-
if (applicationTrustFormatId != 0)
382-
_formatList.Add(new(ApplicationTrust, applicationTrustFormatId));
383-
384-
// RegisterClipboardFormat returns 0 on failure
385-
int inkServicesFrameworkFormatId = UnsafeNativeMethods.RegisterClipboardFormat(Ink.StrokeCollection.InkSerializedFormat);
386-
if (inkServicesFrameworkFormatId != 0)
387-
_formatList.Add(new(Ink.StrokeCollection.InkSerializedFormat, inkServicesFrameworkFormatId));
388-
}
344+
new(DataFormats.UnicodeText, NativeMethods.CF_UNICODETEXT),
345+
new(DataFormats.Text, NativeMethods.CF_TEXT),
346+
new(DataFormats.Bitmap, NativeMethods.CF_BITMAP),
347+
new(DataFormats.MetafilePicture, NativeMethods.CF_METAFILEPICT),
348+
new(DataFormats.EnhancedMetafile, NativeMethods.CF_ENHMETAFILE),
349+
new(DataFormats.Dif, NativeMethods.CF_DIF),
350+
new(DataFormats.Tiff, NativeMethods.CF_TIFF),
351+
new(DataFormats.OemText, NativeMethods.CF_OEMTEXT),
352+
new(DataFormats.Dib, NativeMethods.CF_DIB),
353+
new(DataFormats.Palette, NativeMethods.CF_PALETTE),
354+
new(DataFormats.PenData, NativeMethods.CF_PENDATA),
355+
new(DataFormats.Riff, NativeMethods.CF_RIFF),
356+
new(DataFormats.WaveAudio, NativeMethods.CF_WAVE),
357+
new(DataFormats.SymbolicLink, NativeMethods.CF_SYLK),
358+
new(DataFormats.FileDrop, NativeMethods.CF_HDROP),
359+
new(DataFormats.Locale, NativeMethods.CF_LOCALE)
360+
};
361+
362+
int xamlFormatId = UnsafeNativeMethods.RegisterClipboardFormat(DataFormats.Xaml);
363+
if (xamlFormatId != 0)
364+
_formatList.Add(new(DataFormats.Xaml, xamlFormatId));
365+
366+
// This is the format to store trust boundary information. Essentially this is accompalished by storing
367+
// the permission set of the source application where the content comes from. During paste we compare this to
368+
// the permission set of the target application.
369+
int applicationTrustFormatId = UnsafeNativeMethods.RegisterClipboardFormat(DataFormats.ApplicationTrust);
370+
if (applicationTrustFormatId != 0)
371+
_formatList.Add(new(DataFormats.ApplicationTrust, applicationTrustFormatId));
372+
373+
// RegisterClipboardFormat returns 0 on failure
374+
int inkServicesFrameworkFormatId = UnsafeNativeMethods.RegisterClipboardFormat(StrokeCollection.InkSerializedFormat);
375+
if (inkServicesFrameworkFormatId != 0)
376+
_formatList.Add(new(StrokeCollection.InkSerializedFormat, inkServicesFrameworkFormatId));
389377
}
390378

391379
#endregion Private Methods
@@ -399,7 +387,7 @@ private static void EnsurePredefined()
399387
#region Private Fields
400388

401389
// The registered data format list.
402-
private static List<DataFormat> _formatList;
390+
private static readonly List<DataFormat> _formatList;
403391

404392
// This object is for locking the _formatList to access safe in the multi-thread.
405393
private static readonly object _formatListlock = new();

0 commit comments

Comments
 (0)