Fix FontFamily.Name performance by using span and calling the interop code with char...
authorSantiago Fernandez Madero <safern@microsoft.com>
Thu, 28 Jun 2018 16:17:11 +0000 (09:17 -0700)
committerStephen Toub <stoub@microsoft.com>
Thu, 28 Jun 2018 16:17:11 +0000 (12:17 -0400)
* Fix FontFamily.Name performance by using span and calling the interop
code with char pointer

* PR Feedback

* Remove added System.Memory reference

Commit migrated from https://github.com/dotnet/corefx/commit/5d9a3ccdbd1692568b3ea00b891dce9f326285b5

src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj
src/libraries/System.Drawing.Common/src/System/Drawing/FontFamily.cs
src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs

index 4942958..19a0b7f 100644 (file)
     <Reference Include="System.Drawing" />
   </ItemGroup>
   <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
-</Project>
\ No newline at end of file
+</Project>
index 4228c63..1a0deb8 100644 (file)
@@ -25,7 +25,7 @@ namespace System.Drawing
         private static int s_idCount = 0;
         private int _id;
 #endif
-        
+
         [SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts")]
         private void SetNativeFamily(IntPtr family)
         {
@@ -185,15 +185,12 @@ namespace System.Drawing
         /// <summary>
         /// Returns the name of this <see cref='FontFamily'/> in the specified language.
         /// </summary>
-        public string GetName(int language)
+        public unsafe string GetName(int language)
         {
-            // LF_FACESIZE is 32
-            var name = new StringBuilder(32);
-
+            char* name = stackalloc char[32]; // LF_FACESIZE is 32
             int status = SafeNativeMethods.Gdip.GdipGetFamilyName(new HandleRef(this, NativeFamily), name, language);
             SafeNativeMethods.Gdip.CheckStatus(status);
-
-            return name.ToString();
+            return Marshal.PtrToStringUni((IntPtr)name);
         }
 
         /// <summary>
index 4ebf812..181f087 100644 (file)
@@ -978,18 +978,9 @@ namespace System.Drawing
             private static FunctionWrapper<GdipDeleteFontFamily_delegate> GdipDeleteFontFamily_ptr;
             internal static int IntGdipDeleteFontFamily(HandleRef fontFamily) => GdipDeleteFontFamily_ptr.Delegate(fontFamily);
 
-            private delegate int GdipGetFamilyName_delegate(HandleRef family, IntPtr name, int language);
+            private delegate int GdipGetFamilyName_delegate(HandleRef family, char* name, int language);
             private static FunctionWrapper<GdipGetFamilyName_delegate> GdipGetFamilyName_ptr;
-            internal static int GdipGetFamilyName(HandleRef family, IntPtr name, int language) => GdipGetFamilyName_ptr.Delegate(family, name, language);
-            internal static unsafe int GdipGetFamilyName(HandleRef family, StringBuilder nameBuilder, int language)
-            {
-                const int LF_FACESIZE = 32;
-                char* namePtr = stackalloc char[LF_FACESIZE];
-                int ret = GdipGetFamilyName(family, (IntPtr)namePtr, language);
-                string name = Marshal.PtrToStringUni((IntPtr)namePtr);
-                nameBuilder.Append(name);
-                return ret;
-            }
+            internal static int GdipGetFamilyName(HandleRef family, char* name, int language) => GdipGetFamilyName_ptr.Delegate(family, name, language);
 
             private delegate int GdipIsStyleAvailable_delegate(HandleRef family, FontStyle style, out int isStyleAvailable);
             private static FunctionWrapper<GdipIsStyleAvailable_delegate> GdipIsStyleAvailable_ptr;