Definition of SDS

  • Redis does not use the traditional string representation of C language. It builds an abstract type of simple dynamic string by itself, which is SDS. The C string in Redis is only used for places that do not need to modify the string, such as printing logs

  • The data structure

    Struct SDSHDR {// Record the number of bytes used in the buF array // is equal to the length of the string held by the SDS int len; // Record the number of unused bytes in the buF array. // An array of bytes to hold the string char buf[]}Copy the code

SDS follows an empty string end because it is possible to reuse functions in C strings such as printf(“%s”,s->buf)

Two SDS and C string difference

  1. Constant complexity gets the length of the string

    It records its own length information, so it needs to traverse every time to obtain the length, while SDS can directly obtain the length information through LEN, saving performance

  2. Prevent cache overflow

    Strings in C do not record their length. Therefore, when strcat is executed to append another string to the end of a string, the previous string will be overwritten if the string is not expanded first, resulting in an overflow of the cache

  3. Reduce the number of memory reallocations when modifying strings

    C language strings do not record their length information, adding strings requires expansion calculation and redistribution, while SDS reduces memory redistribution through space pre-allocation and lazy space release

    • Space preallocation

      If the footprint is less than 1M, it allocates the same length as Len, which is free

    • Inert space release

      When the string is deleted, the free space is not deleted, so that the next increment can use the free space directly

  4. Binary security

    The C string must conform to a certain code, but some codes judge the string differently. For example, redis look, with a space in the middle, may need to wrap a line with a certain code, causing problems, but SDS judges the end of the string by len attribute, so there is no problem.