天天看點

Redis 6.0源碼學習 String類型

文章目錄

  • ​​Redis 6.0源碼學習 String類型​​
  • ​​前置知識​​
  • ​​内部編碼​​
  • ​​OBJ_ENCODING_INT​​
  • ​​OBJ_ENCODING_RAW​​
  • ​​OBJ_ENCODING_EMBSTR​​
  • ​​編碼推斷​​

Redis 6.0源碼學習 String類型

  String字元串類型是RedisObject中最基礎的一個類型,相對應的編碼格式有3種,分别是OBJ_ENCODING_RAW、OBJ_ENCODING_INT和OBJ_ENCODING_EMBSTR。

前置知識

Redis 6.0源碼學習 RedisObject

内部編碼

OBJ_ENCODING_INT

  OBJ_ENCODING_INT算是所有編碼格式中最特殊的一個。OBJ_ENCODING_INT編碼中,redisObject中指針屬性ptr存儲的資料是字面量而不是記憶體位址。由于複用了ptr屬性,是以OBJ_ENCODING_INT隻能存儲8位位元組的長整型。

set key 8653
object encoding key      

  在記憶體優化方面,OBJ_ENCODING_INT編碼還做了享元優化。當伺服器初始化的時候,會調用service.c中的createSharedObjects函數。

代碼片段 初始化createSharedObjects.integers
for (j = 0; j < OBJ_SHARED_INTEGERS; j++) {
    shared.integers[j] =
        makeObjectShared(createObject(OBJ_STRING,(void*)(long)j));
    shared.integers[j]->encoding = OBJ_ENCODING_INT;
}      

OBJ_ENCODING_RAW

  OBJ_ENCODING_RAW是一種非常中規中矩的實作,它的ptr指針指向了一個SDS執行個體。

set key "這是一個等于45個字元的字元串...."
object encoding key      

OBJ_ENCODING_EMBSTR

  OBJ_ENCODING_RAW中robj與sds記憶體空間不是連續的,基于局部性思想就誕生了robj與sds采用連續記憶體空間的OBJ_ENCODING_EMBSTR。當字元串長度不大于44個字元時,會采用OBJ_ENCODING_RAW編碼。

set key "hello,world"
object encoding key      

編碼推斷

  setCommand是set指令的實作函數。以"SET a 111"為例,解析完請求之後,111對應的redisObject是編碼為OBJ_ENCODING_RAW的字元串。tryObjectEncoding會進行一次編碼推斷,如果符合OBJ_ENCODING_INT的限制,還可能從享元池中複用對象。

void setCommand(client *c) {
    int j;
    robj *expire = NULL;
    int unit = UNIT_SECONDS;
    int flags = OBJ_SET_NO_FLAGS;

    ...
    ...
    ...

    c->argv[2] = tryObjectEncoding(c->argv[2]);
    setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);
}      
robj *tryObjectEncoding(robj *o) {
    long value;
    sds s = o->ptr;
    size_t len;

    /* Make sure this is a string object, the only type we encode
     * in this function. Other types use encoded memory efficient
     * representations but are handled by the commands implementing
     * the type. */
    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);

    /* We try some specialized encoding only for objects that are
     * RAW or EMBSTR encoded, in other words objects that are still
     * in represented by an actually array of chars. */
    if (!sdsEncodedObject(o)) return o;

    /* It's not safe to encode shared objects: shared objects can be shared
     * everywhere in the "object space" of Redis and may end in places where
     * they are not handled. We handle them only as values in the keyspace. */
     if (o->refcount > 1) return o;

    /* Check if we can represent this string as a long integer.
     * Note that we are sure that a string larger than 20 chars is not
     * representable as a 32 nor 64 bit integer. */
    len = sdslen(s);
    if (len <= 20 && string2l(s,len,&value)) {
        /* This object is encodable as a long. Try to use a shared object.
         * Note that we avoid using shared integers when maxmemory is used
         * because every object needs to have a private LRU field for the LRU
         * algorithm to work well. */
        if ((server.maxmemory == 0 ||
            !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS)) &&
            value >= 0 &&
            value < OBJ_SHARED_INTEGERS)
        {
            decrRefCount(o);
            incrRefCount(shared.integers[value]);
            return shared.integers[value];
        } else {
            if (o->encoding == OBJ_ENCODING_RAW) {
                sdsfree(o->ptr);
                o->encoding = OBJ_ENCODING_INT;
                o->ptr = (void*) value;
                return o;
            } else if (o->encoding == OBJ_ENCODING_EMBSTR) {
                decrRefCount(o);
                return createStringObjectFromLongLongForValue(value);
            }
        }
    }

    /* If the string is small and is still RAW encoded,
     * try the EMBSTR encoding which is more efficient.
     * In this representation the object and the SDS string are allocated
     * in the same chunk of memory to save space and cache misses. */
    if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT) {
        robj *emb;

        if (o->encoding == OBJ_ENCODING_EMBSTR) return o;
        emb = createEmbeddedStringObject(s,sdslen(s));
        decrRefCount(o);
        return emb;
    }

    /* We can't encode the object...
     *
     * Do the last try, and at least optimize the SDS string inside
     * the string object to require little space, in case there
     * is more than 10% of free space at the end of the SDS string.
     *
     * We do that only for relatively large strings as this branch
     * is only entered if the length of the string is greater than
     * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. */
    trimStringObjectIfNeeded(o);

    /* Return the original object. */
    return o;
}