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

Reduce allocation in HttpUtility.UrlPathEncode. #102909

Merged

Conversation

TrayanZapryanov
Copy link
Contributor

Incorporate previously closed PR + feedback.
New benchmarks with slightly modified URL to contain non ascii characters.

[MemoryDiagnoser]
public class HttpUtilityBenchmarks
{
	[Benchmark]
	public bool UrlTryCreate() => Uri.TryCreate("http://EXAMPLE.NET/d\u00E9fault.xxx?sdsd=sds", UriKind.Absolute, out var uri);

	[Benchmark(Baseline = true)]
	public string UrlTryCreateAndUseAuthority() => Uri.TryCreate("http://EXAMPLE.NET/d\u00E9fault.xxx?sdsd=sds", UriKind.Absolute, out var uri) ? uri.Authority : "error";

	[Benchmark]
	public string UrlPathEncode() => HttpUtility.UrlPathEncode("http://EXAMPLE.NET/d\u00E9fault.xxx?sdsd=sds");

	[Benchmark]
	public string UrlPathEncode_PR() => MyHttpUtility.UrlPathEncode("http://EXAMPLE.NET/d\u00E9fault.xxx?sdsd=sds");
}

Results:

Method Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
UrlTryCreate 305.1 ns 6.07 ns 7.68 ns 0.82 0.03 0.0677 568 B 0.86
UrlTryCreateAndUseAuthority 376.1 ns 4.76 ns 4.45 ns 1.00 0.00 0.0792 664 B 1.00
UrlPathEncode 437.4 ns 2.93 ns 2.45 ns 1.16 0.02 0.1211 1016 B 1.53
UrlPathEncode_PR 438.8 ns 6.04 ns 5.65 ns 1.17 0.02 0.0982 824 B 1.24

There is a small regression in worst scenario - I guess due to renting 2 arrays, but this saves quite a lot of allocations.
These are results for URL without non ascii chars: http://example.net:8080/default.xxx?sdsd=sds

Method Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
UrlTryCreate 88.95 ns 0.692 ns 0.614 ns 0.35 0.00 0.0067 56 B 0.23
UrlTryCreateAndUseAuthority 257.24 ns 2.847 ns 2.663 ns 1.00 0.00 0.0286 240 B 1.00
UrlPathEncode 342.34 ns 4.357 ns 3.862 ns 1.33 0.02 0.0830 696 B 2.90
UrlPathEncode_PR 289.49 ns 3.168 ns 2.963 ns 1.13 0.02 0.0591 496 B 2.07

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@MihaZupan
Copy link
Member

Btw this one also hit a conflict after #102917 got merged

@TrayanZapryanov TrayanZapryanov force-pushed the optimize_httputility_UrlPathEncode2 branch from bc43bee to fda0521 Compare June 12, 2024 13:38
@TrayanZapryanov
Copy link
Contributor Author

@MihaZupan Rebase done.

@MihaZupan
Copy link
Member

Thanks

@MihaZupan MihaZupan merged commit 5f91817 into dotnet:main Jun 12, 2024
80 of 83 checks passed
@TrayanZapryanov TrayanZapryanov deleted the optimize_httputility_UrlPathEncode2 branch June 13, 2024 06:31
@github-actions github-actions bot locked and limited conversation to collaborators Jul 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants