源函数
static inline size_t sdslen(const sds s) { struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); return sh->len; }
这是redis中用来计算字符串长度的函数,时间复杂度为O(1)。具体为什么这么写,我也不是很明白。随着学习的深入,慢慢就懂了。 主要让我困惑的就是这一句:
struct sdshdr *sh=(void*)(s-(sizeof(struct sdshdr))); //这一句是什么意思呢?为什么这么写?理由是什么? //优势很明显,时间复杂度只有O(1),很高效。
源函数
static inline size_t sdslen(const sds s) { struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
>return sh->free;
}
这是描述当前sds字符串中还剩下的空间量的函数。为什么这么写,我也不是很明白。
时间复杂度也是O(1),很高效的写法啊。