Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix perf regression in IntPtr operators on 32-bit platforms #41198

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this still implicitly invoking the IntPtr ctor since it returns one? Your change somehow changes it to use the int one on x86?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C# compiler has implicit nint -> IntPtr conversion. This conversion is no-op. It does not generate any code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that was what I was missing. OK thanks LGTM

(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