Redis data type string, redis Data Type

Source: Internet
Author: User

Redis data type string, redis Data Type
Redis data type string redis string

The redis string is not a C-language native string, but a self-built simple dynamic string (SDS), which is similar to a C-language native string, use '\ 0' as the end.

In addition to printing logs, we basically use SDS to operate strings.

Primary Functions of SDS in redis

1. Save the string value of the database 2. Use it as the buffer

Definition of SDS in redis
Sds. h and sds. c under the src directory under the source package

Typedef char * sds;/*** Save the structure of the string object */struct sdshdr {// The length of occupied space in the buf int len; // The length of the remaining available space in the buf int free; // The data space char buf [];};

Why is SDS used instead of a C-language native string?
SDS features

/*** Returns the actual length */static inline size_t sdslen (const sds s) {struct sdshdr * sh = (void *) (s-(sizeof (struct sdshdr); return sh-> len ;}
    strcat(char* dest,const char* src)

Look at sdscat () in sds. c ()

// Function prototype sds sdscat (sds s, const char * t); // specific implementation of sds sdscat (sds s, const char * t) {return sdscatlen (s, t, strlen (t);} // sdscatlen sds sdscatlen (sds s, const void * t, size_t len) {struct sdshdr * sh; // original string length size_t curlen = sdslen (s); // extended sds space // T = O (N) s = sdsMakeRoomFor (s, len); // insufficient memory? Directly return if (s = NULL) return NULL; // copy the content in t to the back of the string // T = O (N) sh = (void *) (s-(sizeof (struct sdshdr); memcpy (s + curlen, t, len); // update the property sh-> len = curlen + len; sh-> free = sh-> free-len; // Add the new ending symbol s [curlen + len] = '\ 0'; // return the new sds return s ;}

We can see that when sds is splicing, we first get the length of the original string and then expand it.
3. Reduce memory reallocation when modifying strings
C-language strings depend on character arrays. strings with N length correspond to an array of N + 1 characters. Therefore, when operating on strings, the memory of the character array is re-allocated.

For memory reallocation, complicated algorithms and system calls are generally designed, which is relatively time-consuming. Occasional reallocation is acceptable. However, for redis, frequent operations, efficiency is definitely very low.

Do you still remember the structure of sds? An unused space is saved. Sds blocks the association with the underlying array.

So how does sds use this free option to reduce memory reallocation?

Method 1: Pre-allocate memory. Optimize sds Growth

To put it simply, when the redis API modifies the sds, if it involves extending the sds length, it also expands the free space length of the sds. If free is sufficient, free space is used.

So how much? What is the relationship between free and length?
-If the sds size after expansion is less than 1 M, the free = length after expansion, and the total memory size is Free + length + 1 byte
-If the expanded sds is greater than 1 M, the free size is 1 M and the total length is 1 M + length + 1 byte

Method 2. Optimize sds reduction for inert space release

Simply put, when the redis API truncates sds, the program does not immediately recycle the extra memory, but uses the free attribute to save the space.
Implementation Code

Sds sdstrim (sds s, const char * cset) {struct sdshdr * sh = (void *) (s-(sizeof (struct sdshdr); char * start, * end, * sp, * ep; size_t len; // set and record pointer sp = start = s; ep = end = s + sdslen (s)-1; // trim, T = O (N ^ 2) while (sp <= end & strchr (cset, * sp) sp ++; while (ep> start & strchr (cset, * ep) ep --; // After trim is calculated, the remaining String Length len = (sp> ep )? 0: (ep-sp) + 1); // if necessary, move the string content forward // T = O (N) if (sh-> buf! = Sp) memmove (sh-> buf, sp, len); // Add the terminator sh-> buf [len] = '\ 0 '; // update attributes sh-> free = sh-> free + (sh-> len-len); sh-> len = len; // return the adjusted sds return s ;}

Binary Security

A c string can only be used to save ANSI characters, because it cannot contain null characters.
All sds APIs process the data stored in the sds byte array in binary mode and do not process the data in any way.

Compatible with some C-language strings.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.