File tree 1 file changed +11
-6
lines changed
1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change 6
6
)
7
7
8
8
const defaultBufSize = 4096 // must be 2^n
9
- // const maxCachedBufSize = 256 * 1024
9
+ const maxCachedBufSize = 256 * 1024
10
10
11
11
type writeBuffer struct {
12
12
buf []byte
@@ -39,9 +39,10 @@ func (wb *writeBuffer) store(buf []byte) {
39
39
}
40
40
41
41
type readBuffer struct {
42
- buf []byte
43
- idx int
44
- nc net.Conn
42
+ buf []byte
43
+ idx int
44
+ nc net.Conn
45
+ cached []byte
45
46
}
46
47
47
48
func newReadBuffer (nc net.Conn ) readBuffer {
@@ -54,12 +55,16 @@ func newReadBuffer(nc net.Conn) readBuffer {
54
55
// fill reads into the buffer until at least _need_ bytes are in it.
55
56
func (rb * readBuffer ) fill (need int ) error {
56
57
var buf []byte
57
- if need <= cap (rb .buf ) {
58
- buf = rb .buf [:0 ]
58
+ if need <= cap (rb .cached ) {
59
+ buf = rb .cached [:0 ]
59
60
} else {
60
61
// Round up to the next multiple of the default size
61
62
size := (need + defaultBufSize - 1 ) &^ (defaultBufSize - 1 )
63
+
62
64
buf = make ([]byte , 0 , size )
65
+ if size <= maxCachedBufSize {
66
+ rb .cached = buf
67
+ }
63
68
}
64
69
65
70
// move the existing data to the start of it.
You can’t perform that action at this time.
0 commit comments