Skip to content

Update AddValuesToArray sample: net10.0, append methods, grow benchmark - #2207

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/74071-add-values-to-csharp-array
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/74071-add-values-to-csharp-array

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the collections-arrays/AddValuesToArray sample alongside a rewrite of the article it backs.

Retarget and packages

All three projects move from net8.0 to net10.0. BenchmarkDotNet 0.13.1 to 0.15.8, xunit 2.4.1 to 2.9.3, xunit.runner.visualstudio 2.4.3 to 4.0.0, Microsoft.NET.Test.Sdk 17.1.0 to 18.10.0, coverlet.collector 3.1.2 to 10.0.1. Versions read from NuGet on the day this PR opened. Build is clean, 0 warnings and 0 errors.

Benchmark fix

AddValuesToArrayBenchmark declared three fixed 10,000-element fields and then took arraySize from ArgumentsSource, whose body yielded only 10_000. Only the two Manual methods honoured the parameter, and adding a 1,000-element case made ArrayCopyTo copy a 10,000-element source into a 1,000-element destination, which throws ArgumentException. Replaced with [Params(1_000, 10_000)] plus a [GlobalSetup] that builds the source array and the list at the parameterised size.

UsingList(int arraySize, List<int> list) never read arraySize, so the parameter is dropped and its two call sites updated.

New methods and tests

AppendWithResize, AppendWithCollectionExpression, CollectionExpression, GrowWithResize and GrowWithList, each covered by a new xunit test, including one pinning that Array.Resize leaves any other reference on the original array. A new GrowBenchmark class compares resizing in a loop against List<T> followed by one ToArray().

Program.cs in the benchmark project now uses BenchmarkSwitcher so either class can be selected.

10 of 10 tests pass locally on SDK 10.0.302 / runtime 10.0.10.

Retarget all three projects from net8.0 to net10.0 and refresh the
packages: BenchmarkDotNet 0.15.8, xunit 2.9.3 with runner 4.0.0,
Microsoft.NET.Test.Sdk 18.10.0, coverlet.collector 10.0.1.

Fix the benchmark's size parameter. The class held three fixed
10,000-element fields while taking arraySize as an argument, so only the
two Manual methods honoured it and a 1,000-element run made ArrayCopyTo
throw. Replace ArgumentsSource with [Params(1_000, 10_000)] and build the
source array and list in a [GlobalSetup].

Drop the dead arraySize parameter from UsingList and update its two call
sites. Trim trailing whitespace in ArrayCopyTo.

Add AppendWithResize, AppendWithCollectionExpression, CollectionExpression,
GrowWithResize and GrowWithList, with five tests covering them, a
CollectionExpression benchmark and a new GrowBenchmark class comparing
Array.Resize in a loop against List<T>.Add followed by ToArray.

Run the benchmark project through BenchmarkSwitcher so either class can
be selected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant