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

Remove unsafe code from IPAddress #110824

Merged
merged 5 commits into from
Dec 19, 2024
Merged

Remove unsafe code from IPAddress #110824

merged 5 commits into from
Dec 19, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Dec 18, 2024

Contributes to #94941

The safer patterns seem to show better codegen in fact + removed an allocation from MapToIPv6

Copy link
Contributor

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

@EgorBo

This comment was marked as resolved.

@EgorBo

This comment was marked as resolved.

@EgorBo
Copy link
Member Author

EgorBo commented Dec 19, 2024

@EgorBot -amd -arm

using System.Net;
using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
public class IPAddressBenchmarks
{
    static IPAddress Ipv6_1 = IPAddress.Parse("3cca:65e1:957d:e712:1c8c:ec01:6d44:febc");
    static IPAddress Ipv6_2 = IPAddress.Parse("3cca:65e1:957d:e712:1c8c:ec01:6d44:febc");
    static IPAddress Ipv6_3 = IPAddress.Parse("17e1:891a:1a00:d475:92cd:d4c7:5b30:775a");
    static IPAddress Ipv6_4 = IPAddress.Parse("33.55.77.99").MapToIPv6();
    static IPAddress Ipv4_1 = IPAddress.Parse("33.55.77.99");
    static byte[] _buffer = new byte[16];

    [Benchmark]
    public bool CompareSameIPv6() => Ipv6_1.Equals(Ipv6_2);

    [Benchmark]
    public bool CompareDifferentIPv6() => Ipv6_1.Equals(Ipv6_3);

    [Benchmark]
    public IPAddress MapToIPv6() => Ipv4_1.MapToIPv6();

    [Benchmark]
    public bool IsIPv4MappedToIPv6() => Ipv6_4.IsIPv4MappedToIPv6;

    [Benchmark]
    public bool TryWriteBytes_v6() => Ipv6_1.TryWriteBytes(_buffer, out _);

    [Benchmark]
    public IPAddress NewIPAddressv6() => new("1111222233334444"u8);

    [Benchmark]
    public IPAddress ParseIPAddressv6() => IPAddress.Parse("3cca:65e1:957d:e712:1c8c:ec01:6d44:febc");
}

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

Thanks

@MihaZupan MihaZupan added this to the 10.0.0 milestone Dec 19, 2024
@EgorBo
Copy link
Member Author

EgorBo commented Dec 19, 2024

/ba-g "Build browser-wasm linux Release LibraryTests stuck"

@EgorBo EgorBo merged commit 1019d31 into dotnet:main Dec 19, 2024
81 of 83 checks passed
@EgorBo EgorBo deleted the safer-ipaddress branch December 19, 2024 17:05
{
// For IPv4 addresses, compare the integer representation.
return comparand.PrivateAddress == PrivateAddress;
// We give JIT a hint that the arrays are always of length IPv6AddressShorts, so it

Choose a reason for hiding this comment

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

Does Debug.Assert have a side effect on JIT decisions? Even in Release builds? Is it mentioned somewhere or is it not guaranteed? What if the assertion no longer holds in Release?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does Debug.Assert have a side effect on JIT decisions? Even in Release builds? Is it mentioned somewhere or is it not guaranteed? What if the assertion no longer holds in Release?

Debug.Assert doesn't exist in Release code, it's Debug only

Choose a reason for hiding this comment

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

Thank you for clarification. I misunderstood your code comment, and thus the question arose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants