Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix HashSet.SetCapacity() that increases capacity failing after a successful Remove(). - #40638

Merged
scalablecory merged 2 commits into
dotnet:masterfrom
scalablecory:40621-hashset-ensurecapacity
Aug 28, 2019
Merged

scalablecory merged 2 commits into
dotnet:masterfrom
scalablecory:40621-hashset-ensurecapacity

Conversation

@scalablecory

Copy link
Copy Markdown

HashSet.SetCapacity does not take into account the freelist, and will bomb out due to a bad array index.

Resolves #40621

@scalablecory scalablecory added this to the 5.0 milestone Aug 28, 2019
@scalablecory scalablecory self-assigned this Aug 28, 2019
@scalablecory
scalablecory requested a review from safern August 28, 2019 02:42

HashSet<T> set = (HashSet<T>)GenericISetFactory(setLength);
set.Remove(set.ElementAt(0));
set.EnsureCapacity(set.Count * 3); // * 3 to ensure we hit the next prime so that a resize happens.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this a known property of primes or just tested happenstance?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, see Bertrand's postulate.

@gfoidl gfoidl Aug 28, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hit the next prime

Eg.
prime := 2 => 2 * 3 = 6, which is not a prime
prime := 3 => 3 * 3 = 9, which is not a prime
...
prime := 4001 => 4001 * 3 = 12003, which is not a prime

Bertrand's postulate states something different.

Or the comment is misleading, as the next prime must not be hit exactely.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

set.EnsureCapacity(set.Count * 3); // * 3 to ensure we hit the next prime so that a resize happens.

Wouldn't it need to be based on capacity rather than count?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bertrand would use * 2 since it's "there some prime between here and twice here" (here > 1), which is true mathematically, but since the hash helper table of primes skips 2 and 5 it "doesn't hold" for N < 7 in our implementation.

set.EnsureCapacity(set.Capacity + 1) will always grow to the next bigger prime (predefined, or actual). If you're wanting to actually skip over (at least) one and go to the next one then set.Capacity * 3 or (set.Capacity + 1) * 2 will do it (the +1 solves the 3 => 6 => 7 problem where we skipped 5, and becomes 3 => 8 => 11). But I don't know that we really would care how many prime candidates we skipped... so I'd just do +1 and assert that the number it returned was bigger than the capacity before the call.

@scalablecory scalablecory Aug 28, 2019 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

set.EnsureCapacity(set.Capacity + 1) will always grow to the next bigger prime (predefined, or actual).

Yea, this is my planned change. I was trying to avoid reflection (there is no public Capacity), but basing it off Count seems fragile if we ever change ValidCollectionSizes and I've realized EnsureCapacity can be used to get the current.


[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void EnsureCapacity_AfterRemovingOne(int setLength)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Other than the comments, LGTM.

Change test so it won't break if we introduce new set sizes.
@scalablecory
scalablecory merged commit 2d6fe6b into dotnet:master Aug 28, 2019

@safern safern left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…cessful Remove(). (dotnet/corefx#40638)

Fix HashSet.SetCapacity() not accounting for freelist when increasing capacity.

Resolves dotnet/corefx#40621 

Commit migrated from dotnet/corefx@2d6fe6b
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexOutOfRangeException in HashSet.EnsureCapacity method

6 participants