Skip to content

Commit

Permalink
feat: optimize listpack structure to enhance lrange perf
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Sep 28, 2024
1 parent 5a5e387 commit b59ab85
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine AS builder
FROM golang:1.23-alpine AS builder

LABEL stage=gobuilder \
mainatiner=https://github.com/xgzlucario/rotom
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ In rotom, the AeLoop event loop mechanism in Redis is replicated, specifically:

Rotom has made several optimizations in data structures:

- dict: Rotom uses `swissmap` as the db hash table, which has better performance and memory efficiency than `stdmap`.
- dict: Rotom uses `stdmap` as the db hash table, with built-in progressive rehashing.
- hash: Uses `zipmap` when the hash is small and `hashmap` when it is large.
- set: Uses `zipset` when the set is small and `mapset` when it is large.
- list: Uses a `quicklist` based on `listpack` for a doubly linked list.
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AeLoop(Async Event Loop) 是 Redis 的核心异步事件驱动机制,主要有

rotom 在数据结构上做了许多优化:

- dict:rotom 使用 `swissmap` 作为 db 的哈希表,它的性能和内存使用效率都比 `stdmap` 更好
- dict:rotom 使用 `stdmap` 作为 db 的哈希表,自带渐进式 rehash 功能
- hash:当 hash 较小时使用 `zipmap`,较大时使用 `hashmap`
- set:当 set 较小时使用 `zipset`,较大时使用 `mapset`
- list:使用基于 `listpack` 的双向链表 `quicklist`
Expand Down
2 changes: 1 addition & 1 deletion bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ IMAGE_NAME=$TEST_NAME

OUTPUT_FILE="output/$TEST_NAME"

COMMANDS="set,get,incr,lpush,rpush,hset,sadd,zadd"
COMMANDS="set,get,incr,lpush,rpush,lrange,hset,sadd,zadd"

PIPELINES=(1 10 50)

Expand Down
4 changes: 3 additions & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func startup() {
config4Server(config)
printBanner(config)
server.aeLoop.AddRead(server.fd, AcceptHandler, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 1000, SysMonitor, nil)
// custom
server.aeLoop.AddTimeEvent(AE_ONCE, 300, func(loop *AeLoop, id int, extra interface{}) {}, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 1000, CronSyncAOF, nil)
server.aeLoop.AeMain()
}

Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Config struct {
Port int `json:"port"`
AppendOnly bool `json:"appendonly"`
AppendFileName string `json:"appendfilename"`
MaxMemory int `json:"maxMemory"`
}

func LoadConfig(path string) (config *Config, err error) {
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/xgzlucario/rotom

go 1.22
go 1.23

require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/chen3feng/stl4go v0.1.1
github.com/deckarep/golang-set/v2 v2.6.0
github.com/influxdata/tdigest v0.0.1
github.com/redis/go-redis/v9 v9.5.2
github.com/redis/go-redis/v9 v9.6.1
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/mmap v0.3.0
Expand All @@ -16,18 +16,18 @@ require (
)

require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
33 changes: 16 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chen3feng/stl4go v0.1.1 h1:0L1+mDw7pomftKDruM23f1mA7miavOj6C6MZeadzN2Q=
github.com/chen3feng/stl4go v0.1.1/go.mod h1:5ml3psLgETJjRJnMbPE+JiHLrCpt+Ajc2weeTECXzWU=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
Expand All @@ -26,9 +26,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY=
github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -39,14 +39,15 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.5.2 h1:L0L3fcSNReTRGyZ6AqAEN0K56wYeYAwapBIhkvh0f3E=
github.com/redis/go-redis/v9 v9.5.2/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand All @@ -57,8 +58,8 @@ github.com/tidwall/mmap v0.3.0/go.mod h1:2/dNzF5zA+te/JVHfrqNLcRkb8LjdH3c80vYHFQ
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -70,9 +71,7 @@ gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca h1:PupagGYwj8+I4ubCxcmcBRk
gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
gonum.org/v1/netlib v0.0.0-20181029234149-ec6d1f5cefe6/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38 changes: 17 additions & 21 deletions internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ var bpool = pool.NewBufferPool()
|
entry0 content:
+------------+--------------+---------------------+
| data_len | data | entry_len |
| data_len | data | data_len |
+------------+--------------+---------------------+
|<- varint ->|<- data_len ->|<- varint(reverse) ->|
|<------- entry_len ------->|
Using this structure, it is fast to iterate from both sides.
*/
Expand Down Expand Up @@ -97,15 +96,15 @@ func (it *LpIterator) IsLast() bool { return it.index == len(it.data) }

func (it *LpIterator) Next() []byte {
//
// index dataStartPos dataEndPos indexNext
// | | | |
// +------------+--------------+---------------------+-----+
// --> | data_len | data | entry_len | ... |
// +------------+--------------+---------------------+-----+
// |<--- n ---->|<- data_len ->|<-- size_entry_len ->|
// index dataStartPos dataEndPos indexNext
// | | | |
// +------------+--------------+------------+
// --> | data_len | data | data_len |
// +------------+--------------+------------+
// |<--- n ---->|<- data_len ->|<---- n --->|
//
dataLen, n := binary.Uvarint(it.data[it.index:])
indexNext := it.index + n + int(dataLen) + SizeUvarint(dataLen+uint64(n))
indexNext := it.index + n + int(dataLen) + n

dataStartPos := it.index + n
dataEndPos := dataStartPos + int(dataLen)
Expand All @@ -118,18 +117,16 @@ func (it *LpIterator) Next() []byte {

func (it *LpIterator) Prev() []byte {
//
// indexNext dataStartPos dataEndPos index
// | | | |
// +-----+------------+--------------+---------------------+
// | ... | data_len | data | entry_len | <--
// +-----+------------+--------------+---------------------+
// |<--- n ---->|<- data_len ->|<-- size_entry_len ->|
// |<------ entry_len -------->|
// indexNext dataStartPos dataEndPos index
// | | | |
// +-----+------------+--------------+------------+
// | ... | data_len | data | data_len | <--
// +-----+------------+--------------+------------+
// |<--- n ---->|<- data_len ->|<---- n --->|
//
entryLen, sizeEntryLen := uvarintReverse(it.data[:it.index])
indexNext := it.index - int(entryLen) - sizeEntryLen
dataLen, n := uvarintReverse(it.data[:it.index])
indexNext := it.index - n - int(dataLen) - n

dataLen, n := binary.Uvarint(it.data[indexNext:])
dataStartPos := indexNext + n
dataEndPos := dataStartPos + int(dataLen)

Expand Down Expand Up @@ -196,8 +193,7 @@ func appendEntry(dst []byte, data string) []byte {
sz := len(data) + 2*SizeUvarint(uint64(len(data)))
dst = bpool.Get(sz)[:0]
}
before := len(dst)
dst = appendUvarint(dst, len(data), false)
dst = append(dst, data...)
return appendUvarint(dst, len(dst)-before, true)
return appendUvarint(dst, len(data), true)
}
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ func main() {

// register main aeLoop event
server.aeLoop.AddRead(server.fd, AcceptHandler, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 100, EvictExpired, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 60*1000, SysMonitor, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 100, CronEvictExpired, nil)
if server.config.AppendOnly {
server.aeLoop.AddTimeEvent(AE_NORMAL, 1000, SyncAOF, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 1000, CronSyncAOF, nil)
}
server.aeLoop.AeMain()
}
37 changes: 2 additions & 35 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package main
import (
"fmt"
"io"
"runtime"
"runtime/debug"
"time"

"github.com/xgzlucario/rotom/internal/dict"
"github.com/xgzlucario/rotom/internal/hash"
Expand Down Expand Up @@ -235,46 +232,16 @@ func initServer(config *Config) (err error) {
return nil
}

func SyncAOF(loop *AeLoop, id int, extra interface{}) {
func CronSyncAOF(loop *AeLoop, id int, extra interface{}) {
if err := db.aof.Flush(); err != nil {
log.Error().Msgf("sync aof error: %v", err)
}
}

func EvictExpired(loop *AeLoop, id int, extra interface{}) {
func CronEvictExpired(loop *AeLoop, id int, extra interface{}) {
db.dict.EvictExpired()
}

func SysMonitor(loop *AeLoop, id int, extra interface{}) {
var mem runtime.MemStats
var stat debug.GCStats

runtime.ReadMemStats(&mem)
debug.ReadGCStats(&stat)

var pause time.Duration
if stat.NumGC > 0 {
pause = stat.PauseTotal / time.Duration(stat.NumGC)
}

log.Info().
Str("gcsys", readableSize(mem.GCSys)).
Str("heapInuse", readableSize(mem.HeapInuse)).
Str("heapObjects", fmt.Sprintf("%.1fk", float64(mem.HeapObjects)/1e3)).
Str("pause", fmt.Sprintf("%v", pause)).
Int64("gc", stat.NumGC).
Msgf("[SYS]")

// check outOfMemory
if server.config.MaxMemory == 0 {
return
}
if server.outOfMemory {
runtime.GC()
}
server.outOfMemory = int(mem.HeapAlloc) > server.config.MaxMemory
}

func readableSize[T int | uint64](sz T) string {
switch {
case sz >= GB:
Expand Down

0 comments on commit b59ab85

Please sign in to comment.