关键词搜索

源码搜索 ×
×

漫话Redis源码之三十五

发布2021-12-12浏览929次

详情内容

早年我写代码时候,也喜欢用#if 0来调试,现在不用了。如下代码主要是跟优化level相关:

  1. #ifndef REDIS_STATIC
  2. #define REDIS_STATIC static
  3. #endif
  4. /* Optimization levels for size-based filling.
  5. * Note that the largest possible limit is 16k, so even if each record takes
  6. * just one byte, it still won't overflow the 16 bit count field. */
  7. static const size_t optimization_level[] = {4096, 8192, 16384, 32768, 65536};
  8. /* Maximum size in bytes of any multi-element ziplist.
  9. * Larger values will live in their own isolated ziplists.
  10. * This is used only if we're limited by record count. when we're limited by
  11. * size, the maximum limit is bigger, but still safe.
  12. * 8k is a recommended / default size limit */
  13. #define SIZE_SAFETY_LIMIT 8192
  14. /* Minimum ziplist size in bytes for attempting compression. */
  15. #define MIN_COMPRESS_BYTES 48
  16. /* Minimum size reduction in bytes to store compressed quicklistNode data.
  17. * This also prevents us from storing compression if the compression
  18. * resulted in a larger size than the original data. */
  19. #define MIN_COMPRESS_IMPROVE 8
  20. /* If not verbose testing, remove all debug printing. */
  21. #ifndef REDIS_TEST_VERBOSE
  22. #define D(...)
  23. #else
  24. #define D(...) \
  25. do { \
  26. printf("%s:%s:%d:\t", __FILE__, __func__, __LINE__); \
  27. printf(__VA_ARGS__); \
  28. printf("\n"); \
  29. } while (0)
  30. #endif
  31. /* Bookmarks forward declarations */
  32. #define QL_MAX_BM ((1 << QL_BM_BITS)-1)
  33. quicklistBookmark *_quicklistBookmarkFindByName(quicklist *ql, const char *name);
  34. quicklistBookmark *_quicklistBookmarkFindByNode(quicklist *ql, quicklistNode *node);
  35. void _quicklistBookmarkDelete(quicklist *ql, quicklistBookmark *bm);
  36. /* Simple way to give quicklistEntry structs default values with one call. */
  37. #define initEntry(e) \
  38. do { \
  39. (e)->zi = (e)->value = NULL; \
  40. (e)->longval = -123456789; \
  41. (e)->quicklist = NULL; \
  42. (e)->node = NULL; \
  43. (e)->offset = 123456789; \
  44. (e)->sz = 0; \
  45. } while (0)
  46. /* Create a new quicklist.
  47. * Free with quicklistRelease(). */
  48. quicklist *quicklistCreate(void) {
  49. struct quicklist *quicklist;
  50. quicklist = zmalloc(sizeof(*quicklist));
  51. quicklist->head = quicklist->tail = NULL;
  52. quicklist->len = 0;
  53. quicklist->count = 0;
  54. quicklist->compress = 0;
  55. quicklist->fill = -2;
  56. quicklist->bookmark_count = 0;
  57. return quicklist;
  58. }
  59. #define COMPRESS_MAX ((1 << QL_COMP_BITS)-1)
  60. void quicklistSetCompressDepth(quicklist *quicklist, int compress) {
  61. if (compress > COMPRESS_MAX) {
  62. compress = COMPRESS_MAX;
  63. } else if (compress < 0) {
  64. compress = 0;
  65. }
  66. quicklist->compress = compress;
  67. }
  68. #define FILL_MAX ((1 << (QL_FILL_BITS-1))-1)
  69. void quicklistSetFill(quicklist *quicklist, int fill) {
  70. if (fill > FILL_MAX) {
  71. fill = FILL_MAX;
  72. } else if (fill < -5) {
  73. fill = -5;
  74. }
  75. quicklist->fill = fill;
  76. }
  77. void quicklistSetOptions(quicklist *quicklist, int fill, int depth) {
  78. quicklistSetFill(quicklist, fill);
  79. quicklistSetCompressDepth(quicklist, depth);
  80. }
  81. /* Create a new quicklist with some default parameters. */
  82. quicklist *quicklistNew(int fill, int compress) {
  83. quicklist *quicklist = quicklistCreate();
  84. quicklistSetOptions(quicklist, fill, compress);
  85. return quicklist;
  86. }
  87. REDIS_STATIC quicklistNode *quicklistCreateNode(void) {
  88. quicklistNode *node;
  89. node = zmalloc(sizeof(*node));
  90. node->zl = NULL;
  91. node->count = 0;
  92. node->sz = 0;
  93. node->next = node->prev = NULL;
  94. node->encoding = QUICKLIST_NODE_ENCODING_RAW;
  95. node->container = QUICKLIST_NODE_CONTAINER_ZIPLIST;
  96. node->recompress = 0;
  97. return node;
  98. }
  99. /* Return cached quicklist count */
  100. unsigned long quicklistCount(const quicklist *ql) { return ql->count; }
  101. /* Free entire quicklist. */
  102. void quicklistRelease(quicklist *quicklist) {
  103. unsigned long len;
  104. quicklistNode *current, *next;
  105. current = quicklist->head;
  106. len = quicklist->len;
  107. while (len--) {
  108. next = current->next;
  109. zfree(current->zl);
  110. quicklist->count -= current->count;
  111. zfree(current);
  112. quicklist->len--;
  113. current = next;
  114. }
  115. quicklistBookmarksClear(quicklist);
  116. zfree(quicklist);
  117. }
  118. /* Compress the ziplist in 'node' and update encoding details.
  119. * Returns 1 if ziplist compressed successfully.
  120. * Returns 0 if compression failed or if ziplist too small to compress. */
  121. REDIS_STATIC int __quicklistCompressNode(quicklistNode *node) {
  122. #ifdef REDIS_TEST
  123. node->attempted_compress = 1;
  124. #endif
  125. /* Don't bother compressing small values */
  126. if (node->sz < MIN_COMPRESS_BYTES)
  127. return 0;
  128. quicklistLZF *lzf = zmalloc(sizeof(*lzf) + node->sz);
  129. /* Cancel if compression fails or doesn't compress small enough */
  130. if (((lzf->sz = lzf_compress(node->zl, node->sz, lzf->compressed,
  131. node->sz)) == 0) ||
  132. lzf->sz + MIN_COMPRESS_IMPROVE >= node->sz) {
  133. /* lzf_compress aborts/rejects compression if value not compressable. */
  134. zfree(lzf);
  135. return 0;
  136. }
  137. lzf = zrealloc(lzf, sizeof(*lzf) + lzf->sz);
  138. zfree(node->zl);
  139. node->zl = (unsigned char *)lzf;
  140. node->encoding = QUICKLIST_NODE_ENCODING_LZF;
  141. node->recompress = 0;
  142. return 1;
  143. }
  144. /* Compress only uncompressed nodes. */
  145. #define quicklistCompressNode(_node) \
  146. do { \
  147. if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_RAW) { \
  148. __quicklistCompressNode((_node)); \
  149. } \
  150. } while (0)
  151. /* Uncompress the ziplist in 'node' and update encoding details.
  152. * Returns 1 on successful decode, 0 on failure to decode. */
  153. REDIS_STATIC int __quicklistDecompressNode(quicklistNode *node) {
  154. #ifdef REDIS_TEST
  155. node->attempted_compress = 0;
  156. #endif
  157. void *decompressed = zmalloc(node->sz);
  158. quicklistLZF *lzf = (quicklistLZF *)node->zl;
  159. if (lzf_decompress(lzf->compressed, lzf->sz, decompressed, node->sz) == 0) {
  160. /* Someone requested decompress, but we can't decompress. Not good. */
  161. zfree(decompressed);
  162. return 0;
  163. }
  164. zfree(lzf);
  165. node->zl = decompressed;
  166. node->encoding = QUICKLIST_NODE_ENCODING_RAW;
  167. return 1;
  168. }
  169. /* Decompress only compressed nodes. */
  170. #define quicklistDecompressNode(_node) \
  171. do { \
  172. if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_LZF) { \
  173. __quicklistDecompressNode((_node)); \
  174. } \
  175. } while (0)
  176. /* Force node to not be immediately re-compresable */
  177. #define quicklistDecompressNodeForUse(_node) \
  178. do { \
  179. if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_LZF) { \
  180. __quicklistDecompressNode((_node)); \
  181. (_node)->recompress = 1; \
  182. } \
  183. } while (0)
  184. /* Extract the raw LZF data from this quicklistNode.
  185. * Pointer to LZF data is assigned to '*data'.
  186. * Return value is the length of compressed LZF data. */
  187. size_t quicklistGetLzf(const quicklistNode *node, void **data) {
  188. quicklistLZF *lzf = (quicklistLZF *)node->zl;
  189. *data = lzf->compressed;
  190. return lzf->sz;
  191. }
  192. #define quicklistAllowsCompression(_ql) ((_ql)->compress != 0)
  193. /* Force 'quicklist' to meet compression guidelines set by compress depth.
  194. * The only way to guarantee interior nodes get compressed is to iterate
  195. * to our "interior" compress depth then compress the next node we find.
  196. * If compress depth is larger than the entire list, we return immediately. */
  197. REDIS_STATIC void __quicklistCompress(const quicklist *quicklist,
  198. quicklistNode *node) {
  199. /* If length is less than our compress depth (from both sides),
  200. * we can't compress anything. */
  201. if (!quicklistAllowsCompression(quicklist) ||
  202. quicklist->len < (unsigned int)(quicklist->compress * 2))
  203. return;
  204. #if 0
  205. /* Optimized cases for small depth counts */
  206. if (quicklist->compress == 1) {
  207. quicklistNode *h = quicklist->head, *t = quicklist->tail;
  208. quicklistDecompressNode(h);
  209. quicklistDecompressNode(t);
  210. if (h != node && t != node)
  211. quicklistCompressNode(node);
  212. return;
  213. } else if (quicklist->compress == 2) {
  214. quicklistNode *h = quicklist->head, *hn = h->next, *hnn = hn->next;
  215. quicklistNode *t = quicklist->tail, *tp = t->prev, *tpp = tp->prev;
  216. quicklistDecompressNode(h);
  217. quicklistDecompressNode(hn);
  218. quicklistDecompressNode(t);
  219. quicklistDecompressNode(tp);
  220. if (h != node && hn != node && t != node && tp != node) {
  221. quicklistCompressNode(node);
  222. }
  223. if (hnn != t) {
  224. quicklistCompressNode(hnn);
  225. }
  226. if (tpp != h) {
  227. quicklistCompressNode(tpp);
  228. }
  229. return;
  230. }
  231. #endif
  232. /* Iterate until we reach compress depth for both sides of the list.a
  233. * Note: because we do length checks at the *top* of this function,
  234. * we can skip explicit null checks below. Everything exists. */
  235. quicklistNode *forward = quicklist->head;
  236. quicklistNode *reverse = quicklist->tail;
  237. int depth = 0;
  238. int in_depth = 0;
  239. while (depth++ < quicklist->compress) {
  240. quicklistDecompressNode(forward);
  241. quicklistDecompressNode(reverse);
  242. if (forward == node || reverse == node)
  243. in_depth = 1;
  244. /* We passed into compress depth of opposite side of the quicklist
  245. * so there's no need to compress anything and we can exit. */
  246. if (forward == reverse || forward->next == reverse)
  247. return;
  248. forward = forward->next;
  249. reverse = reverse->prev;
  250. }
  251. if (!in_depth)
  252. quicklistCompressNode(node);
  253. /* At this point, forward and reverse are one node beyond depth */
  254. quicklistCompressNode(forward);
  255. quicklistCompressNode(reverse);
  256. }
  257. #define quicklistCompress(_ql, _node) \
  258. do { \
  259. if ((_node)->recompress) \
  260. quicklistCompressNode((_node)); \
  261. else \
  262. __quicklistCompress((_ql), (_node)); \
  263. } while (0)
  264. /* If we previously used quicklistDecompressNodeForUse(), just recompress. */
  265. #define quicklistRecompressOnly(_ql, _node) \
  266. do { \
  267. if ((_node)->recompress) \
  268. quicklistCompressNode((_node)); \
  269. } while (0)
  270. /* Insert 'new_node' after 'old_node' if 'after' is 1.
  271. * Insert 'new_node' before 'old_node' if 'after' is 0.
  272. * Note: 'new_node' is *always* uncompressed, so if we assign it to
  273. * head or tail, we do not need to uncompress it. */
  274. REDIS_STATIC void __quicklistInsertNode(quicklist *quicklist,
  275. quicklistNode *old_node,
  276. quicklistNode *new_node, int after) {
  277. if (after) {
  278. new_node->prev = old_node;
  279. if (old_node) {
  280. new_node->next = old_node->next;
  281. if (old_node->next)
  282. old_node->next->prev = new_node;
  283. old_node->next = new_node;
  284. }
  285. if (quicklist->tail == old_node)
  286. quicklist->tail = new_node;
  287. } else {
  288. new_node->next = old_node;
  289. if (old_node) {
  290. new_node->prev = old_node->prev;
  291. if (old_node->prev)
  292. old_node->prev->next = new_node;
  293. old_node->prev = new_node;
  294. }
  295. if (quicklist->head == old_node)
  296. quicklist->head = new_node;
  297. }
  298. /* If this insert creates the only element so far, initialize head/tail. */
  299. if (quicklist->len == 0) {
  300. quicklist->head = quicklist->tail = new_node;
  301. }
  302. /* Update len first, so in __quicklistCompress we know exactly len */
  303. quicklist->len++;
  304. if (old_node)
  305. quicklistCompress(quicklist, old_node);
  306. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载