Browse Source

allocate buffer on stack

pull/1435/head
Darien Raymond 6 years ago
parent
commit
a20262ef20
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 8
      common/buf/buffer.go
  2. 4
      proxy/socks/protocol.go
  3. 15
      proxy/socks/protocol_test.go
  4. 2
      proxy/vmess/encoding/client.go
  5. 2
      transport/internet/kcp/io.go

8
common/buf/buffer.go

@ -199,3 +199,11 @@ func New() *Buffer {
v: pool.Get().([]byte),
}
}
// StackNew creates a new Buffer object on stack.
// This method is for buffers that is released in the same function.
func StackNew() Buffer {
return Buffer{
v: pool.Get().([]byte),
}
}

4
proxy/socks/protocol.go

@ -190,7 +190,7 @@ func (s *ServerSession) Handshake(reader io.Reader, writer io.Writer) (*protocol
// | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
// +----+------+----------+------+----------+
func ReadUsernamePassword(reader io.Reader) (string, string, error) {
buffer := buf.New()
buffer := buf.StackNew()
defer buffer.Release()
if _, err := buffer.ReadFullFrom(reader, 2); err != nil {
@ -220,7 +220,7 @@ func ReadUsernamePassword(reader io.Reader) (string, string, error) {
// ReadUntilNull reads content from given reader, until a null (0x00) byte.
func ReadUntilNull(reader io.Reader) (string, error) {
b := buf.New()
b := buf.StackNew()
defer b.Release()
for {

15
proxy/socks/protocol_test.go

@ -4,6 +4,7 @@ import (
"bytes"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
_ "v2ray.com/core/common/net/testing"
@ -107,3 +108,17 @@ func TestReadUntilNull(t *testing.T) {
}
}
}
func BenchmarkReadUsernamePassword(b *testing.B) {
input := []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'}
buffer := buf.New()
buffer.Write(input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := ReadUsernamePassword(buffer)
common.Must(err)
buffer.Clear()
buffer.Extend(int32(len(input)))
}
}

2
proxy/vmess/encoding/client.go

@ -164,7 +164,7 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
aesStream := crypto.NewAesDecryptionStream(c.responseBodyKey[:], c.responseBodyIV[:])
c.responseReader = crypto.NewCryptionReader(aesStream, reader)
buffer := buf.New()
buffer := buf.StackNew()
defer buffer.Release()
if _, err := buffer.ReadFullFrom(c.responseReader, 4); err != nil {

2
transport/internet/kcp/io.go

@ -70,7 +70,7 @@ func (w *KCPPacketWriter) Overhead() int {
}
func (w *KCPPacketWriter) Write(b []byte) (int, error) {
bb := buf.New()
bb := buf.StackNew()
defer bb.Release()
if w.Header != nil {

Loading…
Cancel
Save