Skip to content

Commit 3d9f7c1

Browse files
authored
Merge pull request #295 from zifter/bugfix/fix-too-much-memory-allocations
#233 fixed: leakypool occupy too much memory.
2 parents 640426b + f7b3ae4 commit 3d9f7c1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/writebuffer/buffer.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ func (wb *WriteBuffer) WriteTo(w io.Writer) (int64, error) {
4141
for _, chunk := range wb.chunks {
4242
ln, err := w.Write(chunk)
4343
if err != nil {
44-
wb.Reset()
44+
wb.clear()
4545
return 0, err
4646
}
4747
size += int64(ln)
4848
}
49-
wb.Reset()
49+
wb.clear()
5050
return size, nil
5151
}
5252

@@ -86,7 +86,7 @@ func (wb *WriteBuffer) calcCap(dataSize int) int {
8686
return max(dataSize, cap(wb.chunks[len(wb.chunks)-1])*2)
8787
}
8888

89-
func (wb *WriteBuffer) Reset() {
89+
func (wb *WriteBuffer) clear() {
9090
if len(wb.chunks) == 0 {
9191
return
9292
}
@@ -105,6 +105,13 @@ func (wb *WriteBuffer) Reset() {
105105
wb.chunks = wb.chunks[:1]
106106
}
107107

108+
func (wb *WriteBuffer) Reset() {
109+
for _, chunk := range wb.chunks {
110+
leakypool.PutBytes(chunk[:0])
111+
}
112+
wb.chunks = nil
113+
}
114+
108115
func max(a, b int) int {
109116
if b > a {
110117
return b

0 commit comments

Comments
 (0)