mirror of
https://github.com/rclone/rclone.git
synced 2025-12-06 00:03:32 +00:00
pool: unify memory between multipart and asyncreader to use one pool
Before this the multipart code and asyncreader used separate pools which is inefficient on memory use.
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/lib/pool"
|
||||
@@ -16,10 +15,8 @@ import (
|
||||
|
||||
const (
|
||||
// BufferSize is the default size of the async buffer
|
||||
BufferSize = 1024 * 1024
|
||||
softStartInitial = 4 * 1024
|
||||
bufferCacheSize = 64 // max number of buffers to keep in cache
|
||||
bufferCacheFlushTime = 5 * time.Second // flush the cached buffers after this long
|
||||
BufferSize = pool.BufferSize
|
||||
softStartInitial = 4 * 1024
|
||||
)
|
||||
|
||||
// ErrorStreamAbandoned is returned when the input is closed before the end of the stream
|
||||
@@ -42,6 +39,7 @@ type AsyncReader struct {
|
||||
closed bool // whether we have closed the underlying stream
|
||||
mu sync.Mutex // lock for Read/WriteTo/Abandon/Close
|
||||
ci *fs.ConfigInfo // for reading config
|
||||
pool *pool.Pool // pool to get memory from
|
||||
}
|
||||
|
||||
// New returns a reader that will asynchronously read from
|
||||
@@ -58,7 +56,8 @@ func New(ctx context.Context, rd io.ReadCloser, buffers int) (*AsyncReader, erro
|
||||
return nil, errors.New("nil reader supplied")
|
||||
}
|
||||
a := &AsyncReader{
|
||||
ci: fs.GetConfig(ctx),
|
||||
ci: fs.GetConfig(ctx),
|
||||
pool: pool.Global(),
|
||||
}
|
||||
a.init(rd, buffers)
|
||||
return a, nil
|
||||
@@ -104,24 +103,16 @@ func (a *AsyncReader) init(rd io.ReadCloser, buffers int) {
|
||||
}()
|
||||
}
|
||||
|
||||
// bufferPool is a global pool of buffers
|
||||
var bufferPool *pool.Pool
|
||||
var bufferPoolOnce sync.Once
|
||||
|
||||
// return the buffer to the pool (clearing it)
|
||||
func (a *AsyncReader) putBuffer(b *buffer) {
|
||||
bufferPool.Put(b.buf)
|
||||
a.pool.Put(b.buf)
|
||||
b.buf = nil
|
||||
}
|
||||
|
||||
// get a buffer from the pool
|
||||
func (a *AsyncReader) getBuffer() *buffer {
|
||||
bufferPoolOnce.Do(func() {
|
||||
// Initialise the buffer pool when used
|
||||
bufferPool = pool.New(bufferCacheFlushTime, BufferSize, bufferCacheSize, a.ci.UseMmap)
|
||||
})
|
||||
return &buffer{
|
||||
buf: bufferPool.Get(),
|
||||
buf: a.pool.Get(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user