Skip to content

Dictionary key by value: retarget net10.0, add prebuilt reverse map benchmarks - #2235

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/116632-csharp-fastest-way-to-get-a-dictionary-key-by-value
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/116632-csharp-fastest-way-to-get-a-dictionary-key-by-value

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "Fastest Way to Get a Dictionary Key by Value in C#" (https://code-maze.com/csharp-fastest-way-to-get-a-dictionary-key-by-value/). Touches only collections-dictionary/GetDictionaryKeyByValue.

Changes:

  • Both projects retargeted from net8.0 to net10.0. ImplicitUsings and Nullable were already enabled.
  • New ReverseDictionaryLookup class. It builds a reverse Dictionary<string, string> and a FrozenDictionary<string, string> once, with the same TryAdd reversal UseReverseDictionary() uses, and answers each lookup with a single TryGetValue.
  • Two new benchmarks, UsePrebuiltReverseDictionary and UsePrebuiltFrozenDictionary. The maps are built in a [GlobalSetup], so each benchmark times only the lookup. The existing four benchmarks are unchanged: they still charge the two reversing methods the full build on every call, which is the single-lookup case the article's table measures.
  • Four new tests, one per new method in each existing test class (DictionaryHelperPositiveTest and DictionaryHelperNullTest), same GivenA...When...Then... naming. 8 tests become 12.
  • DictionaryHelper.cs is not touched; the article's four methods stay byte-identical to it.

Packages, re-queried from api.nuget.org on 2026-09-23 (newest stable):

  • BenchmarkDotNet 0.13.12 -> 0.15.8 (0.16.0-preview.1 exists and is not taken)
  • Microsoft.NET.Test.Sdk 17.8.0 -> 18.10.1 (the batch plan said 18.10.0; the re-query wins)
  • xunit 2.5.3 -> 2.9.3
  • xunit.runner.visualstudio 2.5.3 -> 4.0.0
  • coverlet.collector 6.0.0 -> 10.0.1

Verified locally on SDK 10.0.302, runtime .NET 10.0.10: dotnet build -c Release 0 warnings, 0 errors; dotnet test 12 passed, 0 failed (8 existing + 4 new), in Release and Debug; dotnet list package --vulnerable --include-transitive reports no vulnerable packages on either project.

…enchmarks

Both projects move from net8.0 to net10.0. BenchmarkDotNet 0.15.8 and the
test packages bumped to today's newest stable.

The existing benchmark charges the two reversing methods a full build on
every call, which measures a single lookup. ReverseDictionaryLookup builds a
reverse Dictionary and a FrozenDictionary once, and two new benchmarks time
only the TryGetValue against each, with the maps built in GlobalSetup. The
four existing benchmarks are unchanged. Each new method gets the same pair of
tests the existing four have.
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