Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

  • Understand the feedback requesting performance optimization
  • Add early Contains check to HashSet.TryGetValue (O(1) for not-found case)
  • Add early Contains check to SortedSet.TryGetValue (O(log n) for not-found case)
  • Build and verify the changes
  • Run code review

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@Tyrrrz Tyrrrz changed the title [WIP] Add polyfills for HashSet<T> and SortedSet<T> TryGetValue Add early returns for HashSet<T> and OrderedSet<T> polyfills Nov 27, 2025
@Tyrrrz Tyrrrz marked this pull request as ready for review November 27, 2025 15:42
Copilot AI review requested due to automatic review settings November 27, 2025 15:42
@Tyrrrz Tyrrrz merged commit 1e1c809 into set-trygetvalue Nov 27, 2025
2 checks passed
@Tyrrrz Tyrrrz deleted the copilot/sub-pr-50 branch November 27, 2025 15:42
Copilot AI requested a review from Tyrrrz November 27, 2025 15:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds early Contains checks to TryGetValue polyfill methods for HashSet<T> and SortedSet<T>, aiming to optimize performance for the not-found case by returning early without iterating through the collection.

Key changes:

  • Added early Contains check in HashSet<T>.TryGetValue before iterating
  • Added early Contains check in SortedSet<T>.TryGetValue before iterating

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
PolyShim/NetCore20/HashSet.cs Adds early return optimization using Contains check before iteration
PolyShim/NetCore20/SortedSet.cs Adds early return optimization using Contains check before iteration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +17 to +22
if (!set.Contains(equalValue))
{
actualValue = default;
return false;
}

Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early Contains check creates redundant work when the item exists in the set. When the item is found, the code performs:

  1. Contains() check - O(1) lookup
  2. Full iteration through the set - O(n)

This means for the success case (item exists), we're doing O(1) + O(n) work instead of just O(n). The early return only optimizes the failure case (item not found), but adds overhead to the success case.

Consider whether the failure case is common enough to justify degrading the success case performance. If both cases are important, a more efficient implementation would directly use the underlying hash table structure to get the actual value in O(1) time, avoiding the iteration entirely.

Suggested change
if (!set.Contains(equalValue))
{
actualValue = default;
return false;
}

Copilot uses AI. Check for mistakes.
Comment on lines +18 to +23
if (!set.Contains(equalValue))
{
actualValue = default;
return false;
}

Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early Contains check creates redundant work when the item exists in the set. When the item is found, the code performs:

  1. Contains() check - O(log n) lookup
  2. Full iteration through the set - O(n)

This means for the success case (item exists), we're doing O(log n) + O(n) work instead of just O(n). The early return only optimizes the failure case (item not found), but adds overhead to the success case.

Consider whether the failure case is common enough to justify degrading the success case performance. If both cases are important, a more efficient implementation would use SortedSet's internal structure more directly to avoid the full iteration.

Suggested change
if (!set.Contains(equalValue))
{
actualValue = default;
return false;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants