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

Simplify and improve SocketAddress.Equals #71090

Merged
merged 1 commit into from
Jun 22, 2022

Conversation

stephentoub
Copy link
Member

Use SequenceEqual rather than an open-coded loop.

private SocketAddress _addr = new IPEndPoint(IPAddress.Parse("123.123.123.123"), 80).Serialize();
private SocketAddress _addr_same = new IPEndPoint(IPAddress.Parse("123.123.123.123"), 80).Serialize();
private SocketAddress _addr_notsame1 = new IPEndPoint(IPAddress.Parse("123.123.123.124"), 80).Serialize();
private SocketAddress _addr_notsame2 = new IPEndPoint(IPAddress.Parse("124.123.123.123"), 80).Serialize();
private SocketAddress _addr_notsame3 = new IPEndPoint(IPAddress.Parse("2620:1ec:c11::200"), 80).Serialize();

[Benchmark]
public bool Equals_Same() => _addr.Equals(_addr_same);

[Benchmark]
public bool Equals_NotSame1() => _addr.Equals(_addr_notsame1);

[Benchmark]
public bool Equals_NotSame2() => _addr.Equals(_addr_notsame2);

[Benchmark]
public bool Equals_NotSame3() => _addr.Equals(_addr_notsame3);
Method Toolchain Mean Ratio
Equals_Same \main\corerun.exe 46.994 ns 1.00
Equals_Same \pr\corerun.exe 4.507 ns 0.10
Equals_NotSame1 \main\corerun.exe 24.946 ns 1.00
Equals_NotSame1 \pr\corerun.exe 4.446 ns 0.18
Equals_NotSame2 \main\corerun.exe 15.960 ns 1.00
Equals_NotSame2 \pr\corerun.exe 4.410 ns 0.28
Equals_NotSame3 \main\corerun.exe 1.933 ns 1.00
Equals_NotSame3 \pr\corerun.exe 2.172 ns 1.12

Use SequenceEqual rather than an open-coded loop.
@ghost
Copy link

ghost commented Jun 21, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Use SequenceEqual rather than an open-coded loop.

private SocketAddress _addr = new IPEndPoint(IPAddress.Parse("123.123.123.123"), 80).Serialize();
private SocketAddress _addr_same = new IPEndPoint(IPAddress.Parse("123.123.123.123"), 80).Serialize();
private SocketAddress _addr_notsame1 = new IPEndPoint(IPAddress.Parse("123.123.123.124"), 80).Serialize();
private SocketAddress _addr_notsame2 = new IPEndPoint(IPAddress.Parse("124.123.123.123"), 80).Serialize();
private SocketAddress _addr_notsame3 = new IPEndPoint(IPAddress.Parse("2620:1ec:c11::200"), 80).Serialize();

[Benchmark]
public bool Equals_Same() => _addr.Equals(_addr_same);

[Benchmark]
public bool Equals_NotSame1() => _addr.Equals(_addr_notsame1);

[Benchmark]
public bool Equals_NotSame2() => _addr.Equals(_addr_notsame2);

[Benchmark]
public bool Equals_NotSame3() => _addr.Equals(_addr_notsame3);
Method Toolchain Mean Ratio
Equals_Same \main\corerun.exe 46.994 ns 1.00
Equals_Same \pr\corerun.exe 4.507 ns 0.10
Equals_NotSame1 \main\corerun.exe 24.946 ns 1.00
Equals_NotSame1 \pr\corerun.exe 4.446 ns 0.18
Equals_NotSame2 \main\corerun.exe 15.960 ns 1.00
Equals_NotSame2 \pr\corerun.exe 4.410 ns 0.28
Equals_NotSame3 \main\corerun.exe 1.933 ns 1.00
Equals_NotSame3 \pr\corerun.exe 2.172 ns 1.12
Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Net.Sockets

Milestone: -

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

LGTM.
It is curious why the IPv6 would be slower. The difference is small so perhaps noise...?

@stephentoub
Copy link
Member Author

It is curious why the IPv6 would be slower. The difference is small so perhaps noise...?

Nothing needs to be compared other than the length (which differs), and now that length check is the one inside SequenceEqual.

@stephentoub stephentoub merged commit 9b4f152 into dotnet:main Jun 22, 2022
@stephentoub stephentoub deleted the socketaddressbytes branch June 22, 2022 10:31
@karelz karelz added this to the 7.0.0 milestone Jul 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants