Skip to content

Backport improvements from DictionarySlim to Dictionary #27877

@danmoseley

Description

@danmoseley

Keeping a list of changes made in DictionarySlim that could benefit Dictionary

  • Instead of & 0x7FFFFFFF; to remove the sign bit on the hash code, use (uint) cast and store a uint hashcode, retaining more entropy. To store 'empty' instead of hashcode of -1 use this scheme:
            // 0-based index of next entry in chain: -1 means end of chain
            // also encodes whether this entry _itself_ is part of the free list by changing sign and subtracting 3,
            // so -2 means end of free list, -3 means index 0 but on free list, -4 means index 1 but on free list, etc.
            public int next;
  • Eliminate the _freeCount field and use _freeList == -1 as a sentinel instead
  • Eliminate _buckets == null check on every access, use a dummy 1-element array instead, on which reads fail and adds cause a resize into a real array

Some of these apply to HashSet<T> also.

@AnthonyLloyd @Zhentar

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions