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

Use IndexOfAnyValues in Uri #78666

Merged
merged 6 commits into from
Nov 23, 2022
Merged

Conversation

MihaZupan
Copy link
Member

@MihaZupan MihaZupan commented Nov 21, 2022

Contributes to #78204

There are more places we can use IndexOfAnyValues in System.Uri. I'll address those in future PRs to keep them reviewable, this is complex enough as it is.

Method Toolchain Input Mean Error Ratio Allocated
NewUriIdnHost main http://aaaaaaaa(...) [50] 428.2 ns 2.98 ns 1.00 240 B
NewUriIdnHost pr http://aaaaaaaa(...) [50] 314.0 ns 1.31 ns 0.73 240 B
NewUriIdnHost main http://www.Micr(...) [28] 254.3 ns 1.29 ns 1.00 248 B
NewUriIdnHost pr http://www.Micr(...) [28] 232.2 ns 0.67 ns 0.91 192 B
NewUriIdnHost main http://www.micr(...) [28] 240.6 ns 1.09 ns 1.00 192 B
NewUriIdnHost pr http://www.micr(...) [28] 224.2 ns 0.64 ns 0.93 192 B
Benchmark code
[MemoryDiagnoser]
public class UriBenchmarks
{
    public static IEnumerable<string> Inputs()
    {
        yield return "http://www.microsoft.com/foo";
        yield return "http://www.Microsoft.com/foo";
        yield return $"http://{string.Concat(Enumerable.Repeat(new string('a', 12) + '.', 3))}/foo";
    }

    [ParamsSource(nameof(Inputs))]
    public string Input;

    [Benchmark]
    public string NewUriIdnHost() => new Uri(Input).IdnHost;
}

@MihaZupan MihaZupan added this to the 8.0.0 milestone Nov 21, 2022
@MihaZupan MihaZupan requested review from stephentoub and a team November 21, 2022 23:25
@ghost
Copy link

ghost commented Nov 21, 2022

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

Issue Details

Contributes to #78204

There are more places we can use IndexOfAnyValues in System.Uri. I'll address those in future PRs to keep them reviewable, this is complex enough as it is.

Author: MihaZupan
Assignees: -
Labels:

area-System.Net

Milestone: 8.0.0

@ghost ghost assigned MihaZupan Nov 21, 2022
//

internal static unsafe bool IsValid(char* name, int pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile)
public static bool IsValid(ReadOnlySpan<char> hostname, bool iri, bool notImplicitFile, out int length)
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 iri parameter is always constant, so we could handle the differences with a generic, e.g.

private interface IIsValidImpl
{
    static abstract int IndexOfInvalidOrDelimiterChar(ReadOnlySpan<char> chars);
    static abstract bool CheckFirstLabelCharacter(char c);
    static abstract int DotIndex(ReadOnlySpan<char> chars);
    static abstract int CalculateLabelLength(ReadOnlySpan<char> label);
}

but it seemed like it was adding more code than it's worth (although arguably more readable?).

{
if (char.IsAsciiLetterUpper(str[i]))
Debug.Assert(!str.AsSpan(start, index).Contains(':'),
"A colon should appear at most once, and must never be followed by letters.");
Copy link
Member

Choose a reason for hiding this comment

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

ParseCanonicalName is handed already sanitized data?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

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.

2 participants