Skip to content

Commit

Permalink
Fix perf regression in IntPtr operators on 32-bit platforms (#41198)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jkotas authored Aug 24, 2020
1 parent da90d08 commit 384a2ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/IntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ 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) =>
pointer - 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
{
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ 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) =>
pointer - 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
{
Expand Down

0 comments on commit 384a2ae

Please sign in to comment.