From 00b1573d6ffa6c7244c9853a6acad387a61080b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 11 Oct 2026 19:16:02 +0200 Subject: [PATCH] perf: bind the value accessor of `ContainsKey` only for `WhoseValue` `ContainsKey` and `ContainsKeys` created a closure over the expected keys for every expectation, although only `WhoseValue` and `WhoseValues` use it. The accessor now gets the keys as an argument and is bound to them when `WhoseValue(s)` is used. ABBA, mean of two runs per side: | Method | Before | After | Allocated before / after | |---|---:|---:|---:| | `ContainsKey("b")` | 157.5 ns | 145.0 ns | 496 B / 408 B | | `ContainsKey("b").WhoseValue.IsEqualTo(2)` | 588.2 ns | 594.7 ns | 1,136 B / 1,112 B | | `ContainsKeys("a", "b")` | 951.4 ns | 927.7 ns | 1,768 B / 1,680 B | The time difference of the last two is within the noise; the allocation is stable. --- Source/aweXpect/Results/ContainsKeyResult.cs | 13 +++++++++---- Source/aweXpect/Results/ContainsKeysResult.cs | 12 ++++++++---- .../That/Collections/ThatDictionary.ContainsKey.cs | 2 +- .../That/Collections/ThatDictionary.ContainsKeys.cs | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Source/aweXpect/Results/ContainsKeyResult.cs b/Source/aweXpect/Results/ContainsKeyResult.cs index b84d8572be..e6d41e2592 100644 --- a/Source/aweXpect/Results/ContainsKeyResult.cs +++ b/Source/aweXpect/Results/ContainsKeyResult.cs @@ -13,24 +13,29 @@ public class ContainsKeyResult : AndOrResult { private readonly TKey _key; - private readonly Func _memberAccessor; + private readonly Func _valueAccessor; + /// + /// The gets the as its argument, so that the accessor of + /// the value is only bound to the key when is used. + /// internal ContainsKeyResult( ExpectationBuilder expectationBuilder, TThat returnValue, TKey key, - Func memberAccessor) + Func valueAccessor) : base(expectationBuilder, returnValue) { _key = key; - _memberAccessor = memberAccessor; + _valueAccessor = valueAccessor; } /// /// Further expectations on the selected value of the dictionary. /// public IThat WhoseValue - => new ThatSubject(ExpectationBuilder.ForWhich(_memberAccessor, " whose value ", + => new ThatSubject(ExpectationBuilder.ForWhich((TCollection collection) => _valueAccessor(collection, _key), + " whose value ", $"value of key {Formatter.Format(_key)}", grammars => (grammars & ~ExpectationGrammars.Plural) | ExpectationGrammars.Introduced)); } diff --git a/Source/aweXpect/Results/ContainsKeysResult.cs b/Source/aweXpect/Results/ContainsKeysResult.cs index bd4f13b744..09671e5a4c 100644 --- a/Source/aweXpect/Results/ContainsKeysResult.cs +++ b/Source/aweXpect/Results/ContainsKeysResult.cs @@ -14,17 +14,21 @@ public class ContainsKeysResult : AndOrResult { private readonly TKey[] _keys; - private readonly Func> _memberAccessor; + private readonly Func> _valuesAccessor; + /// + /// The gets the as its argument, so that the + /// accessor of the values is only bound to the keys when is used. + /// internal ContainsKeysResult( ExpectationBuilder expectationBuilder, TThat returnValue, TKey[] keys, - Func> memberAccessor) + Func> valuesAccessor) : base(expectationBuilder, returnValue) { _keys = keys; - _memberAccessor = memberAccessor; + _valuesAccessor = valuesAccessor; } /// @@ -32,7 +36,7 @@ internal ContainsKeysResult( /// public IThat> WhoseValues => new ThatSubject>(ExpectationBuilder - .ForWhich(_memberAccessor, " whose values ", $"values of keys {Formatter.Format(_keys)}", + .ForWhich((TCollection collection) => _valuesAccessor(collection, _keys), " whose values ", $"values of keys {Formatter.Format(_keys)}", grammars => grammars | ExpectationGrammars.Nested | ExpectationGrammars.Plural | ExpectationGrammars.Introduced)); } diff --git a/Source/aweXpect/That/Collections/ThatDictionary.ContainsKey.cs b/Source/aweXpect/That/Collections/ThatDictionary.ContainsKey.cs index 3a4cb4b0df..e91b68f3d4 100644 --- a/Source/aweXpect/That/Collections/ThatDictionary.ContainsKey.cs +++ b/Source/aweXpect/That/Collections/ThatDictionary.ContainsKey.cs @@ -29,7 +29,7 @@ public static partial class ThatDictionary .InvertIf(state.Negated)), subject, expected, - f => TryLookUp(GetLookup(f), expected, out TValue? value) ? value : default + static (f, key) => TryLookUp(GetLookup(f), key, out TValue? value) ? value : default ); } diff --git a/Source/aweXpect/That/Collections/ThatDictionary.ContainsKeys.cs b/Source/aweXpect/That/Collections/ThatDictionary.ContainsKeys.cs index 30b9bb2716..d1eb7ef6ec 100644 --- a/Source/aweXpect/That/Collections/ThatDictionary.ContainsKeys.cs +++ b/Source/aweXpect/That/Collections/ThatDictionary.ContainsKeys.cs @@ -52,7 +52,7 @@ public static partial class ThatDictionary .InvertIf(state.Negated)), subject, keys, - dictionary => new KeyedValues(keys + static (dictionary, keys) => new KeyedValues(keys .Where(key => ContainsKey(dictionary, key)) .Select(key => new KeyValuePair(key, TryLookUp(GetLookup(dictionary), key, out TValue? value) ? value : default)))