redis6.0.5之lzf阅读笔记1--压缩解压定义
2021/7/12 19:06:28
本文主要是介绍redis6.0.5之lzf阅读笔记1--压缩解压定义,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
********************lzf.h************************************************************************************************* /*********************************************************************** ** ** lzf -- an extremely fast/free compression/decompression-method 一种非常快速/免费的压缩/解压方法 ** http://liblzf.plan9.de/ ** ** This algorithm is believed to be patent-free. 这种算法被认为是无专利的 ** ***********************************************************************/ #define LZF_VERSION 0x0105 /* 1.5, API version */ LZF的版本,方便压缩数据之间的兼容 /* * Compress in_len bytes stored at the memory block starting at * in_data and write the result to out_data, up to a maximum length * of out_len bytes. 压缩存储在内存块中从in_data开始的in_len长字节的数据, 将压缩结果写入到out_data中,最大的长度为out_len个字节 * * If the output buffer is not large enough or any error occurs return 0, * otherwise return the number of bytes used, which might be considerably * more than in_len (but less than 104% of the original size), so it * makes sense to always use out_len == in_len - 1), to ensure _some_ * compression, and store the data uncompressed otherwise (with a flag, of * course. 如果输出的缓存不够大或者任何错误发生,返回0,否则返回使用过的字节数,这个可能超过输入的长度in_len (但是会少于原大小的百分之104),所以使用out_len == in_len - 1 时有意义的,确保压缩结果,否则保存不压缩的结果,当然需要带标志 * lzf_compress might use different algorithms on different systems and * even different runs, thus might result in different compressed strings * depending on the phase of the moon or similar factors. However, all * these strings are architecture-independent and will result in the * original data when decompressed using lzf_decompress. lzf压缩算法使用在不同的系统甚至不同的运行中使用不同的系统,这个可能导致基于指标或者类似的因素产生不同的压缩字符串。 然而,所有的压缩字符串都时独立于体系结构的,如果使用lzf解压都能获得原始的数据 * * The buffers must not be overlapping. 缓存不能重叠 * * If the option LZF_STATE_ARG is enabled, an extra argument must be * supplied which is not reflected in this header file. Refer to lzfP.h * and lzf_c.c. 如果选项LZF_STATE_ARG是有效的,一个额外参数必需提供,该参数不会反映在这个头文件,参见头文件lzfP.h 和 lzf_c.c */ unsigned int lzf_compress (const void *const in_data, unsigned int in_len, void *out_data, unsigned int out_len); /* * Decompress data compressed with some version of the lzf_compress * function and stored at location in_data and length in_len. The result * will be stored at out_data up to a maximum of out_len characters. 解压使用某个lzf压缩函数压缩的保存在in_data中的数据,长度为in_len。将结果保存到out_data中,最大长度为out_len个字符 * If the output buffer is not large enough to hold the decompressed * data, a 0 is returned and errno is set to E2BIG. Otherwise the number * of decompressed bytes (i.e. the original length of the data) is * returned. 如果输出的缓存不是足够大能保持解压的数据,0会返回,错误代码会设置为E2BIG。否则返回解压后的字节数(例如,数据的原始长度) * If an error in the compressed data is detected, a zero is returned and * errno is set to EINVAL. 如果有错误在压缩的数据中发现,返回0, 错误码设置为EINVAL * This function is very fast, about as fast as a copying loop. 这个函数是非常快的,和拷贝循环一样的快! */ unsigned int lzf_decompress (const void *const in_data, unsigned int in_len, void *out_data, unsigned int out_len); #endif ********************************************************************************************************************* ***************************************lzfP.h************************************************************************ #ifndef LZFP_h #define LZFP_h #define STANDALONE 1 /* at the moment, this is ok. */ #ifndef STANDALONE # include "lzf.h" #endif /* * Size of hashtable is (1 << HLOG) * sizeof (char *) hash表的大小为 (1 << HLOG) * sizeof (char *) * decompression is independent of the hash table size 解压同hash表大小无关 * the difference between 15 and 14 is very small HLOG为14和15之间的差距很小 * for small blocks (and 14 is usually a bit faster). 对于小的块(14通常会快一点) * For a low-memory/faster configuration, use HLOG == 13; 对于一个低内存/高速 的配置,使用HLOG为13 * For best compression, use 15 or 16 (or more, up to 22). 对一个最好的压缩比,使用15或者16(或者更多到22) */ #ifndef HLOG # define HLOG 16 #endif /* * Sacrifice very little compression quality in favour of compression speed. * This gives almost the same compression as the default code, and is * (very roughly) 15% faster. This is the preferred mode of operation. */ 牺牲很少的压缩质量从而提高压缩的速度。这个提供了几乎于原代码相同的压缩比,但是能提高百分之15的速度(粗略计算)。 这个是优选的操作 #ifndef VERY_FAST # define VERY_FAST 1 #endif /* * Sacrifice some more compression quality in favour of compression speed. * (roughly 1-2% worse compression for large blocks and * 9-10% for small, redundant, blocks and >>20% better speed in both cases) * In short: when in need for speed, enable this for binary data, * possibly disable this for text data. */ 为了压缩速度牺牲一些压缩质量(对大数据块的压缩比大约差百分之1到2,对于冗余的小的块差百分之9到10, 但是对于这两种情况可以获得大于百分之20的速度)。简单的说,如果需要提高速度, 对于二进制数据使用这个选项,对于文本数据关闭这个选项 #ifndef ULTRA_FAST # define ULTRA_FAST 0 #endif /* * Unconditionally aligning does not cost very much, so do it if unsure 无条件对齐 不会花费很多代价,所以如果不确定就这样做(即对齐) */ #ifndef STRICT_ALIGN # if !(defined(__i386) || defined (__amd64)) # define STRICT_ALIGN 1 # else # define STRICT_ALIGN 0 # endif #endif /* * You may choose to pre-set the hash table (might be faster on some * modern cpus and large (>>64k) blocks, and also makes compression * deterministic/repeatable when the configuration otherwise is the same). */ 你可以选择预设hash表(这样可能使得在某些现代cpu和大数据块(大于64k)速度更快, 并且在其它配置相同时,使得压缩具有确定性和重复性) #ifndef INIT_HTAB # define INIT_HTAB 0 #endif /* * Avoid assigning values to errno variable? for some embedding purposes * (linux kernel for example), this is necessary. NOTE: this breaks * the documentation in lzf.h. Avoiding errno has no speed impact. */ 避免关联错误信息到变量errno,这是为了嵌入目的考虑的(例如Linux内核),那么这个就和必要。(不能同内核错误混在一起) 注意:这种情况打破了文档lzf.h中的描述。 避免错误代码不影响速度 #ifndef AVOID_ERRNO # define AVOID_ERRNO 0 #endif /* * Whether to pass the LZF_STATE variable as argument, or allocate it * on the stack. For small-stack environments, define this to 1. * NOTE: this breaks the prototype in lzf.h. */ 是否传入LZF_STATE变量当做一个参数,或者分配在栈上分配它。对于一个小栈的环境,定义为1. 注意:这种情况不同于定于在lzf.h中的原型 #ifndef LZF_STATE_ARG # define LZF_STATE_ARG 0 #endif /* * Whether to add extra checks for input validity in lzf_decompress * and return EINVAL if the input stream has been corrupted. This * only shields against overflowing the input buffer and will not * detect most corrupted streams. * This check is not normally noticeable on modern hardware * (<1% slowdown), but might slow down older cpus considerably. */ 是否对输入lzf解压数据的有效性添加额外的检查,如果输入的流损坏,返回EINVAL。 这个值针对输入缓冲区的溢出,不会检车更多的损坏数据流。 这个检查在现代硬件上不会很明显降低速速(小于百分之1的降速),但是对老的cpu会有明显的降速 #ifndef CHECK_INPUT # define CHECK_INPUT 1 #endif /* * Whether to store pointers or offsets inside the hash table. On * 64 bit architetcures, pointers take up twice as much space, * and might also be slower. Default is to autodetect. */ 是否在hash表内部保存指针或者偏移量。在64比特架构中,指正需要使用两倍的空间, 可能导致变慢。默认是自动检测(是否使用保存模式) /*#define LZF_USER_OFFSETS autodetect */ /*****************************************************************************/ /* nothing should be changed below */ 以下不允许修改 #ifdef __cplusplus # include <cstring> # include <climits> using namespace std; #else # include <string.h> # include <limits.h> #endif #ifndef LZF_USE_OFFSETS # if defined (WIN32) Windows平台 # define LZF_USE_OFFSETS defined(_M_X64) 根据是否定义了_M_X64 来设定 LZF_USE_OFFSETS的值 -- https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?redirectedfrom=MSDN&view=msvc-160 -- _M_X64 Defined as the integer literal value 100 for compilations that target x64 processors. Otherwise, undefined. # else # if __cplusplus > 199711L 大于这值就用c++的库 # include <cstdint> # else # include <stdint.h> 小于就用c库 # endif # define LZF_USE_OFFSETS (UINTPTR_MAX > 0xffffffffU) 是否大于0xffffffffU来设定 LZF_USE_OFFSETS的值 # endif #endif typedef unsigned char u8; #if LZF_USE_OFFSETS 如果定义偏移量为1 # define LZF_HSLOT_BIAS ((const u8 *)in_data) 就从输入数据开始的点为偏移量 typedef unsigned int LZF_HSLOT; 用整型 #else # define LZF_HSLOT_BIAS 0 typedef const u8 *LZF_HSLOT; 单个字节 #endif typedef LZF_HSLOT LZF_STATE[1 << (HLOG)]; 定义一个2^16的数组,具体大小更上面定义的变量类型有关 #if !STRICT_ALIGN /* for unaligned accesses we need a 16 bit datatype. */ 对于没有对齐的访问,我们需要一个16位的数据类型 # if USHRT_MAX == 65535 如果shot的最大值为65535,那么16位使用short即可 typedef unsigned short u16; # elif UINT_MAX == 65535 如果int的最大值为65535,那么使用int typedef unsigned int u16; # else # undef STRICT_ALIGN # define STRICT_ALIGN 1 严格对齐为1 # endif #endif #if ULTRA_FAST 如果定义了超级快 # undef VERY_FAST 取消 非常快的定义 #endif #endif *********************************************************************************************************************
这篇关于redis6.0.5之lzf阅读笔记1--压缩解压定义的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧
- 2024-12-04Redis入门:简单教程详解
- 2024-11-29Redis开发入门教程:从零开始学习Redis
- 2024-11-27Redis入门指南:快速掌握Redis基础操作