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 performance by 1) moving an unnecessary allocation
to outside the hot loop and by 2) eliminating an unnecessary Write call.
  • Loading branch information
Philipp Winter committed May 8, 2023
1 parent ee06501 commit 1fb8057
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 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
20 changes: 13 additions & 7 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 Expand Up @@ -223,6 +224,9 @@ loop:
func (e *Switch) rxStream(ctx context.Context, id int, conn net.Conn, sProtocol streamProtocol) error {
reader := bufio.NewReader(conn)
sizeBuf := sProtocol.Buf()
// start with a 65k receive buffer and extend if needed
buf := make([]byte, 1<<16-1)

loop:
for {
select {
Expand All @@ -237,8 +241,10 @@ loop:
}
size := sProtocol.Read(sizeBuf)

buf := make([]byte, size)
_, err = io.ReadFull(reader, buf)
if size > len(buf) {
buf = make([]byte, size)
}
_, err = io.ReadFull(reader, buf[:size])
if err != nil {
return errors.Wrap(err, "cannot read packet from socket")
}
Expand Down

0 comments on commit 1fb8057

Please sign in to comment.