diff --git a/common/redisclient.go b/common/redisclient.go index 4f977db..3149259 100644 --- a/common/redisclient.go +++ b/common/redisclient.go @@ -144,14 +144,23 @@ func (client *RedisClient) readBulkString(data *[]byte) (size int, err error) { return } +var dataPool = sync.Pool{ + New: func() interface{} { + return make([]byte, 8*1024) + }, +} + func (client *RedisClient) QueryCuboid(cuboid Cuboid, fn func(*Block)) (count int, err error) { p1 := CoordToPlain(cuboid.P1) p2 := CoordToPlain(cuboid.P2) if err = client.writeHSpatial(p1, p2); err != nil { return } + + data := dataPool.Get().([]byte) + defer dataPool.Put(data[:0]) + var ( - data = make([]byte, 8*1024) block = Block{} size int key int64 @@ -164,14 +173,14 @@ func (client *RedisClient) QueryCuboid(cuboid Cuboid, fn func(*Block)) (count in if size <= 0 { break } - if key, err = DecodeStringFromBytes(data[0:size]); err != nil { + if key, err = DecodeStringFromBytes(data[:size]); err != nil { return } block.Coord = PlainToCoord(key) if size, err = client.readBulkString(&data); err != nil || size < 0 { return } - block.Data = data[0:size] + block.Data = data[:size] fn(&block) }