关键词搜索

源码搜索 ×
×

漫话Redis源码之四十八

发布2022-01-02浏览492次

详情内容

这里还是老方法,看核心逻辑,主要就是压缩算法,compress那段是核心。

如果觉得细节难以理解,可以不用过问,不用去细看。反正明白意图即可。

任何人,都不可能熟悉每个细节,对吧!

  1. /*
  2. * Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>
  3. *
  4. * Redistribution and use in source and binary forms, with or without modifica-
  5. * tion, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  16. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  17. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  18. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  22. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  23. * OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * the GNU General Public License ("GPL") version 2 or any later version,
  27. * in which case the provisions of the GPL are applicable instead of
  28. * the above. If you wish to allow the use of your version of this file
  29. * only under the terms of the GPL and not to allow others to use your
  30. * version of this file under the BSD license, indicate your decision
  31. * by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL. If you do not delete the
  33. * provisions above, a recipient may use your version of this file under
  34. * either the BSD or the GPL.
  35. */
  36. #include "lzfP.h"
  37. #define HSIZE (1 << (HLOG))
  38. /*
  39. * don't play with this unless you benchmark!
  40. * the data format is not dependent on the hash function.
  41. * the hash function might seem strange, just believe me,
  42. * it works ;)
  43. */
  44. #ifndef FRST
  45. # define FRST(p) (((p[0]) << 8) | p[1])
  46. # define NEXT(v,p) (((v) << 8) | p[2])
  47. # if ULTRA_FAST
  48. # define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
  49. # elif VERY_FAST
  50. # define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
  51. # else
  52. # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
  53. # endif
  54. #endif
  55. /*
  56. * IDX works because it is very similar to a multiplicative hash, e.g.
  57. * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
  58. * the latter is also quite fast on newer CPUs, and compresses similarly.
  59. *
  60. * the next one is also quite good, albeit slow ;)
  61. * (int)(cos(h & 0xffffff) * 1e6)
  62. */
  63. #if 0
  64. /* original lzv-like hash function, much worse and thus slower */
  65. # define FRST(p) (p[0] << 5) ^ p[1]
  66. # define NEXT(v,p) ((v) << 5) ^ p[2]
  67. # define IDX(h) ((h) & (HSIZE - 1))
  68. #endif
  69. #define MAX_LIT (1 << 5)
  70. #define MAX_OFF (1 << 13)
  71. #define MAX_REF ((1 << 8) + (1 << 3))
  72. #if __GNUC__ >= 3
  73. # define expect(expr,value) __builtin_expect ((expr),(value))
  74. # define inline inline
  75. #else
  76. # define expect(expr,value) (expr)
  77. # define inline static
  78. #endif
  79. #define expect_false(expr) expect ((expr) != 0, 0)
  80. #define expect_true(expr) expect ((expr) != 0, 1)
  81. /*
  82. * compressed format
  83. *
  84. * 000LLLLL <L+1> ; literal, L+1=1..33 octets
  85. * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
  86. * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
  87. *
  88. */
  89. unsigned int
  90. lzf_compress (const void *const in_data, unsigned int in_len,
  91. void *out_data, unsigned int out_len
  92. #if LZF_STATE_ARG
  93. , LZF_STATE htab
  94. #endif
  95. )
  96. {
  97. #if !LZF_STATE_ARG
  98. LZF_STATE htab;
  99. #endif
  100. const u8 *ip = (const u8 *)in_data;
  101. u8 *op = (u8 *)out_data;
  102. const u8 *in_end = ip + in_len;
  103. u8 *out_end = op + out_len;
  104. const u8 *ref;
  105. /* off requires a type wide enough to hold a general pointer difference.
  106. * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
  107. * works for differences within a single object). We also assume that no
  108. * no bit pattern traps. Since the only platform that is both non-POSIX
  109. * and fails to support both assumptions is windows 64 bit, we make a
  110. * special workaround for it.
  111. */
  112. #if defined (WIN32) && defined (_M_X64)
  113. unsigned _int64 off; /* workaround for missing POSIX compliance */
  114. #else
  115. unsigned long off;
  116. #endif
  117. unsigned int hval;
  118. int lit;
  119. if (!in_len || !out_len)
  120. return 0;
  121. #if INIT_HTAB
  122. memset (htab, 0, sizeof (htab));
  123. #endif
  124. lit = 0; op++; /* start run */
  125. hval = FRST (ip);
  126. while (ip < in_end - 2)
  127. {
  128. LZF_HSLOT *hslot;
  129. hval = NEXT (hval, ip);
  130. hslot = htab + IDX (hval);
  131. ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS;
  132. if (1
  133. #if INIT_HTAB
  134. && ref < ip /* the next test will actually take care of this, but this is faster */
  135. #endif
  136. && (off = ip - ref - 1) < MAX_OFF
  137. && ref > (u8 *)in_data
  138. && ref[2] == ip[2]
  139. #if STRICT_ALIGN
  140. && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
  141. #else
  142. && *(u16 *)ref == *(u16 *)ip
  143. #endif
  144. )
  145. {
  146. /* match found at *ref++ */
  147. unsigned int len = 2;
  148. unsigned int maxlen = in_end - ip - len;
  149. maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
  150. if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
  151. if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
  152. return 0;
  153. op [- lit - 1] = lit - 1; /* stop run */
  154. op -= !lit; /* undo run if length is zero */
  155. for (;;)
  156. {
  157. if (expect_true (maxlen > 16))
  158. {
  159. len++; if (ref [len] != ip [len]) break;
  160. len++; if (ref [len] != ip [len]) break;
  161. len++; if (ref [len] != ip [len]) break;
  162. len++; if (ref [len] != ip [len]) break;
  163. len++; if (ref [len] != ip [len]) break;
  164. len++; if (ref [len] != ip [len]) break;
  165. len++; if (ref [len] != ip [len]) break;
  166. len++; if (ref [len] != ip [len]) break;
  167. len++; if (ref [len] != ip [len]) break;
  168. len++; if (ref [len] != ip [len]) break;
  169. len++; if (ref [len] != ip [len]) break;
  170. len++; if (ref [len] != ip [len]) break;
  171. len++; if (ref [len] != ip [len]) break;
  172. len++; if (ref [len] != ip [len]) break;
  173. len++; if (ref [len] != ip [len]) break;
  174. len++; if (ref [len] != ip [len]) break;
  175. }
  176. do
  177. len++;
  178. while (len < maxlen && ref[len] == ip[len]);
  179. break;
  180. }
  181. len -= 2; /* len is now #octets - 1 */
  182. ip++;
  183. if (len < 7)
  184. {
  185. *op++ = (off >> 8) + (len << 5);
  186. }
  187. else
  188. {
  189. *op++ = (off >> 8) + ( 7 << 5);
  190. *op++ = len - 7;
  191. }
  192. *op++ = off;
  193. lit = 0; op++; /* start run */
  194. ip += len + 1;
  195. if (expect_false (ip >= in_end - 2))
  196. break;
  197. #if ULTRA_FAST || VERY_FAST
  198. --ip;
  199. # if VERY_FAST && !ULTRA_FAST
  200. --ip;
  201. # endif
  202. hval = FRST (ip);
  203. hval = NEXT (hval, ip);
  204. htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
  205. ip++;
  206. # if VERY_FAST && !ULTRA_FAST
  207. hval = NEXT (hval, ip);
  208. htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
  209. ip++;
  210. # endif
  211. #else
  212. ip -= len + 1;
  213. do
  214. {
  215. hval = NEXT (hval, ip);
  216. htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
  217. ip++;
  218. }
  219. while (len--);
  220. #endif
  221. }
  222. else
  223. {
  224. /* one more literal byte we must copy */
  225. if (expect_false (op >= out_end))
  226. return 0;
  227. lit++; *op++ = *ip++;
  228. if (expect_false (lit == MAX_LIT))
  229. {
  230. op [- lit - 1] = lit - 1; /* stop run */
  231. lit = 0; op++; /* start run */
  232. }
  233. }
  234. }
  235. if (op + 3 > out_end) /* at most 3 bytes can be missing here */
  236. return 0;
  237. while (ip < in_end)
  238. {
  239. lit++; *op++ = *ip++;
  240. if (expect_false (lit == MAX_LIT))
  241. {
  242. op [- lit - 1] = lit - 1; /* stop run */
  243. lit = 0; op++; /* start run */
  244. }
  245. }
  246. op [- lit - 1] = lit - 1; /* end run */
  247. op -= !lit; /* undo run if length is zero */
  248. return op - (u8 *)out_data;
  249. }

相关技术文章

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

提示信息

×

选择支付方式

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