当前位置:网站首页>Redis 6.0 source code learning simple dynamic string
Redis 6.0 source code learning simple dynamic string
2022-07-24 02:03:00 【51CTO】
List of articles
Simple Dynamic String
Source code
- src/sds.h
- src/sds.c
data structure
Simple Dynamic String, abbreviation SDS, yes Redis The basic data structure used to store string and shaping data in .Redis Can only be used in C String as literal ; in the majority of cases ,Redis Use SDS As a string . Compared with C character string ,SDS Has the following advantages :
- Constant complexity gets string length .
- Prevent buffer overflow .
- Reduce the number of memory reallocations required to modify string length .
- Binary security .
- Compatible with the part C String function .
C Strings in languages
C The string in the language is Null-terminated String, It USES ’\0’ To represent the end of the string . And besides the end of the string , The string cannot contain empty characters , Otherwise, the first empty character read will be mistaken for the end sign .
This inconsistency between storing binary and reading is called Binary Unsafe. For binary security ,redis Medium SDS use len Property to record the length of the represented string .
Expand information
Once used data structure
Redis 3.2 The previous data structure is relatively simple , Learning the historical version is more conducive to the understanding of the new data structure .
len Recorded buf Number of bytes already used in , namely SDS The length of the string represented ;free Recorded buf Number of bytes not used in .buf It's an array of bytes , The array length is len+free+1, Because it needs to save one more ’\0’ As an end sign .
Through the understanding of data structure , You can calculate SDS Required memory size . With 64 For example, bit system , A byte string corresponds to SDS Need at least 10 Bytes .
SDS The pointer exposed to the upper layer does not point to SDS Pointer to structure , It's directed at buf The pointer to . because buf Will be redundant at the end of the string ’\0’ As an end sign , therefore SDS Compatible with C Language standard string handler .
data structure
Redis 3.2 After that SDS The structure becomes 5 Kind of :sdshdr5、sdshdr8、sdshdr16、sdshdr32、sdshdr64.sdshdr yes Simple Dynamic String Header Abbreviation , The new structure consists of an indefinite header and an array of characters .
When the string length is less than 32 when , Will use sdshdr5 structure .sdshdr5 The structure only uses flags Indicates the structure type and length , low 3 Bits indicate the structure type , high 5 Bits indicate storage length .
sdshdr8、sdshdr16、sdshdr32 and sdshdr64 The structure is the same , take len and free Separate storage .
About comments sdshdr5 Unused in doubt
sdshdr5 There is a comment in “sdshdr5 is never used.”, When reading the source code of creating strings , Find out sdshdr5 It is actually used . Submit directly here “LNMPRG Source code research ” Conclusion .
Finally, we can confirm , The length is less than 32 The key/value pair , The bottom layer of the key is sdshdr5, And worthy robj The bottom is sdshdr8.
Q1: Why sdshdr5 save key Sure , save value no way ?
My guess is that the key will not update but the value will update , Therefore, the key is stored in as small a structure as possible ; Value update will cause capacity expansion , Simply store in a larger structure .Q2: Why when parsing parameters ,Redis And abandoned the small sdshdr5?
My guess is for the convenience of coding . The number of parameters of different commands is different , At first, I couldn't tell which position was key Which position is value, Simply deal with it in a unified way , In specific scenarios , Then optimize it separately .
Create string
Redis adopt sdsnewlen Function creation SDS. In the function, the appropriate type will be selected according to the length of the string , After initializing the corresponding statistics , Returns a pointer to the contents of the specified string .
Source details :1 It's about
sdsReqType The function of the method is to return the corresponding... Based on the length of the string sds type .
边栏推荐
- NLP introduction + practice: Chapter 1: deep learning and neural network
- Draw a two coordinate diagram with MATLAB (the simplest in the whole network)
- Draw pictures with canvas
- Construction and test of hfish honey pot
- CANopen communication - PDO and SDO
- Location and path planning (lingo, matlab implementation)
- ASP.NET CORE写一个缓存Attribute工具
- How to use the directory classification function of the new version of easycvr (v2.5.0)?
- How QT counts the frequency of letters in a string
- Advantages and disadvantages of XML
猜你喜欢

Location and path planning (lingo, matlab implementation)

Precautions for using XXL job

Draw a two coordinate diagram with MATLAB (the simplest in the whole network)

Arm architecture and programming 7 -- exceptions and interrupts (based on Baiwen arm architecture and programming tutorial video)

Topological sorting & critical path

浅谈领域驱动设计
![[hiflow] regularly send Tencent cloud SMS sending group](/img/af/40e4a16e4214ae2fc4781e85364a64.png)
[hiflow] regularly send Tencent cloud SMS sending group

1000 okaleido tiger launched binance NFT, triggering a rush to buy

Is software testing still popular in 2022?

Exchange 2010 wildcard SSL certificate installation document
随机推荐
通过Arduino IDE向闪存文件系统上传文件
5年接觸近百比特老板,身為獵頭的我,發現昇職的秘密不過4個字
Construction and test of hfish honey pot
Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots
Spark partition operators partitionby, coalesce, repartition
架构实战营模块二作业
Jmeter+influxdb+grafana pressure measurement real-time monitoring platform construction
Improvement of DB file sequential read caused by insert
MySQL Basics (operators, sorting and paging, multi table queries, functions)
Graduation design campus information publishing platform website source code
1000 okaleido tiger launched binance NFT, triggering a rush to buy
Deliver temperature with science and technology, vivo protects the beauty of biodiversity
Halide::Generator生成器使用说明
暑假第三周
Problèmes de localisation et de planification des itinéraires (Lingo, mise en œuvre de MATLAB)
Draw a two coordinate diagram with MATLAB (the simplest in the whole network)
医院综合布线
Structure the second operation of the actual combat battalion module
After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
分布式资源管理与任务调度框架Yarn