From ab2bfad8c1d09c62beb655f2f2ba61c5e76538d2 Mon Sep 17 00:00:00 2001 From: Donatas Puidokas Date: Thu, 24 Nov 2022 12:13:52 +0200 Subject: [PATCH 1/3] Removing append to optimize memory usage --- wormhole/transit.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wormhole/transit.go b/wormhole/transit.go index 065e1548..cbb0f407 100644 --- a/wormhole/transit.go +++ b/wormhole/transit.go @@ -94,7 +94,11 @@ type wsConnection struct { func (self *wsConnection) writeMsg(msg []byte) error { l := make([]byte, 4) binary.BigEndian.PutUint32(l, uint32(len(msg))) - return self.conn.Write(self.ctx, websocket.MessageBinary, append(l, msg...)) + err := self.conn.Write(self.ctx, websocket.MessageBinary, l) + if err != nil { + return err + } + return self.conn.Write(self.ctx, websocket.MessageBinary, msg) } func (self *wsConnection) writeHandshakeMsg(msg []byte) error { @@ -366,17 +370,17 @@ func (d *transportCryptorClassic) writeRecord(msg []byte) error { binary.BigEndian.PutUint64(nonce[crypto.NonceSize-8:], d.nextWriteNonce) d.nextWriteNonce++ - sealedMsg := secretbox.Seal(nil, msg, &nonce, &d.writeKey) + sealedMsg := secretbox.Seal(nonce[:], msg, &nonce, &d.writeKey) - nonceAndSealedMsg := append(nonce[:], sealedMsg...) + //nonceAndSealedMsg := append(nonce[:], sealedMsg...) // we do an explit cast to int64 to avoid compilation failures // for 32bit systems. - nonceAndSealedMsgSize := int64(len(nonceAndSealedMsg)) + nonceAndSealedMsgSize := int64(len(sealedMsg)) if nonceAndSealedMsgSize >= math.MaxUint32 { - panic(fmt.Sprintf("writeRecord too large: %d", len(nonceAndSealedMsg))) + panic(fmt.Sprintf("writeRecord too large: %d", len(sealedMsg))) } - return d.conn.writeMsg(nonceAndSealedMsg) + return d.conn.writeMsg(sealedMsg) } From 97da493cb6470a25b1094efc4913bbca05235969 Mon Sep 17 00:00:00 2001 From: Donatas Puidokas Date: Thu, 24 Nov 2022 15:14:37 +0200 Subject: [PATCH 2/3] Remove comment line --- wormhole/transit.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/wormhole/transit.go b/wormhole/transit.go index cbb0f407..2c443ac7 100644 --- a/wormhole/transit.go +++ b/wormhole/transit.go @@ -372,8 +372,6 @@ func (d *transportCryptorClassic) writeRecord(msg []byte) error { sealedMsg := secretbox.Seal(nonce[:], msg, &nonce, &d.writeKey) - //nonceAndSealedMsg := append(nonce[:], sealedMsg...) - // we do an explit cast to int64 to avoid compilation failures // for 32bit systems. nonceAndSealedMsgSize := int64(len(sealedMsg)) From 5dde9f3126468e3be202eb3ada221f3b916d0b9e Mon Sep 17 00:00:00 2001 From: Donatas Puidokas Date: Wed, 14 Dec 2022 18:42:43 +0200 Subject: [PATCH 3/3] Rollback append removal --- wormhole/transit.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/wormhole/transit.go b/wormhole/transit.go index 2c443ac7..5e602099 100644 --- a/wormhole/transit.go +++ b/wormhole/transit.go @@ -94,11 +94,7 @@ type wsConnection struct { func (self *wsConnection) writeMsg(msg []byte) error { l := make([]byte, 4) binary.BigEndian.PutUint32(l, uint32(len(msg))) - err := self.conn.Write(self.ctx, websocket.MessageBinary, l) - if err != nil { - return err - } - return self.conn.Write(self.ctx, websocket.MessageBinary, msg) + return self.conn.Write(self.ctx, websocket.MessageBinary, append(l, msg...)) } func (self *wsConnection) writeHandshakeMsg(msg []byte) error {