Commit 4e15d38b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 511c3c3b
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
// See https://www.nexedi.com/licensing for rationale and options. // See https://www.nexedi.com/licensing for rationale and options.
package xbufio package xbufio
// bufferring for io.ReaderAt optimized for sequential access // buffering for io.ReaderAt optimized for sequential access
import ( import (
"io" "io"
...@@ -49,13 +49,14 @@ type SeqReaderAt struct { ...@@ -49,13 +49,14 @@ type SeqReaderAt struct {
//posLastIO int64 //posLastIO int64
} }
// TODO text about syscall / memcpy etc const defaultSeqBufSize = 8192
const defaultSeqBufSize = 8192 // XXX retune - must be <= size(L1d) / 2
// NewSeqReaderAt wraps r with SeqReaderAt with buffer of default size.
func NewSeqReaderAt(r io.ReaderAt) *SeqReaderAt { func NewSeqReaderAt(r io.ReaderAt) *SeqReaderAt {
return NewSeqReaderAtSize(r, defaultSeqBufSize) return NewSeqReaderAtSize(r, defaultSeqBufSize)
} }
// NewSeqReaderAtSize wraps r with SeqReaderAt with buffer of specified size.
func NewSeqReaderAtSize(r io.ReaderAt, size int) *SeqReaderAt { func NewSeqReaderAtSize(r io.ReaderAt, size int) *SeqReaderAt {
sb := &SeqReaderAt{r: r, buf: make([]byte, 0, size)} // all positions are zero initially sb := &SeqReaderAt{r: r, buf: make([]byte, 0, size)} // all positions are zero initially
return sb return sb
...@@ -171,7 +172,7 @@ func (sb *SeqReaderAt) ReadAt(p []byte, pos int64) (int, error) { ...@@ -171,7 +172,7 @@ func (sb *SeqReaderAt) ReadAt(p []byte, pos int64) (int, error) {
// backward // backward
xpos = pos xpos = pos
// if backward trend continues and bufferring would overlap with // if backward trend continues and buffering would overlap with
// previous backward access - shift reading up right to it. // previous backward access - shift reading up right to it.
xLastBackward := posLastBackward - int64(ntail) // adjusted for already read from buffer xLastBackward := posLastBackward - int64(ntail) // adjusted for already read from buffer
if xpos < xLastBackward && xLastBackward < xpos + cap64(sb.buf) { if xpos < xLastBackward && xLastBackward < xpos + cap64(sb.buf) {
...@@ -210,7 +211,7 @@ func (sb *SeqReaderAt) ReadAt(p []byte, pos int64) (int, error) { ...@@ -210,7 +211,7 @@ func (sb *SeqReaderAt) ReadAt(p []byte, pos int64) (int, error) {
if pBufOffset >= len64(sb.buf) { if pBufOffset >= len64(sb.buf) {
// this can be only due to some IO error // this can be only due to some IO error
// if original requst was narrower than buffer try to satisfy // if original request was narrower than buffer try to satisfy
// it once again directly // it once again directly
if pos != xpos { if pos != xpos {
nn, err = sb.ioReadAt(p, pos) nn, err = sb.ioReadAt(p, pos)
......
...@@ -27,8 +27,8 @@ import ( ...@@ -27,8 +27,8 @@ import (
) )
// XReader is an io.ReaderAt that reads first 256 bytes with content_i = i // XReader is an io.ReaderAt that reads first 256 bytes with content_i = i.
// bytes in range [100, 104] give EIO on reading // bytes in range [100, 104] give EIO on reading.
type XReader struct { type XReader struct {
} }
...@@ -56,8 +56,6 @@ func (r *XReader) ReadAt(p []byte, pos int64) (n int, err error) { ...@@ -56,8 +56,6 @@ func (r *XReader) ReadAt(p []byte, pos int64) (n int, err error) {
// read @pos/len -> rb.pos, len(rb.buf) // read @pos/len -> rb.pos, len(rb.buf)
var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} { var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} {
// TODO add trend / not trend everywhere
// TODO review
{40, 5, 40, 10}, // 1st access, forward by default {40, 5, 40, 10}, // 1st access, forward by default
{45, 7, 50, 10}, // part taken from buf, part read next, forward (trend) {45, 7, 50, 10}, // part taken from buf, part read next, forward (trend)
{52, 5, 50, 10}, // everything taken from buf {52, 5, 50, 10}, // everything taken from buf
...@@ -96,7 +94,7 @@ var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} { ...@@ -96,7 +94,7 @@ var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} {
{188, 2, 184, 10}, {188, 2, 184, 10},
{190, 3, 184, 10}, {190, 3, 184, 10},
{182, 4, 174, 10}, // trendy backward access - part taken from buffer and buf refilled adjacet to previous backward IO {182, 4, 174, 10}, // trendy backward access - part taken from buffer and buf refilled adjacent to previous backward IO
{168, 1, 168, 10}, // trendy backward access farther than cap(buf) - buf refilled right at @pos {168, 1, 168, 10}, // trendy backward access farther than cap(buf) - buf refilled right at @pos
...@@ -129,7 +127,7 @@ var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} { ...@@ -129,7 +127,7 @@ var xSeqBufTestv = []struct {pos int64; Len int; bufPos int64; bufLen int} {
{245, 5, 240, 10}, // backward @245 (4) {245, 5, 240, 10}, // backward @245 (4)
{5, 4, 5, 10}, // forward near file start {5, 4, 5, 10}, // forward near file start
{2, 3, 0, 10}, // backward: buf does not go beyong 0 {2, 3, 0, 10}, // backward: buf does not go beyond 0
{40, 0, 0, 10}, // zero-sized out-of-buffer read do not change buffer {40, 0, 0, 10}, // zero-sized out-of-buffer read do not change buffer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment