Skip to content

Commit 4ac906c

Browse files
committed
opt: make the mixed-buffer more flexible
1 parent 4b3679d commit 4ac906c

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

connection_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newTCPConn(fd int, el *eventloop, sa unix.Sockaddr, codec ICodec, localAddr
5858
localAddr: localAddr,
5959
remoteAddr: remoteAddr,
6060
inboundBuffer: rbPool.GetWithSize(ringbuffer.TCPReadBufferSize),
61-
outboundBuffer: mixedbuffer.New(),
61+
outboundBuffer: mixedbuffer.New(mixedbuffer.MaxStackingBytes),
6262
}
6363
c.pollAttachment = netpoll.GetPollAttachment()
6464
c.pollAttachment.FD, c.pollAttachment.Callback = fd, c.handleEvents

eventloop_unix.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ func (el *eventloop) loopWrite(c *conn) error {
155155
el.eventHandler.PreWrite(c)
156156

157157
iov := c.outboundBuffer.Peek()
158-
n, err := io.Writev(c.fd, iov)
158+
var (
159+
n int
160+
err error
161+
)
162+
if len(iov) > 1 {
163+
n, err = io.Writev(c.fd, iov)
164+
} else {
165+
n, err = unix.Write(c.fd, iov[0])
166+
}
159167
c.outboundBuffer.Discard(n)
160168
switch err {
161169
case nil:

pkg/mixedbuffer/mixed_ring_list_buffer.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ const MaxStackingBytes = 32 * 1024 // 32KB
2828
// list-buffer if the data size of ring-buffer reaches the maximum(MaxStackingBytes), list-buffer is more
2929
// flexible and scalable, which helps the application reduce memory footprint.
3030
type Buffer struct {
31-
ringBuffer *ringbuffer.RingBuffer
32-
listBuffer listbuffer.ListBuffer
31+
maxStackingBytes int
32+
ringBuffer *ringbuffer.RingBuffer
33+
listBuffer listbuffer.ListBuffer
3334
}
3435

3536
// New instantiates a mixedbuffer.Buffer and returns it.
36-
func New() *Buffer {
37-
return &Buffer{ringBuffer: rbPool.Get()}
37+
func New(maxTopBufCap int) *Buffer {
38+
return &Buffer{maxStackingBytes: maxTopBufCap, ringBuffer: rbPool.Get()}
3839
}
3940

4041
// Peek returns all bytes as [][]byte, these bytes won't be discarded until Buffer.Discard() is called.
@@ -55,18 +56,22 @@ func (mb *Buffer) Discard(n int) {
5556
}
5657

5758
// Write appends data to this buffer.
58-
func (mb *Buffer) Write(p []byte) (int, error) {
59-
if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Length() >= MaxStackingBytes {
59+
func (mb *Buffer) Write(p []byte) (n int, err error) {
60+
if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Length() >= mb.maxStackingBytes {
6061
mb.listBuffer.PushBytesBack(p)
6162
return len(p), nil
6263
}
63-
return mb.ringBuffer.Write(p)
64+
n, err = mb.ringBuffer.Write(p)
65+
if n > mb.maxStackingBytes {
66+
mb.maxStackingBytes = n
67+
}
68+
return
6469
}
6570

6671
// Writev appends multiple byte slices to this buffer.
6772
func (mb *Buffer) Writev(bs [][]byte) (int, error) {
6873
var n int
69-
if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Length() >= MaxStackingBytes {
74+
if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Length() >= mb.maxStackingBytes {
7075
for _, b := range bs {
7176
mb.listBuffer.PushBytesBack(b)
7277
n += len(b)
@@ -77,6 +82,9 @@ func (mb *Buffer) Writev(bs [][]byte) (int, error) {
7782
_, _ = mb.ringBuffer.Write(b)
7883
n += len(b)
7984
}
85+
if n > mb.maxStackingBytes {
86+
mb.maxStackingBytes = n
87+
}
8088
return n, nil
8189
}
8290

0 commit comments

Comments
 (0)