Skip to content

Commit

Permalink
Remove unnecessary allocations and Write calls.
Browse files Browse the repository at this point in the history
This commit improves networking performance by 1) moving an unnecessary
allocation to outside the hot loop and by 2) eliminating an unnecessary
Write call.

This result in ~7% faster throughput as measured via iperf3 and a custom
Web service to measure requests per second.  Note that this is *despite*
the append call.

Signed-off-by: Philipp Winter <phw@brave.com>
  • Loading branch information
Philipp Winter committed May 10, 2023
1 parent ee06501 commit 4f4385e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
11 changes: 3 additions & 8 deletions cmd/vm/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func dhcp() error {

func rx(conn net.Conn, tap *water.Interface, errCh chan error, mtu int) {
log.Info("waiting for packets...")
size := make([]byte, 2)
var frame ethernet.Frame
for {
frame.Resize(mtu)
Expand All @@ -167,15 +168,9 @@ func rx(conn net.Conn, tap *water.Interface, errCh chan error, mtu int) {
log.Info(packet.String())
}

size := make([]byte, 2)
binary.LittleEndian.PutUint16(size, uint16(n))

if _, err := conn.Write(size); err != nil {
errCh <- errors.Wrap(err, "cannot write size to socket")
return
}
if _, err := conn.Write(frame); err != nil {
errCh <- errors.Wrap(err, "cannot write packet to socket")
if _, err := conn.Write(append(size, frame...)); err != nil {
errCh <- errors.Wrap(err, "cannot write size and packet to socket")
return
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/tap/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ func (e *Switch) txBuf(id int, conn protocolConn, buf []byte) error {
size := conn.protocolImpl.(streamProtocol).Buf()
conn.protocolImpl.(streamProtocol).Write(size, len(buf))

if _, err := conn.Write(size); err != nil {
if _, err := conn.Write(append(size, buf...)); err != nil {
e.disconnect(id, conn)
return err
}
} else {
if _, err := conn.Write(buf); err != nil {
e.disconnect(id, conn)
return err
}
}
if _, err := conn.Write(buf); err != nil {
e.disconnect(id, conn)
return err
}
return nil
}
Expand Down

0 comments on commit 4f4385e

Please sign in to comment.