Skip to content

Commit

Permalink
Optimize write with Redis Set cmd if whole buffer is to be written (f…
Browse files Browse the repository at this point in the history
…aster)
  • Loading branch information
JCapul committed Apr 15, 2019
1 parent 922fcff commit bd34304
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/go/redisfs/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,22 @@ func string2byteNoCopy(s string) []byte {

//WriteAt writes data to the Buffer starting at byte offset off.
func (b *RedisBuffer) WriteAt(data []byte, off int64) (int, error) {
try(b.redis.SetRange(b.key, off, byte2StringNoCopy(data)).Err())
if off == 0 && len(data) == b.bufSize {
// if the whole buffer is to be written, Set is faster than SetRange
try(b.redis.Set(b.key, byte2StringNoCopy(data), 0).Err())
} else {
try(b.redis.SetRange(b.key, off, byte2StringNoCopy(data)).Err())
}
return len(data), nil
}

//WriteVecAt writes a vector of byte slices to the Buffer starting at byte offset off.
func (b *RedisBuffer) WriteVecAt(datav [][]byte, off int64) (int, error) {
var n int
for _, data := range datav {
try(b.redis.SetRange(b.key, off, byte2StringNoCopy(data)).Err())
off += int64(len(data))
n += len(data)
l, _ := b.WriteAt(data, off)
off += int64(l)
n += l
}
return n, nil
}
Expand Down

0 comments on commit bd34304

Please sign in to comment.