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

TryAddWithoutValidation for multiple values could be more efficient #4240

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using MicroBenchmarks;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;

namespace System.Net.Http.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class HttpHeaders
{
private readonly HttpResponseHeaders _responseHeaders = new HttpResponseMessage().Headers;

[Benchmark]
[ArgumentsSource(nameof(IEnumerableArgument))]
public bool TryAddWithoutValidationTests(TryAddWithoutValidationTestData data)
{
var result = _responseHeaders.TryAddWithoutValidation("headerName", data.Values);
_responseHeaders.Clear();
return result;
}

public IEnumerable<TryAddWithoutValidationTestData> IEnumerableArgument()
{
yield return new TryAddWithoutValidationTestData("Array", Enumerable.Range(0, 5).Select(i => "value" + i).ToArray()); // tests IList optimized case
yield return new TryAddWithoutValidationTestData("List", Enumerable.Range(0, 5).Select(i => "value" + i).ToList()); // tests IList optimized case
yield return new TryAddWithoutValidationTestData("Hashset", new HashSet<string>(Enumerable.Range(0, 5).Select(i => "value" + i))); // tests the slow path IEnumerable case
}
}

public class TryAddWithoutValidationTestData
{
public IEnumerable<string> Values { get; }

public string InstanceName { get; }

public TryAddWithoutValidationTestData(string instanceName, IEnumerable<string> values)
{
InstanceName = instanceName;
Values = values;
}

public override string ToString() => InstanceName;
}
}
Loading