diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 75d93b5af..e0509a174 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -139,11 +139,6 @@ func (b *Buffer) Write(data []byte) (int, error) { return nBytes, nil } -// WriteBytes appends one or more bytes to the end of the buffer. -func (b *Buffer) WriteBytes(bytes ...byte) (int, error) { - return b.Write(bytes) -} - // WriteByte writes a single byte into the buffer. func (b *Buffer) WriteByte(v byte) error { if b.IsFull() { diff --git a/common/buf/buffer_test.go b/common/buf/buffer_test.go index d45677ab0..c61ff7d88 100644 --- a/common/buf/buffer_test.go +++ b/common/buf/buffer_test.go @@ -96,3 +96,63 @@ func BenchmarkNewBuffer(b *testing.B) { buffer.Release() } } + +func BenchmarkWrite2(b *testing.B) { + buffer := New() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = buffer.Write([]byte{'a', 'b'}) + buffer.Clear() + } +} + +func BenchmarkWrite8(b *testing.B) { + buffer := New() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = buffer.Write([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}) + buffer.Clear() + } +} + +func BenchmarkWrite32(b *testing.B) { + buffer := New() + payload := make([]byte, 32) + rand.Read(payload) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = buffer.Write(payload) + buffer.Clear() + } +} + +func BenchmarkWriteByte2(b *testing.B) { + buffer := New() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = buffer.WriteByte('a') + _ = buffer.WriteByte('b') + buffer.Clear() + } +} + +func BenchmarkWriteByte8(b *testing.B) { + buffer := New() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = buffer.WriteByte('a') + _ = buffer.WriteByte('b') + _ = buffer.WriteByte('c') + _ = buffer.WriteByte('d') + _ = buffer.WriteByte('e') + _ = buffer.WriteByte('f') + _ = buffer.WriteByte('g') + _ = buffer.WriteByte('h') + buffer.Clear() + } +} diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index 84592792f..0ec46ef1a 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -14,10 +14,10 @@ func TestMultiBufferRead(t *testing.T) { assert := With(t) b1 := New() - b1.WriteBytes('a', 'b') + b1.WriteString("ab") b2 := New() - b2.WriteBytes('c', 'd') + b2.WriteString("cd") mb := NewMultiBufferValue(b1, b2) bs := make([]byte, 32) @@ -32,7 +32,7 @@ func TestMultiBufferAppend(t *testing.T) { var mb MultiBuffer b := New() - b.WriteBytes('a', 'b') + b.WriteString("ab") mb.Append(b) assert(mb.Len(), Equals, int32(2)) } diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index 9e3458423..272ab5a9a 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -17,9 +17,9 @@ func TestBytesReaderWriteTo(t *testing.T) { pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024)) reader := &BufferedReader{Reader: pReader} b1 := New() - b1.WriteBytes('a', 'b', 'c') + b1.WriteString("abc") b2 := New() - b2.WriteBytes('e', 'f', 'g') + b2.WriteString("efg") assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil) pWriter.Close() @@ -44,9 +44,9 @@ func TestBytesReaderMultiBuffer(t *testing.T) { pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024)) reader := &BufferedReader{Reader: pReader} b1 := New() - b1.WriteBytes('a', 'b', 'c') + b1.WriteString("abc") b2 := New() - b2.WriteBytes('e', 'f', 'g') + b2.WriteString("efg") assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil) pWriter.Close() diff --git a/common/crypto/chunk_test.go b/common/crypto/chunk_test.go index 47402de18..90409fc3b 100644 --- a/common/crypto/chunk_test.go +++ b/common/crypto/chunk_test.go @@ -20,11 +20,11 @@ func TestChunkStreamIO(t *testing.T) { reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache) b := buf.New() - b.WriteBytes('a', 'b', 'c', 'd') + b.WriteString("abcd") common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))) b = buf.New() - b.WriteBytes('e', 'f', 'g') + b.WriteString("efg") common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))) common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{})) diff --git a/proxy/blackhole/config.go b/proxy/blackhole/config.go index 2a0da01d0..6cd629a20 100644 --- a/proxy/blackhole/config.go +++ b/proxy/blackhole/config.go @@ -27,7 +27,7 @@ func (*NoneResponse) WriteTo(buf.Writer) int32 { return 0 } // WriteTo implements ResponseConfig.WriteTo(). func (*HTTPResponse) WriteTo(writer buf.Writer) int32 { b := buf.New() - common.Must2(b.Write([]byte(http403response))) + common.Must2(b.WriteString(http403response)) n := b.Len() writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) return n diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index 3bd11b865..de7c1d9a4 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -12,8 +12,7 @@ func TestNormalChunkReading(t *testing.T) { assert := With(t) buffer := buf.New() - buffer.WriteBytes( - 0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18) + buffer.Write([]byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}) reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator( []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}))) payload, err := reader.ReadMultiBuffer() diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 319b3adaf..cdc98c7c5 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -255,7 +255,7 @@ func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, po buffer := buf.New() defer buffer.Release() - common.Must2(buffer.WriteBytes(socks5Version, errCode, 0x00 /* reserved */)) + common.Must2(buffer.Write([]byte{socks5Version, errCode, 0x00 /* reserved */})) if err := addrParser.WriteAddressPort(buffer, address, port); err != nil { return err } @@ -267,7 +267,8 @@ func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, po buffer := buf.New() defer buffer.Release() - common.Must2(buffer.WriteBytes(0x00, errCode)) + common.Must(buffer.WriteByte(0x00)) + common.Must(buffer.WriteByte(errCode)) common.Must2(serial.WriteUint16(buffer, port.Value())) common.Must2(buffer.Write(address.IP())) return buf.WriteAllBytes(writer, buffer.Bytes()) @@ -300,7 +301,7 @@ func DecodeUDPPacket(packet *buf.Buffer) (*protocol.RequestHeader, error) { func EncodeUDPPacket(request *protocol.RequestHeader, data []byte) (*buf.Buffer, error) { b := buf.New() - common.Must2(b.WriteBytes(0, 0, 0 /* Fragment */)) + common.Must2(b.Write([]byte{0, 0, 0 /* Fragment */})) if err := addrParser.WriteAddressPort(b, request.Address, request.Port); err != nil { b.Release() return nil, err @@ -362,14 +363,15 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i b := buf.New() defer b.Release() - common.Must2(b.WriteBytes(socks5Version, 0x01, authByte)) + common.Must2(b.Write([]byte{socks5Version, 0x01, authByte})) if authByte == authPassword { account := request.User.Account.(*Account) - common.Must2(b.WriteBytes(0x01, byte(len(account.Username)))) - common.Must2(b.Write([]byte(account.Username))) - common.Must2(b.WriteBytes(byte(len(account.Password)))) - common.Must2(b.Write([]byte(account.Password))) + common.Must(b.WriteByte(0x01)) + common.Must(b.WriteByte(byte(len(account.Username)))) + common.Must2(b.WriteString(account.Username)) + common.Must(b.WriteByte(byte(len(account.Password)))) + common.Must2(b.WriteString(account.Password)) } if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil { @@ -404,7 +406,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i if request.Command == protocol.RequestCommandUDP { command = byte(cmdUDPPort) } - common.Must2(b.WriteBytes(socks5Version, command, 0x00 /* reserved */)) + common.Must2(b.Write([]byte{socks5Version, command, 0x00 /* reserved */})) if err := addrParser.WriteAddressPort(b, request.Address, request.Port); err != nil { return nil, err } diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index c35f5e210..a75631a9e 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -65,14 +65,15 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ buffer := buf.New() defer buffer.Release() - common.Must2(buffer.WriteBytes(Version)) + common.Must(buffer.WriteByte(Version)) common.Must2(buffer.Write(c.requestBodyIV[:])) common.Must2(buffer.Write(c.requestBodyKey[:])) - common.Must2(buffer.WriteBytes(c.responseHeader, byte(header.Option))) + common.Must(buffer.WriteByte(c.responseHeader)) + common.Must(buffer.WriteByte(byte(header.Option))) padingLen := dice.Roll(16) security := byte(padingLen<<4) | byte(header.Security) - common.Must2(buffer.WriteBytes(security, byte(0), byte(header.Command))) + common.Must2(buffer.Write([]byte{security, byte(0), byte(header.Command)})) if header.Command != protocol.RequestCommandMux { if err := addrParser.WriteAddressPort(buffer, header.Address, header.Port); err != nil { diff --git a/transport/internet/kcp/segment_test.go b/transport/internet/kcp/segment_test.go index b60d26ebb..f4369e15e 100644 --- a/transport/internet/kcp/segment_test.go +++ b/transport/internet/kcp/segment_test.go @@ -50,7 +50,7 @@ func Test1ByteDataSegment(t *testing.T) { Number: 4, SendingNext: 5, } - seg.Data().WriteBytes('a') + seg.Data().WriteByte('a') nBytes := seg.ByteSize() bytes := make([]byte, nBytes) diff --git a/transport/internet/udp/dispatcher_test.go b/transport/internet/udp/dispatcher_test.go index 0dd93ff7e..329b7b562 100644 --- a/transport/internet/udp/dispatcher_test.go +++ b/transport/internet/udp/dispatcher_test.go @@ -63,7 +63,7 @@ func TestSameDestinationDispatching(t *testing.T) { dest := net.UDPDestination(net.LocalHostIP, 53) b := buf.New() - b.WriteBytes('a', 'b', 'c', 'd') + b.WriteString("abcd") var msgCount uint32 dispatcher := NewDispatcher(td, func(ctx context.Context, payload *buf.Buffer) { diff --git a/transport/pipe/pipe_test.go b/transport/pipe/pipe_test.go index 964043342..6ab787558 100644 --- a/transport/pipe/pipe_test.go +++ b/transport/pipe/pipe_test.go @@ -103,7 +103,7 @@ func TestPipeWriteMultiThread(t *testing.T) { wg.Add(1) go func() { b := buf.New() - b.WriteBytes('a', 'b', 'c', 'd') + b.WriteString("abcd") pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)) wg.Done() }()