当前位置:网站首页>Redis source code analysis 3 implementation of discontinuous traversal
Redis source code analysis 3 implementation of discontinuous traversal
2022-07-19 05:22:00 【HjasnJH】
Total ergodic problem , Will lead to the whole redis Unavailable , So discontinuous traversal is introduced
dictscan Implement discontinuous traversal
dictscan The implementation of the
unsigned long dictScan(dict *d,
unsigned long v,
dictScanFunction *fn,
dictScanBucketFunction* bucketfn,
void *privdata)
{
dictht *t0, *t1;
const dictEntry *de, *next;
unsigned long m0, m1;
if (dictSize(d) == 0) return 0;
if (!dictIsRehashing(d)) {
t0 = &(d->ht[0]);
m0 = t0->sizemask;
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
de = t0->table[v & m0];
while (de) {
next = de->next;
fn(privdata, de);
de = next;
}
/* Set unmasked bits so incrementing the reversed cursor
* operates on the masked bits */
v |= ~m0;
/* Increment the reverse cursor */
v = rev(v);
v++;
v = rev(v);
} else {
t0 = &d->ht[0];
t1 = &d->ht[1];
/* Make sure t0 is the smaller and t1 is the bigger table */
if (t0->size > t1->size) {
t0 = &d->ht[1];
t1 = &d->ht[0];
}
m0 = t0->sizemask;
m1 = t1->sizemask;
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
de = t0->table[v & m0];
while (de) {
next = de->next;
fn(privdata, de);
de = next;
}
/* Iterate over indices in larger table that are the expansion
* of the index pointed to by the cursor in the smaller table */
do {
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t1->table[v & m1]);
de = t1->table[v & m1];
while (de) {
next = de->next;
fn(privdata, de);
de = next;
}
/* Increment the reverse cursor not covered by the smaller mask.*/
v |= ~m1;
v = rev(v);
v++;
v = rev(v);
/* Continue while bits covered by mask difference is non-zero */
} while (v & (m0 ^ m1));
}
return v;
}
There are three cases of discontinuous traversal :
1) From the beginning to the end of the iteration , Hash table is not rehash operation .
2) From the beginning to the end of the iteration , The hash table has been expanded or reduced , And just between the two iterations rehash operation .
3) From the beginning to the end of the iteration , Hash table is in progress during one or more iterations rehash operation
The principle of discontinuous traversal
One of the most important things to master , After the expansion and reduction of the dictionary , Where will the original data be in the new dictionary ?
For example, before capacity expansion , Suppose the length is from 4 Extended to 8, Originally in 0 Of the elements of location , In the new hash table , Only in 0 perhaps 4 The location of , The algorithm makes use of this feature , To achieve no repeat traversal and no leakage traversal . The principle of volume reduction is the same .

among , I will list one of them , Let's see how the algorithm can avoid this repeated traversal phenomenon
Suppose the current capacity bit 4, In the third iteration , The hash table has been expanded to 8,
/* Increment the reverse cursor not covered by the smaller mask.*/
v |= ~m1;
v = rev(v);
v++;
v = rev(v); The mask of the array is m0=0x11, ~m0=0x100
Then the first two iterations are as follows :

It's time to start again rehash
The mask of the array m0=0x111,~m0=0x1000

At this point, the traversal is over , Let's see what we have traversed in total
old hash surface :0 2
new hash surface :1 5 3 7
At this time, do you think it's missing ?
Actually , We have a new hash From the perspective of table , In fact, traverse the old hash Tabular 0 2, In fact, it traverses the new hash Tabular 0 4 2 6, because 0-> 0 4 , 2-> 2 6, So in fact, it is realized 0 4 2 6 1 5 3 7 Full traversal of .
Others are similar . This is this reverse binary iteration( Inverse binary iterative algorithm ).
边栏推荐
- SQL注入
- Easypoi excel multi sheet import
- 第一个智能合约程序Faucet.sol
- Get the multi-functional version of the maximum and minimum values of the internal values of the objects in the array and the full version of the roll call system, and show the effect
- MySQL安装配置教程(超级详细)
- 微信小程序5-基础加强(没写完)
- [AI] action recognition using simple neural network -- Based on coco key points
- Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)
- Interface parameters return encapsulated class result
- Easypoi之excel多sheet导入
猜你喜欢
随机推荐
Single arm routing configuration
Applet cloud development form submission and data acquisition in the page
The first smart contract program faucet sol
Ucharts chart, pie chart, bar chart and line chart are used in uniapp
[ES6] quickly print user information to the page
【全网首发】JVM性能问题的自动分析
2.6.2 memory leakage
运维安全要了解的二三事
第一个智能合约程序Faucet.sol
基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
Cesium bind mouse events and remove mouse events
Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr
Wechat applet cloud development and use method-1
STL container - basic operation of vector
路由器loopback口实验
Shell脚本配置root免密登录到其他主机
实习项目1-个性化主页配置
Two methods of rotation chart and automatic rotation
ArcMap 创建常量栅格并镶嵌至新栅格
[ES6] explain in detail the common objects and other methods of set and array (full version)









