Skip to content

Right sizing HashSet ctor is allocating too many elements #24261

@ViktorHofer

Description

@ViktorHofer

When initializing a HashSet like this:

new HashSet<string>(80_000_000);

the underlying arrays (buckets and slots) will not have the size of ~80M (its next prime number) but the size of the next prime number in the pre-computed list, which is 92_434_613. This is an oversize of 11.55% and is not expected by the user. The worst case is that the array gets oversized by 20% because of the layout of the pre-computed prime numbers.

This was introduced by dotnet/corefx#6203

cc @jkotas @stephentoub @danmosemsft

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions