Redis string implementation: simple dynamic string – the difference between SDS and C string

Time:2021-10-14

2.1 definition of SDS

struct {
    //The number of bytes used in buf is equal to the length of the string saved in SDS
    int len;
    //Unused byte length in buf
    int free;
    //Byte array to hold string
    char[] buf;
}

2.2 difference between SDS and C string

C string SDS
The complexity of getting string length is O (n) The complexity of getting string length is O (1)
The API is unsafe and may cause buffer overflow API security, eliminating buffer overflow
Modifying the string length n times must require n memory reallocations Modify the string length n times, and at most N memory reallocations occur
Only text can be saved Binary security, which can save text and binary data
All can be usedFunctions in Compatible with some C string functions, some can be usedFunctions in

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]