redis: dictionary implementation and rehash principle

The dictionary only uses the ht[0] hash table, and the ht[1] hash table will only be used when rehashing the ht[0] hash table

 

key---->hash value---->index value

 

 Redis uses the MurmurHash algorithm, the advantages: even if the input keys are regular, the algorithm can still give a good random distribution, speed block.

 

Resolving key collisions: the chain address method . Each hash table node has a next pointer, and multiple hash table nodes can use the next pointer to form a singly linked list. The program always adds a new node to the head position of the linked list O(1)

 

Load factor--->Hash table expansion and contraction.

Extension: The size of ht[1] is the first 2^n greater than or equal to ht[0].used*2

Shrinkage: The size of ht[1] is the first 2^n greater than or equal to ht[0].used

 

After migrating all key-value pairs contained in ht[0] to ht[1], ht[0] becomes an empty table, release ht[0], set ht[1] to ht[0], and set ht[0] to ht[0]. [1] Create a new blank hash table to prepare for the next rehash

 

No BGSAVE command or BGREWRITEAOF command was executed: load factor greater than or equal to 1 Expand and contract

The BGSAVE command or the BGREWRITEAOF command is being executed: the load factor is greater than or equal to 5 to expand and contract

 

rehash: done in multiple, incremental steps

 

In addition to each time the program performs the specified operation on the dictionary, it will also rehash all the key-value pairs in the rehashidx index of the ht[0] hash table to ht[1].

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325051267&siteId=291194637