Redis通信プロトコルの最適化


1、命令簡略化
分析:redis通信プロトコルのコマンドは、元のset、get、hset、hgetなどの文字列で、0 x 01、0 x 02、0 x 03、0 x 04などの単バイトで代用できます.
メリット:ネットワーク転送トラフィックを節約し、dumpファイルとaofファイルのサイズを削減します.
悪いところ:読みにくい(これは問題ではないようです...).
 
2、コマンド区切り文字の簡略化
分析:redis通信プロトコルのコマンド区切り記号は、「r」を使用し、HTTPプロトコルと同様に、「r」または「」を使用することができます.
メリット:ネットワーク転送トラフィックを節約し、dumpファイルとaofファイルのサイズを削減します.
悪いところ:ないようです.
 
3、コマンドの大文字と小文字の最適化(これは1と関係があり、1がやったら、この条はありません)
分析:redis通信プロトコルのコマンドは大文字と小文字を区別せず、ドキュメントでは、プロトコルのコマンドが大文字で書くか小文字で書くかは説明されていませんが、ソースコードを読むと、最終的には小文字に変換されて処理されるので、小文字で直接書くことができる(余分な変換を減らす)ことがわかります.
メリット:大文字から小文字を減らすことで、コマンドの検索を加速します.
デメリット:なし.
参照先:
//       redis.c

/* A case insensitive version used for the command lookup table. */
int dictSdsKeyCaseCompare(void *privdata, const void *key1,
        const void *key2)
{
    DICT_NOTUSED(privdata);

    return strcasecmp(key1, key2) == 0;
}


/* Command table. sds string -> command struct pointer. */
dictType commandTableDictType = {
    dictSdsCaseHash,           /* hash function */
    NULL,                      /* key dup */
    NULL,                      /* val dup */
    dictSdsKeyCaseCompare,     /* key compare */
    dictSdsDestructor,         /* key destructor */
    NULL                       /* val destructor */
};

void initServerConfig() {

    // ...

    /* Command table -- we intiialize it here as it is part of the
     * initial configuration, since command names may be changed via
     * redis.conf using the rename-command directive. */
    server.commands = dictCreate(&commandTableDictType,NULL);
    populateCommandTable();
    server.delCommand = lookupCommandByCString("del");
    server.multiCommand = lookupCommandByCString("multi");

    // ...
}


//       linux kernel 3.4.4    lib/string.c  

#ifndef __HAVE_ARCH_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{
	int c1, c2;

	do {
		c1 = tolower(*s1++);
		c2 = tolower(*s2++);
	} while (c1 == c2 && c1 != 0);
	return c1 - c2;
}
EXPORT_SYMBOL(strcasecmp);
#endif

キー関数strcasecmpを比較する場合は、s 1とs 2の対応文字を小文字に変換してから比較します.
参照リンク:
http://redis.io/topics/protocol