Skip to content

Commit

Permalink
perf: optimize listpack insert perf
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 27, 2024
1 parent 56ab942 commit 7d9a821
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ Ensure your local Go environment is >= `1.22`. In the project directory, run `go

```
$ go run .
2024-07-18 23:37:13 INF current version buildTime=240718_233649+0800
2024-07-18 23:37:13 INF read cmd arguments config=/etc/rotom/config.json debug=false
2024-07-18 23:37:13 INF running on port=6379
2024-07-18 23:37:13 DBG
________ _____
___ __ \_______ /_____________ ___ Rotom 64 bit (amd64/linux)
__ /_/ / __ \ __/ __ \_ __ '__ \ Port: 6379, Pid: 15817
_ _, _// /_/ / /_ / /_/ / / / / / / Build:
/_/ |_| \____/\__/ \____//_/ /_/ /_/
2024-07-18 23:37:13 INF read config file config=config.json
2024-07-18 23:37:13 INF rotom server is ready to accept.
```

Expand Down
11 changes: 8 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ git clone https://github.com/xgzlucario/rotom

```
$ go run .
2024-07-18 23:37:13 INF current version buildTime=240718_233649+0800
2024-07-18 23:37:13 INF read cmd arguments config=/etc/rotom/config.json debug=false
2024-07-18 23:37:13 INF running on port=6379
2024-07-18 23:37:13 DBG
________ _____
___ __ \_______ /_____________ ___ Rotom 64 bit (amd64/linux)
__ /_/ / __ \ __/ __ \_ __ '__ \ Port: 6379, Pid: 15817
_ _, _// /_/ / /_ / /_/ / / / / / / Build:
/_/ |_| \____/\__/ \____//_/ /_/ /_/
2024-07-18 23:37:13 INF read config file config=config.json
2024-07-18 23:37:13 INF rotom server is ready to accept.
```

Expand Down
1 change: 1 addition & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func startup() {
}
os.Remove(config.AppendFileName)
config4Server(config)
printBanner(config)
server.aeLoop.AddRead(server.fd, AcceptHandler, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 500, CheckOutOfMemory, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 1000, SysMonitor, nil)
Expand Down
12 changes: 11 additions & 1 deletion internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ func (it *LpIterator) Prev() []byte {
}

func (it *LpIterator) Insert(datas ...string) {
// fast insert to tail
if it.IsLast() {
for _, data := range datas {
it.data = appendEntry(it.data, data)
it.size++
}
return
}

var alloc []byte
for _, data := range datas {
alloc = appendEntry(alloc, data)
Expand Down Expand Up @@ -186,7 +195,8 @@ func (it *LpIterator) ReplaceNext(key string) {

func appendEntry(dst []byte, data string) []byte {
if dst == nil {
dst = bpool.Get(len(data) + 10)[:0]
sz := len(data) + 2*SizeUvarint(uint64(len(data)))
dst = bpool.Get(sz)[:0]
}
before := len(dst)
dst = appendUvarint(dst, len(data), false)
Expand Down
25 changes: 20 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"strconv"
"time"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -33,6 +35,19 @@ func config4Server(config *Config) {
}
}

func printBanner(config *Config) {
log.Printf(`
________ _____
___ __ \_______ /_____________ ___ Rotom %d bit (%s/%s)
__ /_/ / __ \ __/ __ \_ __ '__ \ Port: %d, Pid: %d
_ _, _// /_/ / /_ / /_/ / / / / / / Build: %s
/_/ |_| \____/\__/ \____//_/ /_/ /_/
`,
strconv.IntSize, runtime.GOARCH, runtime.GOOS,
config.Port, os.Getpid(),
buildTime)
}

func main() {
var path string
var debug bool
Expand All @@ -41,19 +56,19 @@ func main() {
flag.BoolVar(&debug, "debug", false, "run with debug mode.")
flag.Parse()

log.Info().Str("buildTime", buildTime).Msg("current version")
log.Info().Str("config", path).Bool("debug", debug).Msg("read cmd arguments")

config, err := LoadConfig(path)
if err != nil {
log.Fatal().Msgf("load config error: %v", err)
}
config4Server(config)
printBanner(config)

if debug {
go http.ListenAndServe(":6060", nil)
}

log.Info().Int("port", config.Port).Msg("running on")
log.Info().Str("config", path).Msg("read config file")
config4Server(config)

log.Info().Msg("rotom server is ready to accept.")

// register main aeLoop event
Expand Down

0 comments on commit 7d9a821

Please sign in to comment.