Skip to content

Commit

Permalink
Use byte array on stack for copying data, limit size hint
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Oct 3, 2024
1 parent e6af09b commit b2a1909
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions io/fs/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,9 @@ func (fs *memFilesystem) Symlink(oldname, newname string) error {
return nil
}

var chunkPool = sync.Pool{
New: func() interface{} {
chunk := make([]byte, 128*1024)
return &chunk
},
}

func copyToBufferFromReader(buf *bytes.Buffer, r io.Reader, _ int) (int64, error) {
chunkPtr := chunkPool.Get().(*[]byte)
chunk := *chunkPtr
defer chunkPool.Put(chunkPtr)
chunkData := [128 * 1024]byte{}
chunk := chunkData[0:]

size := int64(0)

Expand Down Expand Up @@ -466,7 +458,7 @@ func (fs *memFilesystem) WriteFileReader(path string, r io.Reader, sizeHint int)
data: &bytes.Buffer{},
}

if sizeHint > 0 {
if sizeHint > 0 && sizeHint < 5*1024*1024 {
newFile.data.Grow(sizeHint)
}

Expand Down

0 comments on commit b2a1909

Please sign in to comment.