Skip to content

Commit

Permalink
Reuse connection output page
Browse files Browse the repository at this point in the history
Avoid the extra allocations for every Data event by hanging onto
the existing connection page when possible.
  • Loading branch information
tidwall committed Jan 7, 2020
1 parent ef80c9b commit 99e633d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions evio_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func loopAccept(s *server, l *loop, fd int) error {
return err
}
c := &conn{fd: nfd, sa: sa, lnidx: i, loop: l}
c.out = make([]byte, 0, 4096)
l.fdconns[c.fd] = c
l.poll.AddReadWrite(c.fd)
atomic.AddInt32(&l.count, 1)
Expand Down Expand Up @@ -377,7 +378,13 @@ func loopWrite(s *server, l *loop, c *conn) error {
return loopCloseConn(s, l, c, err)
}
if n == len(c.out) {
c.out = nil
// release the connection output page if it goes over page size,
// otherwise keep reusing existing page.
if cap(c.out) > 4096 {
c.out = make([]byte, 0, 4096)
} else {
c.out = c.out[:0]
}
} else {
c.out = c.out[n:]
}
Expand Down Expand Up @@ -436,7 +443,7 @@ func loopRead(s *server, l *loop, c *conn) error {
out, action := s.events.Data(c, in)
c.action = action
if len(out) > 0 {
c.out = append([]byte{}, out...)
c.out = append(c.out[:0], out...)
}
}
if len(c.out) != 0 || c.action != None {
Expand Down

0 comments on commit 99e633d

Please sign in to comment.