mirror of https://github.com/ledisdb/ledisdb.git
use arena to reduce object create
This commit is contained in:
parent
d7c59f337d
commit
796f4b3af2
|
@ -14,41 +14,45 @@
|
|||
"Comment": "data/v1-228-g8fb50d5",
|
||||
"Rev": "8fb50d5ee57110936b904a7539d4c5f2bf2359db"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/arena",
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/bson",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/filelock",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/hack",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/ioutil2",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/log",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/num",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/snappy",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/go/sync2",
|
||||
"Rev": "c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1"
|
||||
"Rev": "ecf49fc0738105e87d20e29aa82c403b666ff0b4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/siddontang/goleveldb/leveldb",
|
||||
"Rev": "71404b29ccd98b94ec2278afa806d59a11cd0d28"
|
||||
"Rev": "c1f6d721561c48f467b26a277741e55fd224df1e"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/szferi/gomdb",
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/arena"
|
||||
"github.com/siddontang/ledisdb/server"
|
||||
"net"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -37,12 +37,12 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
var m = map[string][]byte{}
|
||||
var mu sync.Mutex
|
||||
|
||||
func run(c net.Conn) {
|
||||
//buf := make([]byte, 10240)
|
||||
ok := []byte("+OK\r\n")
|
||||
data := []byte("$4096\r\n")
|
||||
data = append(data, make([]byte, 4096)...)
|
||||
data = append(data, "\r\n"...)
|
||||
|
||||
var rt time.Duration
|
||||
var wt time.Duration
|
||||
|
@ -50,10 +50,14 @@ func run(c net.Conn) {
|
|||
rb := bufio.NewReaderSize(c, 10240)
|
||||
wb := bufio.NewWriterSize(c, 10240)
|
||||
|
||||
a := arena.NewArena(10240)
|
||||
|
||||
for {
|
||||
t1 := time.Now()
|
||||
|
||||
req, err := server.ReadRequest(rb)
|
||||
a.Reset()
|
||||
|
||||
req, err := server.ReadRequest(rb, a)
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
|
@ -65,21 +69,9 @@ func run(c net.Conn) {
|
|||
cmd := string(bytes.ToUpper(req[0]))
|
||||
switch cmd {
|
||||
case "SET":
|
||||
mu.Lock()
|
||||
m[string(req[1])] = req[2]
|
||||
mu.Unlock()
|
||||
wb.Write(ok)
|
||||
case "GET":
|
||||
mu.Lock()
|
||||
v := m[string(req[1])]
|
||||
mu.Unlock()
|
||||
if v == nil {
|
||||
wb.WriteString("$-1\r\n")
|
||||
} else {
|
||||
wb.WriteString(fmt.Sprintf("$%d\r\n", len(v)))
|
||||
wb.Write(v)
|
||||
wb.WriteString("\r\n")
|
||||
}
|
||||
wb.Write(data)
|
||||
default:
|
||||
wb.WriteString(fmt.Sprintf("-Err %s Not Supported Now", req[0]))
|
||||
}
|
||||
|
|
|
@ -108,17 +108,18 @@ func (c *client) perform() {
|
|||
}
|
||||
|
||||
if err == nil {
|
||||
go func() {
|
||||
c.reqErr <- exeCmd(c)
|
||||
}()
|
||||
// go func() {
|
||||
// c.reqErr <- exeCmd(c)
|
||||
// }()
|
||||
|
||||
err = <-c.reqErr
|
||||
// err = <-c.reqErr
|
||||
err = exeCmd(c)
|
||||
}
|
||||
}
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
if c.app.access != nil {
|
||||
duration := time.Since(start)
|
||||
|
||||
fullCmd := c.catGenericCommand()
|
||||
cost := duration.Nanoseconds() / 1000000
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package server
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/siddontang/go/arena"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/go/log"
|
||||
"github.com/siddontang/go/num"
|
||||
|
@ -21,6 +22,8 @@ type respClient struct {
|
|||
|
||||
conn net.Conn
|
||||
rb *bufio.Reader
|
||||
|
||||
ar *arena.Arena
|
||||
}
|
||||
|
||||
type respWriter struct {
|
||||
|
@ -43,6 +46,9 @@ func newClientRESP(conn net.Conn, app *App) {
|
|||
c.resp = newWriterRESP(conn, app.cfg.ConnWriteBufferSize)
|
||||
c.remoteAddr = conn.RemoteAddr().String()
|
||||
|
||||
//maybe another config?
|
||||
c.ar = arena.NewArena(app.cfg.ConnReadBufferSize)
|
||||
|
||||
go c.run()
|
||||
}
|
||||
|
||||
|
@ -71,18 +77,31 @@ func (c *respClient) run() {
|
|||
c.app.removeSlave(c.client, handleQuit)
|
||||
}()
|
||||
|
||||
// done := make(chan error)
|
||||
for {
|
||||
// go func() {
|
||||
reqData, err := c.readRequest()
|
||||
if err != nil {
|
||||
// done <- err
|
||||
return
|
||||
}
|
||||
|
||||
c.handleRequest(reqData)
|
||||
// done <- nil
|
||||
// }()
|
||||
|
||||
// err := <-done
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// if c.conn == nil {
|
||||
// return
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
func (c *respClient) readRequest() ([][]byte, error) {
|
||||
return ReadRequest(c.rb)
|
||||
return ReadRequest(c.rb, c.ar)
|
||||
}
|
||||
|
||||
func (c *respClient) handleRequest(reqData [][]byte) {
|
||||
|
@ -103,6 +122,11 @@ func (c *respClient) handleRequest(reqData [][]byte) {
|
|||
|
||||
c.perform()
|
||||
|
||||
c.cmd = ""
|
||||
c.args = nil
|
||||
|
||||
c.ar.Reset()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/arena"
|
||||
"io"
|
||||
)
|
||||
|
||||
|
@ -25,7 +26,7 @@ func ReadLine(rb *bufio.Reader) ([]byte, error) {
|
|||
return p[:i], nil
|
||||
}
|
||||
|
||||
func readBytes(br *bufio.Reader) (bytes []byte, err error) {
|
||||
func readBytes(br *bufio.Reader, a *arena.Arena) (bytes []byte, err error) {
|
||||
size, err := readLong(br)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -37,7 +38,7 @@ func readBytes(br *bufio.Reader) (bytes []byte, err error) {
|
|||
return nil, errors.New("Invalid size: " + fmt.Sprint("%d", size))
|
||||
}
|
||||
|
||||
buf := make([]byte, size+2)
|
||||
buf := a.Make(int(size) + 2)
|
||||
if _, err = io.ReadFull(br, buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -90,7 +91,7 @@ func readLong(in *bufio.Reader) (result int64, err error) {
|
|||
return -1, err
|
||||
}
|
||||
|
||||
func ReadRequest(in *bufio.Reader) ([][]byte, error) {
|
||||
func ReadRequest(in *bufio.Reader, a *arena.Arena) ([][]byte, error) {
|
||||
code, err := in.ReadByte()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -115,7 +116,7 @@ func ReadRequest(in *bufio.Reader) ([][]byte, error) {
|
|||
return nil, errReadRequest
|
||||
}
|
||||
|
||||
if req[i], err = readBytes(in); err != nil {
|
||||
if req[i], err = readBytes(in, a); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue