-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Optimize allocations in HttpUtility.UrlDecodeToBytes for smaller inputs. #102753
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
Optimize allocations in HttpUtility.UrlDecodeToBytes for smaller inputs. #102753
Conversation
|
Tagging subscribers to this area: @dotnet/ncl |
|
Benchmark: [MemoryDiagnoser]
public class HttpUtilityBenchmarks
{
[Benchmark(Baseline = true)]
public byte[] UrlDecodeToBytes() => HttpUtility.UrlDecodeToBytes("http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");
[Benchmark]
public byte[] UrlDecodeToBytes_PR() => MyHttpUtility.UrlDecodeToBytes("http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");
}Results:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for looking into making improvements here.
Is the existing test coverage here sufficient to catch any potential regressions?
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
|
@EgorBot -intel -arm64 -profiler using BenchmarkDotNet.Attributes;
using System.Web;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<HttpUtilityBenchmarks>(args: args);
public class HttpUtilityBenchmarks
{
[Benchmark]
public byte[] UrlDecodeToBytes() =>
HttpUtility.UrlDecodeToBytes(
"http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");
} |
|
|
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs
Outdated
Show resolved
Hide resolved
|
|
||
| [return: NotNullIfNotNull(nameof(bytes))] | ||
| public static byte[]? UrlDecodeToBytes(byte[]? bytes) => bytes == null ? null : UrlDecodeToBytes(bytes, 0, bytes.Length); | ||
| public static byte[]? UrlDecodeToBytes(byte[]? bytes) => bytes == null ? null : HttpEncoder.UrlDecode(bytes.AsSpan(0, bytes.Length)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| public static byte[]? UrlDecodeToBytes(byte[]? bytes) => bytes == null ? null : HttpEncoder.UrlDecode(bytes.AsSpan(0, bytes.Length)); | |
| public static byte[]? UrlDecodeToBytes(byte[]? bytes) => bytes == null ? null : HttpEncoder.UrlDecode(bytes.AsSpan()); |
For future reference (not worth restarting CI on this PR)
Optimize byte[] allocation when:
If a "happy" path is taken, we will have allocations equals to the returned byte[].
Drawback: method will be a bit slower as we need to calculate Encoding.GetMaxByteCount(xxx)