From 057850e6c6fc380d6ae2c6bab1f2a1a92defb447 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Wed, 29 May 2024 23:15:24 +0200 Subject: [PATCH 1/3] TryAddWithoutValidation for multiple values could be more efficient --- .../TryAddWithoutValidation.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs diff --git a/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs b/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs new file mode 100644 index 00000000000..ebe6b409474 --- /dev/null +++ b/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs @@ -0,0 +1,44 @@ +// 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, Categories.LINQ)] + public class TryAddWithoutValidationTests + { + [Benchmark] + [ArgumentsSource(nameof(IEnumerableArgument))] + public bool AddHeaders(AddWithoutValidationTestData data) => new HttpResponseMessage().Headers.TryAddWithoutValidation("headerName", data.Values); + + public IEnumerable IEnumerableArgument() + { + yield return new AddWithoutValidationTestData("List", Enumerable.Range(0, 5).Select(i => "value" + i).ToList()); + yield return new AddWithoutValidationTestData("Array", Enumerable.Range(0, 5).Select(i => "value" + i).ToArray()); + yield return new AddWithoutValidationTestData("Hashset", new HashSet(Enumerable.Range(0, 5).Select(i => "value" + i))); + } + } + + public class AddWithoutValidationTestData + { + public IEnumerable Values { get; } + + public string InstanceName { get; } + + public AddWithoutValidationTestData(string instanceName, IEnumerable values) + { + InstanceName = instanceName; + Values = values; + } + + public override string ToString() => InstanceName; + } +} \ No newline at end of file From a71b98441ac64d32765a4440eef6d59918eb6698 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Fri, 31 May 2024 17:46:29 +0200 Subject: [PATCH 2/3] fix remarks --- .../libraries/System.Net.Http/HttpHeaders.cs | 51 +++++++++++++++++++ .../TryAddWithoutValidation.cs | 44 ---------------- 2 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs delete mode 100644 src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs diff --git a/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs b/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs new file mode 100644 index 00000000000..2d1267f2d00 --- /dev/null +++ b/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs @@ -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 IEnumerableArgument() + { + yield return new TryAddWithoutValidationTestData("Array", Enumerable.Range(0, 5).Select(i => "value" + i).ToArray()); // tests array 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(Enumerable.Range(0, 5).Select(i => "value" + i))); // tests the slow path IEnumerable case + } + } + + public class TryAddWithoutValidationTestData + { + public IEnumerable Values { get; } + + public string InstanceName { get; } + + public TryAddWithoutValidationTestData(string instanceName, IEnumerable values) + { + InstanceName = instanceName; + Values = values; + } + + public override string ToString() => InstanceName; + } +} \ No newline at end of file diff --git a/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs b/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs deleted file mode 100644 index ebe6b409474..00000000000 --- a/src/benchmarks/micro/libraries/System.Net.Http/TryAddWithoutValidation.cs +++ /dev/null @@ -1,44 +0,0 @@ -// 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, Categories.LINQ)] - public class TryAddWithoutValidationTests - { - [Benchmark] - [ArgumentsSource(nameof(IEnumerableArgument))] - public bool AddHeaders(AddWithoutValidationTestData data) => new HttpResponseMessage().Headers.TryAddWithoutValidation("headerName", data.Values); - - public IEnumerable IEnumerableArgument() - { - yield return new AddWithoutValidationTestData("List", Enumerable.Range(0, 5).Select(i => "value" + i).ToList()); - yield return new AddWithoutValidationTestData("Array", Enumerable.Range(0, 5).Select(i => "value" + i).ToArray()); - yield return new AddWithoutValidationTestData("Hashset", new HashSet(Enumerable.Range(0, 5).Select(i => "value" + i))); - } - } - - public class AddWithoutValidationTestData - { - public IEnumerable Values { get; } - - public string InstanceName { get; } - - public AddWithoutValidationTestData(string instanceName, IEnumerable values) - { - InstanceName = instanceName; - Values = values; - } - - public override string ToString() => InstanceName; - } -} \ No newline at end of file From 1ce8bdf275de961a4d6e5f60d4b563770312e33e Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sat, 1 Jun 2024 01:37:44 +0200 Subject: [PATCH 3/3] adjust comment of Array case --- src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs b/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs index 2d1267f2d00..8be07ed2669 100644 --- a/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs +++ b/src/benchmarks/micro/libraries/System.Net.Http/HttpHeaders.cs @@ -28,7 +28,7 @@ public bool TryAddWithoutValidationTests(TryAddWithoutValidationTestData data) public IEnumerable IEnumerableArgument() { - yield return new TryAddWithoutValidationTestData("Array", Enumerable.Range(0, 5).Select(i => "value" + i).ToArray()); // tests array optimized case + 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(Enumerable.Range(0, 5).Select(i => "value" + i))); // tests the slow path IEnumerable case }