Remove unsafe code from IPAddress#110824
Conversation
|
Tagging subscribers to this area: @dotnet/ncl |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
@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");
} |
|
/ba-g "Build browser-wasm linux Release LibraryTests stuck" |
| { | ||
| // 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Does
Debug.Asserthave a side effect on JIT decisions? Even inReleasebuilds? Is it mentioned somewhere or is it not guaranteed? What if the assertion no longer holds inRelease?
Debug.Assert doesn't exist in Release code, it's Debug only
There was a problem hiding this comment.
Thank you for clarification. I misunderstood your code comment, and thus the question arose.
| return | ||
| MemoryMarshal.Read<ulong>(numbers) == 0 && | ||
| BinaryPrimitives.ReadUInt32LittleEndian(numbers.Slice(8)) == 0xFFFF0000; | ||
| return !IsIPv4 && _numbers.AsSpan(0, 6).SequenceEqual((ReadOnlySpan<ushort>)[0, 0, 0, 0, 0, 0xFFFF]); |
There was a problem hiding this comment.
Instead of:
SequenceEqual((ReadOnlySpan<ushort>)[0, 0, 0, 0, 0, 0xFFFF])could it be:
SequenceEqual<ushort>([0, 0, 0, 0, 0, 0xFFFF])?
Contributes to #94941
The safer patterns seem to show better codegen in fact + removed an allocation from
MapToIPv6