Skip to content

Removing unsafe parts of Decode method in QueryStringEnumerable #56630

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

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/Http/Http/perf/Microbenchmarks/QueryCollectionBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ namespace Microsoft.AspNetCore.Http;
[CategoriesColumn]
public class QueryCollectionBenchmarks
{
private string _queryString;
private string _singleValue;
private string _singleValueWithPlus;
private string _encoded;

[IterationSetup]
public void Setup()
{
_queryString = "?key1=value1&key2=value2&key3=value3&key4=&key5=";
_singleValue = "?key1=value1";
_singleValueWithPlus = "?key1=value1+value2+value3";
_encoded = "?key1=value%231";
}
private const string _queryString = "?key1=value1&key2=value2&key3=value3&key4=&key5=";
private const string _singleValue = "?key1=value1";
private const string _singleValueWithPlus = "?key1=value1+value2+value3";
private const string _encoded = "?key1=value%231";

[Benchmark(Description = "ParseNew")]
[BenchmarkCategory("QueryString")]
Expand Down
20 changes: 9 additions & 11 deletions src/Shared/QueryStringEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
Expand Down Expand Up @@ -88,21 +89,18 @@ public ReadOnlyMemory<char> DecodeName()
public ReadOnlyMemory<char> DecodeValue()
=> Decode(EncodedValue);

private static unsafe ReadOnlyMemory<char> Decode(ReadOnlyMemory<char> chars)
private static ReadOnlyMemory<char> Decode(ReadOnlyMemory<char> chars)
{
// If the value is short, it's cheap to check up front if it really needs decoding. If it doesn't,
// then we can save some allocations.
if (chars.Length < 16 && chars.Span.IndexOfAny('%', '+') < 0)
ReadOnlySpan<char> source = chars.Span;
if (!source.ContainsAny('%', '+'))
{
return chars;
}

#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
ReadOnlySpan<char> span = chars.Span;
return Uri.UnescapeDataString(
string.Create(span.Length,
(IntPtr)(&span), static (dest, ptr) => ((ReadOnlySpan<char>*)ptr)->Replace(dest, '+', ' '))).AsMemory();
#pragma warning restore CS8500
var buffer = new char[source.Length];
source.Replace(buffer, '+', ' ');
var success = Uri.TryUnescapeDataString(buffer, buffer, out var unescapedLength);
Debug.Assert(success);
return buffer.AsMemory(0, unescapedLength);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. The difference here to commit Removing unsafe parts of Decode method in QueryStringEnumerable is that this implementation keeps a reference to the 'buffer' array that might be larger to the return value with the AsMemory() call. On the other hand, this implementation avoids an extra string allocation as shown on the perf measurement's Encoded line - and as well faster in that scenario.

  2. I think Uri.TryUnescapeDataString should not return false, despite this comment. The unescape level used by TryUnescapeDataString method seems to avoid the 'rare' case. As an alternative the code could branch based on the result of TryUnescapeDataString, and in case of a false value invoke the Uri.UnescapeDataString method as a fallback.
    Or as another alternative, go with the implementation from the previous commit.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, TryUnescapeDataString shouldn't be able to return false in our case.
https://source.dot.net/#System.Private.Uri/System/UriExt.cs,648

}
}

Expand Down
Loading