From f81b0b4de5019b914c6bca33b6a34d179028350e Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 21 Aug 2020 18:42:15 -0700 Subject: [PATCH] Fix perf regression in IntPtr operators on 32-bit platforms Switching to C# built-in uint/nuint types caused these operators to use long/ulong IntPtr/UIntPtr constructors instead of int/uint IntPtr constructors that it were used before. The fix is to avoid going through the IntPtr/UIntPtr constructors and just depend on the built-in uint/nuint implicit conversions. Fixes #41167 --- src/libraries/System.Private.CoreLib/src/System/IntPtr.cs | 4 ++-- src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs index 15eec9cccba57..c1a9fb84dce43 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs @@ -148,7 +148,7 @@ public static IntPtr Add(IntPtr pointer, int offset) => [NonVersionable] public static unsafe IntPtr operator +(IntPtr pointer, int offset) => - new IntPtr((nint)pointer._value + offset); + (nint)pointer._value + offset; [NonVersionable] public static IntPtr Subtract(IntPtr pointer, int offset) => @@ -156,7 +156,7 @@ public static IntPtr Subtract(IntPtr pointer, int offset) => [NonVersionable] public static unsafe IntPtr operator -(IntPtr pointer, int offset) => - new IntPtr((nint)pointer._value - offset); + (nint)pointer._value - offset; public static int Size { diff --git a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs index 34f4c7706709a..96540328271a4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs @@ -142,7 +142,7 @@ public static UIntPtr Add(UIntPtr pointer, int offset) => [NonVersionable] public static unsafe UIntPtr operator +(UIntPtr pointer, int offset) => - new UIntPtr((nuint)pointer._value + (nuint)offset); + (nuint)pointer._value + (nuint)offset; [NonVersionable] public static UIntPtr Subtract(UIntPtr pointer, int offset) => @@ -150,7 +150,7 @@ public static UIntPtr Subtract(UIntPtr pointer, int offset) => [NonVersionable] public static unsafe UIntPtr operator -(UIntPtr pointer, int offset) => - new UIntPtr((nuint)pointer._value - (nuint)offset); + (nuint)pointer._value - (nuint)offset; public static int Size {