Commit 33bfac3
Optimize zset memory usage by embedding element in skiplist (#2508)
By default, when the number of elements in a zset exceeds 128, the
underlying data structure adopts a skiplist. We can reduce memory usage
by embedding elements into the skiplist nodes. Change the `zskiplistNode`
memory layout as follows:
```
Before
+-------------+
+-----> | element-sds |
| +-------------+
|
+------------------+-------+------------------+---------+-----+---------+
| element--pointer | score | backward-pointer | level-0 | ... | level-N |
+------------------+-------+------------------+---------+-----+---------+
After
+-------+------------------+---------+-----+---------+-------------+
+ score | backward-pointer | level-0 | ... | level-N | element-sds |
+-------+------------------+---------+-----+---------+-------------+
```
Before the embedded SDS representation, we include one byte representing
the size of the SDS header, i.e. the offset into the SDS representation
where that actual string starts.
The memory saving is therefore one pointer minus one byte = 7 bytes per
element, regardless of other factors such as element size or number of
elements.
### Benchmark step
I generated the test data using the following lua script && cli command.
And check memory usage using the `info` command.
**lua script**
```
local start_idx = tonumber(ARGV[1])
local end_idx = tonumber(ARGV[2])
local elem_count = tonumber(ARGV[3])
for i = start_idx, end_idx do
local key = "zset:" .. string.format("%012d", i)
local members = {}
for j = 0, elem_count - 1 do
table.insert(members, j)
table.insert(members, "member:" .. j)
end
redis.call("ZADD", key, unpack(members))
end
return "OK: Created " .. (end_idx - start_idx + 1) .. " zsets"
```
**valkey-cli command**
`valkey-cli EVAL "$(catcreate_zsets.lua)" 0 0 100000
${ZSET_ELEMENT_NUM}`
### Benchmark result
|number of elements in a zset | memory usage before optimization |
memory usage after optimization | change |
|-------|-------|-------|-------|
| 129 | 1047MB | 943MB | -9.9% |
| 256 | 2010MB| 1803MB| -10.3%|
| 512 | 3904MB|3483MB| -10.8%|
---------
Signed-off-by: chzhoo <[email protected]>
Co-authored-by: Viktor Söderqvist <[email protected]>1 parent 616fccb commit 33bfac3
15 files changed
+303
-86
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1922 | 1922 | | |
1923 | 1923 | | |
1924 | 1924 | | |
1925 | | - | |
| 1925 | + | |
1926 | 1926 | | |
1927 | 1927 | | |
1928 | 1928 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1054 | 1054 | | |
1055 | 1055 | | |
1056 | 1056 | | |
1057 | | - | |
| 1057 | + | |
1058 | 1058 | | |
1059 | 1059 | | |
1060 | 1060 | | |
| |||
1077 | 1077 | | |
1078 | 1078 | | |
1079 | 1079 | | |
1080 | | - | |
| 1080 | + | |
1081 | 1081 | | |
1082 | 1082 | | |
1083 | 1083 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
| 219 | + | |
219 | 220 | | |
220 | 221 | | |
221 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | | - | |
270 | | - | |
271 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
272 | 272 | | |
273 | 273 | | |
274 | | - | |
275 | 274 | | |
276 | 275 | | |
277 | 276 | | |
| |||
283 | 282 | | |
284 | 283 | | |
285 | 284 | | |
286 | | - | |
| 285 | + | |
287 | 286 | | |
288 | 287 | | |
289 | 288 | | |
| |||
292 | 291 | | |
293 | 292 | | |
294 | 293 | | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
301 | 297 | | |
302 | 298 | | |
303 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
| 325 | + | |
| 326 | + | |
326 | 327 | | |
327 | 328 | | |
328 | 329 | | |
| |||
825 | 826 | | |
826 | 827 | | |
827 | 828 | | |
| 829 | + | |
828 | 830 | | |
829 | 831 | | |
830 | 832 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5165 | 5165 | | |
5166 | 5166 | | |
5167 | 5167 | | |
5168 | | - | |
| 5168 | + | |
| 5169 | + | |
5169 | 5170 | | |
5170 | 5171 | | |
5171 | 5172 | | |
| |||
5222 | 5223 | | |
5223 | 5224 | | |
5224 | 5225 | | |
5225 | | - | |
| 5226 | + | |
5226 | 5227 | | |
5227 | 5228 | | |
5228 | 5229 | | |
| |||
5284 | 5285 | | |
5285 | 5286 | | |
5286 | 5287 | | |
5287 | | - | |
| 5288 | + | |
5288 | 5289 | | |
5289 | 5290 | | |
5290 | 5291 | | |
| |||
11418 | 11419 | | |
11419 | 11420 | | |
11420 | 11421 | | |
11421 | | - | |
| 11422 | + | |
11422 | 11423 | | |
11423 | 11424 | | |
11424 | 11425 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
667 | 667 | | |
668 | 668 | | |
669 | 669 | | |
670 | | - | |
671 | | - | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
672 | 673 | | |
673 | 674 | | |
674 | 675 | | |
| |||
1190 | 1191 | | |
1191 | 1192 | | |
1192 | 1193 | | |
1193 | | - | |
1194 | 1194 | | |
1195 | 1195 | | |
1196 | 1196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
959 | 959 | | |
960 | 960 | | |
961 | 961 | | |
962 | | - | |
| 962 | + | |
| 963 | + | |
963 | 964 | | |
964 | 965 | | |
965 | 966 | | |
| |||
2095 | 2096 | | |
2096 | 2097 | | |
2097 | 2098 | | |
| 2099 | + | |
2098 | 2100 | | |
2099 | 2101 | | |
2100 | 2102 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
561 | 561 | | |
562 | 562 | | |
563 | 563 | | |
564 | | - | |
| 564 | + | |
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
1426 | | - | |
1427 | 1426 | | |
1428 | 1427 | | |
1429 | 1428 | | |
| |||
1434 | 1433 | | |
1435 | 1434 | | |
1436 | 1435 | | |
| 1436 | + | |
1437 | 1437 | | |
1438 | 1438 | | |
1439 | 1439 | | |
| |||
3275 | 3275 | | |
3276 | 3276 | | |
3277 | 3277 | | |
3278 | | - | |
| 3278 | + | |
3279 | 3279 | | |
| 3280 | + | |
3280 | 3281 | | |
3281 | 3282 | | |
3282 | 3283 | | |
| |||
0 commit comments