> For the complete documentation index, see [llms.txt](https://gitbook.fantasticmao.cn/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.fantasticmao.cn/tech/shu-ju-ku/redis/redis-she-ji-yu-shi-xian-bi-ji-ya-suo-lie-biao.md).

# 《Redis 设计与实现》笔记 - 压缩列表

## 数据结构

```
+-------+------+-----+------+------+---+------+-----+
|zlbytes|zltail|zllen|entry1|entry2|...|entryN|zlend|
+-------+------+-----+------+------+---+------+-----+
```

压缩列表是 Redis 为了节约内存而开发的，由一系列特殊编码的连续内存块组成的顺序型数据结构。

压缩列表中的字段描述：

1. zlbytes 记录压缩列表占用的内存字节数；
2. zltail 记录压缩列表尾节点的偏移量；
3. zllen 记录压缩列表的节点数量
4. entryX 是压缩列表保存的节点数据；
5. 每个 entry 压缩列表节点可以保存一个字节数组或者一个整数值
6. 字节数组支持以下三种长度：63（2^6-1）、16,383（2^14-1）、4,294,967,295（2^32-1）
7. 整数值支持以下六种长度：4 bit、1 byte、3 byte、int16\_t、int32\_t、int64\_t
8. zlend 是一个特殊值，值为 `0xFF`，用于记录压缩列表的末端。
