Skip to content

Improve HeaderSplit perf #9309

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 5 commits into from
Apr 12, 2019
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
10 changes: 5 additions & 5 deletions src/Http/Http.Abstractions/src/Internal/ParsingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ public static StringValues GetHeader(IHeaderDictionary headers, string key)
public static StringValues GetHeaderSplit(IHeaderDictionary headers, string key)
{
var values = GetHeaderUnmodified(headers, key);
return new StringValues(GetHeaderSplitImplementation(values).ToArray());
}

private static IEnumerable<string> GetHeaderSplitImplementation(StringValues values)
{
StringValues result = default;

foreach (var segment in new HeaderSegmentCollection(values))
{
if (!StringSegment.IsNullOrEmpty(segment.Data))
{
var value = DeQuote(segment.Data.Value);
if (!string.IsNullOrEmpty(value))
{
yield return value;
result = StringValues.Concat(in result, value);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't use in now that StringValues is only a pointer size dotnet/extensions#1283

OTOH there isn't currently a Concat overload that takes (StringValues values, string value) without the in so may also want to add that?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can look at this in future changes. How much would it help?

}
}
}

return result;
}

public static StringValues GetHeaderUnmodified(IHeaderDictionary headers, string key)
Expand Down
1 change: 1 addition & 0 deletions src/Http/perf/Microbenchmarks/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: BenchmarkDotNet.Attributes.AspNetCoreBenchmark]
63 changes: 63 additions & 0 deletions src/Http/perf/Microbenchmarks/GetHeaderSplitBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks
{
public class GetHeaderSplitBenchmark
{
HeaderDictionary _dictionary;

[GlobalSetup]
public void GlobalSetup()
{
var dict = new Dictionary<string, StringValues>()
{
{ "singleValue", new StringValues("single") },
{ "singleValueQuoted", new StringValues("\"single\"") },
{ "doubleValue", new StringValues(new [] { "first", "second" }) },
{ "manyValue", new StringValues(new [] { "first", "second", "third", "fourth", "fifth", "sixth" }) }
};
_dictionary = new HeaderDictionary(dict);
}

[Benchmark]
public void SplitSingleHeader()
{
var values = ParsingHelpers.GetHeaderSplit(_dictionary, "singleValue");
if (values.Count != 1)
throw new Exception();
}

[Benchmark]
public void SplitSingleQuotedHeader()
{
var values = ParsingHelpers.GetHeaderSplit(_dictionary, "singleValueQuoted");
if (values.Count != 1)
throw new Exception();
}

[Benchmark]
public void SplitDoubleHeader()
{
var values = ParsingHelpers.GetHeaderSplit(_dictionary, "doubleValue");
if (values.Count != 2)
throw new Exception();
}

[Benchmark]
public void SplitManyHeaders()
{
var values = ParsingHelpers.GetHeaderSplit(_dictionary, "manyValue");
if (values.Count != 6)
throw new Exception();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="BenchmarkDotNet" />
<Reference Include="Microsoft.AspNetCore.BenchmarkRunner.Sources" />
<Reference Include="Microsoft.AspNetCore.Http.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http" />
</ItemGroup>
</Project>