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

Implement IUtf8SpanFormattable on IPAddress and IPNetwork #84487

Merged
merged 4 commits into from
Apr 10, 2023

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Apr 7, 2023

Implements IUtf8SpanFormattable explicitly on both IPAddress and IPNetwork. For IPNetwork, we just use Utf8.TryWrite just as the existing ISpanFormattable uses MemoryExtensions.TryWrite. For IPAddress, the existing formatting code is made to work generically for either byte or char.

In the process, I removed the unsafe pointer-based code from the formatting logic while also making it faster.

Method Toolchain Mean Ratio
TryFormatIPv4 \main\corerun.exe 19.20 ns 1.00
TryFormatIPv4 \pr\corerun.exe 15.22 ns 0.79
TryFormatIPv6 \main\corerun.exe 81.15 ns 1.00
TryFormatIPv6 \pr\corerun.exe 50.50 ns 0.62
private char[] _buffer = new char[100];
private IPAddress _ipv4 = IPAddress.Parse("20.49.104.40");
private IPAddress _ipv6 = IPAddress.Parse("2600:1400:000d:059e:0000:0000:0000:356e");

[Benchmark]
public bool TryFormatIPv4() => _ipv4.TryFormat(_buffer, out _);

[Benchmark]
public bool TryFormatIPv6() => _ipv6.TryFormat(_buffer, out _);

This also fixes #84482.

Contributes to #81500

Implements IUtf8SpanFormattable explicitly on both IPAddress and IPNetwork.  For IPNetwork, we just use Utf8.TryWrite just as the existing ISpanFormattable uses MemoryExtensions.TryWrite.  For IPAddress, the existing formatting code is made to work generically for either byte or char.

In the process, I removed the unsafe pointer-based code from the formatting logic while also making it faster.
@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost assigned stephentoub Apr 7, 2023
@ghost
Copy link

ghost commented Apr 7, 2023

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

Issue Details

Implements IUtf8SpanFormattable explicitly on both IPAddress and IPNetwork. For IPNetwork, we just use Utf8.TryWrite just as the existing ISpanFormattable uses MemoryExtensions.TryWrite. For IPAddress, the existing formatting code is made to work generically for either byte or char.

In the process, I removed the unsafe pointer-based code from the formatting logic while also making it faster.

Method Toolchain Mean Ratio
TryFormatIPv4 \main\corerun.exe 19.20 ns 1.00
TryFormatIPv4 \pr\corerun.exe 15.22 ns 0.79
TryFormatIPv6 \main\corerun.exe 81.15 ns 1.00
TryFormatIPv6 \pr\corerun.exe 50.50 ns 0.62

This also fixes #84482.

Contributes to #81500

Author: stephentoub
Assignees: -
Labels:

area-System.Net

Milestone: 8.0.0

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

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.

IPAddress.TryFormat returns false erroneously for shorter IP addresses
3 participants