Repository navigation
Use the runtime's HashSet.TryGetValue when it exists - #695
Merged
Merged
Conversation
The TryGetValue polyfill scans the set to find the stored value on a hit. HashSet<T>.TryGetValue exists in the runtime from .NET Framework 4.7.2 and .NET Core 2.0, so call it through a cached delegate when it is there, and only scan when it is not.
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
HashSet<T>.TryGetValuepolyfill is compiled for net461 to net471 and netstandard2.0. On a miss it already returns after a hashedContains, but on a hit it scans the whole set to find the stored value, so hits are O(n).HashSet<T>.TryGetValueitself exists in the runtime from .NET Framework 4.7.2 and .NET Core 2.0. Code compiled for these targets very often runs on such a runtime (a net462 app on a machine with .NET Framework 4.8, a netstandard2.0 library on modern .NET). The polyfill now looks the method up once perT, binds it to an open instance delegate held in a static generic class, and calls that. When the method is not there (runtime older than 4.7.2), or binding fails for any reason, the delegate is null and the existingContains+ scan code runs unchanged.Soundness:
bool, parameters(T, out T)) before binding, and is wrapped in a try/catch, so an unexpected runtime can only fall back to the scan.typeof(HashSet<T>).GetMethod("TryGetValue", ...)is a pattern the trimmer understands. The code is only compiled for targets where the AOT/trim analyzers do not apply.defaulton a miss, null handled, no enumerator invalidation.Measurements
Compiled for net462, run on .NET Framework 4.8, Release, x64, Stopwatch, best of 7, ns per call. The hit is a lookup of the last item added.
Tests
Added to
PolyfillTests_HashSet.cs(run on net462, where the polyfill is active, and against the real method everywhere else): the stored instance is returned rather than the argument; hits and a miss in a 2,000 item set with a case-insensitive comparer; value types (int,int?with null, a struct) including thedefaultout value on a miss; afterRemove, re-Add,Clearand on an empty set; calling it while enumerating the set.Not covered: the scan fallback can not be reached on CI, because the .NET Framework there is 4.8, where the real method exists. That code is unchanged by this PR.