Why is Dictionary.First() so slow?

前端 未结 5 1099
自闭症患者
自闭症患者 2020-12-11 16:35

Not a real question because I already found out the answer, but still interesting thing.

I always thought that hash table is the fastest associative container if you

5条回答
  •  一向
    一向 (楼主)
    2020-12-11 17:33

    Reflector shows that Dictionary maintains a Entry array that it's KeyCollection.Enumerator uses. Normally, the lookup should be relatively fast, as it can just index into the array (assuming you don't want a sorted First):

    // Dictionary
    private Entry[] entries;
    

    However, if you're removing the first elements of that array, then you end up walking the array until you find a non-empty one:

    // Dictionary.KeyCollection.Enumerator
    while (this.index < this.dictionary.count) {
        if (this.dictionary.entries[this.index].hashCode >= 0) {
            this.currentKey = this.dictionary.entries[this.index].key;
            this.index++;
            return true;
        }
        this.index++;
    }
    

    As you remove your entries, you start getting more and more empties at the front of the entries array, and it becomes slower to retrieve First next time.

提交回复
热议问题