-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[Uri] Uri.ReCreateParts allocates a char[] that could be avoided #22903
Comments
Here's one way this can be done: In a little benchmark like: using System;
using System.Diagnostics;
class Program
{
static void Main()
{
var sw = new Stopwatch();
var uri = new Uri("http://httpbin.org");
while (true)
{
const int Iters = 20000000;
long mem = GC.GetAllocatedBytesForCurrentThread();
sw.Restart();
for (int i = 0; i < Iters; i++)
{
var result = uri.GetComponents(UriComponents.PathAndQuery | UriComponents.Fragment, UriFormat.UriEscaped);
}
sw.Stop();
mem = GC.GetAllocatedBytesForCurrentThread() - mem;
Console.WriteLine(sw.Elapsed.TotalSeconds + " : " + (mem / (double)Iters));
}
}
} execution time stays approximately the same, but allocation per GetComponents call drops from 336 bytes to 32 bytes. |
More generally, I think there are a bunch of allocations (and probably some CPU) from Uri that we hit in the SocketsHttpHandler path, and that could ideally be avoided. I'm pretty sure we're allocating when we retrieve the Host/IdnHost property, right? The typical case here (for both Host and PathAndQuery) is that we don't need to escape anything, so being able to access this stuff as (Of course |
Yes, that's just a more significant change. |
I know, I'm not suggesting we do anything like that now. I just don't want it to get lost. I'll file a separate issue. |
Accessing a property like Uri.PathAndQuery not only allocates the resulting string, but also a char[] that's often larger than necessary and which is then used to create that resulting string. Whenever/wherever possible, it'd be good to avoid that allocation.
As an example of the impact of this allocation, that one char[] array is currently showing up as ~14% of the memory allocated when making a simple get request with ManagedHandler.
Related to dotnet/corefx#16456
cc: @safern, @geoffkizer, @CIPop
The text was updated successfully, but these errors were encountered: