標籤:源碼 記憶體資料庫 redis
今天分析的是Redis源碼中的字串操作類的代碼實現。有了上幾次的分析經驗,漸漸覺得我得換一種分析的方法,如果每個API都進行程式碼分析,有些功能性的重複,導致分析效率的偏低。所以下面我覺得對於代碼的分析偏重的是一種功能整體的思維實現來講解,其中我也會挑出一個比較有特點的方法進行拆分瞭解,這也可以讓我們見識一下裡面的一些神奇的代碼。好,迴歸正題,說到字串,這不管放到哪個程式設計語言中,都是使用頻率極高的操作類。什麼new String, concat, strcopy,substr, splitStr,這些方法我們也一定是非常熟悉的了。其實這些方法在我們所說的進階語言中是比較多的,像C語言這種更基礎的語言中還沒有開放那麼多的API,而且人家也沒有String這個類,取而代之的實現手法是char[] 數組的形式。所以今天我們所講的sds字串操作類也是基於char[] 的操作。
首頁我們先列出sds.h標頭檔:
/* SDSLib, A C dynamic strings library * * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#ifndef __SDS_H#define __SDS_H/* 最大分配記憶體1M */#define SDS_MAX_PREALLOC (1024*1024)#include <sys/types.h>#include <stdarg.h>/* 聲明了sds的一種char類型 */typedef char *sds;/* 字串結構體類型 */struct sdshdr {//字元長度 unsigned int len; //當前可用空間 unsigned int free; //具體存放字元的buf char buf[];};/* 計算sds的長度,返回的size_t類型的數值 *//* size_t,它是一個與機器相關的unsigned類型,其大小足以保證儲存記憶體中對象的大小。 */static inline size_t sdslen(const sds s) { struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); return sh->len;}/* 根據sdshdr中的free標記擷取可用空間 */static inline size_t sdsavail(const sds s) { struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); return sh->free;}sds sdsnewlen(const void *init, size_t initlen); //根據給定長度,新生出一個sdssds sdsnew(const char *init); //根據給定的值,生出sdssds sdsempty(void); //清空sds操作size_t sdslen(const sds s); //擷取sds的長度sds sdsdup(const sds s); //sds的複製方法void sdsfree(sds s); //sds的free釋放方法size_t sdsavail(const sds s); //判斷sds擷取可用空間sds sdsgrowzero(sds s, size_t len); // 擴充字元串到指定的長度 sds sdscatlen(sds s, const void *t, size_t len);sds sdscat(sds s, const char *t); //sds串連上char字元sds sdscatsds(sds s, const sds t); //sds串連上sdssds sdscpylen(sds s, const char *t, size_t len); //字串複製相關sds sdscpy(sds s, const char *t); //字串複製相關sds sdscatvprintf(sds s, const char *fmt, va_list ap); //字串格式化輸出,依賴已有的方法sprintf,效率不及下面自己寫的#ifdef __GNUC__sds sdscatprintf(sds s, const char *fmt, ...) __attribute__((format(printf, 2, 3)));#elsesds sdscatprintf(sds s, const char *fmt, ...);#endifsds sdscatfmt(sds s, char const *fmt, ...); //字串格式化輸出sds sdstrim(sds s, const char *cset); //字串縮減void sdsrange(sds s, int start, int end); //字串截取函數void sdsupdatelen(sds s); //更新字串最新的長度void sdsclear(sds s); //字串清空操作int sdscmp(const sds s1, const sds s2); //sds比較函數sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); //字串分割子字串void sdsfreesplitres(sds *tokens, int count); //釋放子字串數組void sdstolower(sds s); //sds字元轉小寫表示void sdstoupper(sds s); //sds字元統一轉大寫sds sdsfromlonglong(long long value); //生出數組字串sds sdscatrepr(sds s, const char *p, size_t len);sds *sdssplitargs(const char *line, int *argc); //參數拆分sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); //字元對應表,"ho", "01", h映射為0, o映射為1sds sdsjoin(char **argv, int argc, char *sep); //以分隔字元連接字串子數組構成新的字串/* Low level functions exposed to the user API *//* 開放給使用者的API */sds sdsMakeRoomFor(sds s, size_t addlen);void sdsIncrLen(sds s, int incr);sds sdsRemoveFreeSpace(sds s);size_t sdsAllocSize(sds s);#endif
裡面定義了我們希望看到的很多常用的方法,不錯,看起來非常的全面,還是很佩服源碼的編寫者,用C語言把這些功能都給實現了。 在開放sds字串實現方法之前,我說一下,sds主要是通過什麼原理實現操作的呢,答案是sdshdr結構體,很多的操作都是先將sds轉化為sdshdr結構,通過裡面的一些變數(相當於此字串的屬性了),來設定當前字串的一些狀態,再返回sdshdr->buf返回操作後的結果。在這裡,可以理解sdshdr為String的對象了,sds只是裡面的具體的值。很多的形式都是基於如下的操作:
比如清空操作方法;
/* Modify an sds string on-place to make it empty (zero length). * However all the existing buffer is not discarded but set as free space * so that next append operations will not require allocations up to the * number of bytes previously available. *//* 清Null 字元串 */void sdsclear(sds s) { struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); //閒置長度增多 sh->free += sh->len; sh->len = 0; //字串中的緩衝其實沒有被丟底,只是把第一個設成了結束標誌,以便下次操作可以複用 sh->buf[0] = '\0';}
比如建立方法,通過返回結構體sdshdr->buf返回新的字串:
/* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. * * The string is always null-termined (all the sds strings are, always) so * even if you create an sds string with: * * mystring = sdsnewlen("abc",3"); * * You can print the string with printf() as there is an implicit \0 at the * end of the string. However the string is binary safe and can contain * \0 characters in the middle, as the length is stored in the sds header. *//* 建立新字串方法,傳入目標長度,初始化方法 */sds sdsnewlen(const void *init, size_t initlen) { struct sdshdr *sh; if (init) { sh = zmalloc(sizeof(struct sdshdr)+initlen+1); } else { //當init函數為NULL時候,又來了zcalloc的方法 sh = zcalloc(sizeof(struct sdshdr)+initlen+1); } if (sh == NULL) return NULL; sh->len = initlen; sh->free = 0; if (initlen && init) memcpy(sh->buf, init, initlen); //最末端同樣要加‘\0’結束符 sh->buf[initlen] = '\0'; //最後是通過返回字串結構體中的buf代表新的字串 return (char*)sh->buf;}
下面重點推薦幾個特殊的方法,平時在C語言中看到的方法,沒想到在這裡看到具體的實現方法了,格式化輸出方法C,語言實現:
/* This function is similar to sdscatprintf, but much faster as it does * not rely on sprintf() family functions implemented by the libc that * are often very slow. Moreover directly handling the sds string as * new data is concatenated provides a performance improvement. * * However this function only handles an incompatible subset of printf-alike * format specifiers: * * %s - C String * %S - SDS string * %i - signed int * %I - 64 bit signed integer (long long, int64_t) * %u - unsigned int * %U - 64 bit unsigned integer (unsigned long long, uint64_t) * %% - Verbatim "%" character. *//* 字串格式化輸出,輸入原字串,格式,參數 */sds sdscatfmt(sds s, char const *fmt, ...) { struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); size_t initlen = sdslen(s); const char *f = fmt; int i; va_list ap; va_start(ap,fmt); f = fmt; /* Next format specifier byte to process. */ i = initlen; /* Position of the next byte to write to dest str. */ //關鍵再次,以此比較輸入的格式類型 while(*f) { char next, *str; unsigned int l; long long num; unsigned long long unum; /* Make sure there is always space for at least 1 char. */ if (sh->free == 0) { s = sdsMakeRoomFor(s,1); sh = (void*) (s-(sizeof(struct sdshdr))); } switch(*f) { case '%': /*如果是%,記住百分比符號後面的類型操作值*/ next = *(f+1); f++; switch(next) { case 's': case 'S': str = va_arg(ap,char*); //判斷普通的str,還是sds類型,計算長度的方法不一樣 l = (next == 's') ? strlen(str) : sdslen(str); if (sh->free < l) { s = sdsMakeRoomFor(s,l); sh = (void*) (s-(sizeof(struct sdshdr))); } //如果是字串,直接複製到後面 memcpy(s+i,str,l); sh->len += l; sh->free -= l; i += l; break; case 'i': case 'I': if (next == 'i') num = va_arg(ap,int); else num = va_arg(ap,long long); { char buf[SDS_LLSTR_SIZE]; //如果是數字,調用添加數值字串方法 l = sdsll2str(buf,num); if (sh->free < l) { s = sdsMakeRoomFor(s,l); sh = (void*) (s-(sizeof(struct sdshdr))); } memcpy(s+i,buf,l); sh->len += l; sh->free -= l; i += l; } break; case 'u': case 'U': //無符號整型同上 if (next == 'u') unum = va_arg(ap,unsigned int); else unum = va_arg(ap,unsigned long long); { char buf[SDS_LLSTR_SIZE]; l = sdsull2str(buf,unum); if (sh->free < l) { s = sdsMakeRoomFor(s,l); sh = (void*) (s-(sizeof(struct sdshdr))); } memcpy(s+i,buf,l); sh->len += l; sh->free -= l; i += l; } break; default: /* Handle %% and generally %<unknown>. */ s[i++] = next; sh->len += 1; sh->free -= 1; break; } break; default: //非操作類型,直接單字元添加 s[i++] = *f; sh->len += 1; sh->free -= 1; break; } f++; } va_end(ap); /* Add null-term */ s[i] = '\0'; return s;}
看完的確讓我感覺很強大,小小的格式化輸出,竟然也沒有那麼簡單,應該java,裡的格式化輸出演算法應該都是跟這差不多的吧。另外一個拆分split方法,這個裡面竟然還因為記憶體的問題的用了goto清空間操作,這個也是漲見識了:
/* Split 's' with separator in 'sep'. An array * of sds strings is returned. *count will be set * by reference to the number of tokens returned. * * On out of memory, zero length string, zero length * separator, NULL is returned. * * Note that 'sep' is able to split a string using * a multi-character separator. For example * sdssplit("foo_-_bar","_-_"); will return two * elements "foo" and "bar". * * This version of the function is binary-safe but * requires length arguments. sdssplit() is just the * same function but for zero-terminated strings. *//* sds字串分割方法類似java.lang.String的spilt方法 */sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) { int elements = 0, slots = 5, start = 0, j; sds *tokens; if (seplen < 1 || len < 0) return NULL;//分割的子字串初始值只有5組 tokens = zmalloc(sizeof(sds)*slots); //如果記憶體溢出,直接返回NULL值 if (tokens == NULL) return NULL; if (len == 0) { *count = 0; return tokens; } //從前往後掃描,到最後一個能匹配分隔字元字串的位置len-seplen for (j = 0; j < (len-(seplen-1)); j++) { /* make sure there is room for the next element and the final one */ //如果當前字串數組數量少於當前已存在數組+2個的時候,動態添加 if (slots < elements+2) { sds *newtokens; slots *= 2; newtokens = zrealloc(tokens,sizeof(sds)*slots); //如果記憶體此時溢出,goto語句free釋放記憶體,終於看到了goto語句的派上用處了 if (newtokens == NULL) goto cleanup; tokens = newtokens; } /* search the separator */ //分成單字元比較和多字元比較匹配 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { //賦值子字串 tokens[elements] = sdsnewlen(s+start,j-start); if (tokens[elements] == NULL) goto cleanup; elements++; start = j+seplen; j = j+seplen-1; /* skip the separator */ } } /* Add the final element. We are sure there is room in the tokens array. */ //最後一個字串添加 tokens[elements] = sdsnewlen(s+start,len-start); //如果記憶體溢出,再次清空,也直接直接返回NULL if (tokens[elements] == NULL) goto cleanup; elements++; *count = elements; return tokens;cleanup: { //清除空間 int i; for (i = 0; i < elements; i++) sdsfree(tokens[i]); zfree(tokens); *count = 0; return NULL; }}
第一次看的共同的用法了,少見少見,spilt的方法實現,也不容易,還考慮了OOM的情況,還考慮了動態擴充記憶體,如果子字串比較多的話,在C語言中,可以看到上面代碼在擴增方面還是非常謹慎的。果然平時搞進階語言基本考慮不到這些問題的,還是佩服佩服。下面再來看看數字字串的添加操作,這個跟我們最早的演算法是一樣的,逐位求餘,再倒序添加到主字串中。
/* Helper for sdscatlonglong() doing the actual number -> string * conversion. 's' must point to a string with room for at least * SDS_LLSTR_SIZE bytes. * * The function returns the lenght of the null-terminated string * representation stored at 's'. *//* 字串末尾添加數值字串組成新的字串 */#define SDS_LLSTR_SIZE 21int sdsll2str(char *s, long long value) { char *p, aux; unsigned long long v; size_t l; /* Generate the string representation, this method produces * an reversed string. */ v = (value < 0) ? -value : value; p = s; //用最傳統的逐位取商算出每一個位置上的數,注意現在的順序其實是逆序的 do { *p++ = '0'+(v%10); v /= 10; } while(v); //後面別忘了加號或減號的添加 if (value < 0) *p++ = '-'; /* Compute length and add null term. */ l = p-s; *p = '\0'; /* Reverse the string. */ //將剛才的添加的逆序的數字字串進行倒敘添加到本身的字串s中 p--; while(s < p) { aux = *s; *s = *p; *p = aux; s++; p--; } return l;}
還有人家的字元對應表功能,其實相當與我們的replace方法,不過redis版本的是單字元對應表,代碼如下:
/* Modify the string substituting all the occurrences of the set of * characters specified in the 'from' string to the corresponding character * in the 'to' array. * * For instance: sdsmapchars(mystring, "ho", "01", 2) * will have the effect of turning the string "hello" into "0ell1". * * The function returns the sds string pointer, that is always the same * as the input pointer since no resize is needed. *//* 字元對應表,"ho", "01", h映射為0, o映射為1 */sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { size_t j, i, l = sdslen(s); for (j = 0; j < l; j++) { for (i = 0; i < setlen; i++) { if (s[j] == from[i]) { s[j] = to[i]; break; } } } return s;}
好了,總的來說,字串操作的實現也是非常龐大的代碼量,已經超過千行了,redis代碼通過調用最原始的char[] 數組的一些方法,實現了字串的功能,讓我明白了一些進階語言中的API的底層實現方法,收穫不錯,字串中也有很多體現了函數式編程的思想,有好多函數當參數的呢,根本沒有具體數值類型的。好,今天的分析就這麼多。
Redis源碼分析(四)-- sds字串