Skip to content

Commit

Permalink
use quicvarint.Append instead of quicvarint.Write (#56)
Browse files Browse the repository at this point in the history
Append is faster than Write. Probably doesn't matter much here, but
doesn't hurt.
  • Loading branch information
marten-seemann authored Jan 26, 2023
1 parent 4d1bbf9 commit eb5a5f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
9 changes: 4 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webtransport_test

import (
"bytes"
"context"
"crypto/tls"
"errors"
Expand Down Expand Up @@ -60,10 +59,10 @@ func TestClientInvalidResponseHandling(t *testing.T) {
str, err := conn.AcceptStream(context.Background())
require.NoError(t, err)
// write a HTTP3 data frame. This will cause an error, since a HEADERS frame is expected
b := &bytes.Buffer{}
quicvarint.Write(b, 0x0)
quicvarint.Write(b, 1337)
_, err = str.Write(b.Bytes())
var b []byte
b = quicvarint.Append(b, 0x0)
b = quicvarint.Append(b, 1337)
_, err = str.Write(b)
require.NoError(t, err)
for {
if _, err := str.Read(make([]byte, 64)); err != nil {
Expand Down
11 changes: 5 additions & 6 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webtransport_test

import (
"bytes"
"context"
"crypto/tls"
"fmt"
Expand Down Expand Up @@ -72,11 +71,11 @@ func createStreamAndWrite(t *testing.T, qconn http3.StreamCreator, sessionID uin
t.Helper()
str, err := qconn.OpenStream()
require.NoError(t, err)
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
quicvarint.Write(buf, sessionID) // stream ID of the stream used to establish the WebTransport session.
buf.Write(data)
_, err = str.Write(buf.Bytes())
var buf []byte
buf = quicvarint.Append(buf, 0x41)
buf = quicvarint.Append(buf, sessionID) // stream ID of the stream used to establish the WebTransport session.
buf = append(buf, data...)
_, err = str.Write(buf)
require.NoError(t, err)
require.NoError(t, str.Close())
return str
Expand Down
15 changes: 6 additions & 9 deletions session.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webtransport

import (
"bytes"
"context"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -97,15 +96,13 @@ func newSession(sessionID sessionID, qconn http3.StreamCreator, requestStr quic.
streams: *newStreamsMap(),
}
// precompute the headers for unidirectional streams
buf := bytes.NewBuffer(make([]byte, 0, 2+quicvarint.Len(uint64(c.sessionID))))
quicvarint.Write(buf, webTransportUniStreamType)
quicvarint.Write(buf, uint64(c.sessionID))
c.uniStreamHdr = buf.Bytes()
c.uniStreamHdr = make([]byte, 0, 2+quicvarint.Len(uint64(c.sessionID)))
c.uniStreamHdr = quicvarint.Append(c.uniStreamHdr, webTransportUniStreamType)
c.uniStreamHdr = quicvarint.Append(c.uniStreamHdr, uint64(c.sessionID))
// precompute the headers for bidirectional streams
buf = bytes.NewBuffer(make([]byte, 0, 2+quicvarint.Len(uint64(c.sessionID))))
quicvarint.Write(buf, webTransportFrameType)
quicvarint.Write(buf, uint64(c.sessionID))
c.streamHdr = buf.Bytes()
c.streamHdr = make([]byte, 0, 2+quicvarint.Len(uint64(c.sessionID)))
c.streamHdr = quicvarint.Append(c.streamHdr, webTransportFrameType)
c.streamHdr = quicvarint.Append(c.streamHdr, uint64(c.sessionID))

go func() {
defer ctxCancel()
Expand Down

0 comments on commit eb5a5f3

Please sign in to comment.