Ver código fonte

remove unnecessary use of buffer

v2ray 9 anos atrás
pai
commit
b6a6c154a3
2 arquivos alterados com 23 adições e 37 exclusões
  1. 7 17
      common/log/internal/log_entry.go
  2. 16 20
      common/protocol/raw/client.go

+ 7 - 17
common/log/internal/log_entry.go

@@ -2,9 +2,9 @@ package internal
 
 import (
 	"fmt"
+	"strings"
 
 	"github.com/v2ray/v2ray-core/common"
-	"github.com/v2ray/v2ray-core/common/alloc"
 	"github.com/v2ray/v2ray-core/common/serial"
 )
 
@@ -46,15 +46,12 @@ func (this *ErrorLog) Release() {
 }
 
 func (this *ErrorLog) String() string {
-	b := alloc.NewSmallBuffer().Clear()
-	defer b.Release()
-
-	b.AppendString(this.Prefix)
-
-	for _, value := range this.Values {
-		b.AppendString(InterfaceToString(value))
+	values := make([]string, len(this.Values)+1)
+	values[0] = this.Prefix
+	for i, value := range this.Values {
+		values[i+1] = InterfaceToString(value)
 	}
-	return b.String()
+	return strings.Join(values, "")
 }
 
 type AccessLog struct {
@@ -71,12 +68,5 @@ func (this *AccessLog) Release() {
 }
 
 func (this *AccessLog) String() string {
-	b := alloc.NewSmallBuffer().Clear()
-	defer b.Release()
-
-	b.AppendString(InterfaceToString(this.From)).AppendString(" ")
-	b.AppendString(this.Status).AppendString(" ")
-	b.AppendString(InterfaceToString(this.To)).AppendString(" ")
-	b.AppendString(InterfaceToString(this.Reason))
-	return b.String()
+	return strings.Join([]string{InterfaceToString(this.From), this.Status, InterfaceToString(this.To), InterfaceToString(this.Reason)}, " ")
 }

+ 16 - 20
common/protocol/raw/client.go

@@ -55,41 +55,37 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
 	idHash.Write(timestamp.Bytes(nil))
 	writer.Write(idHash.Sum(nil))
 
-	buffer := alloc.NewSmallBuffer().Clear()
-	defer buffer.Release()
-
-	buffer.AppendBytes(Version)
-	buffer.Append(this.requestBodyIV)
-	buffer.Append(this.requestBodyKey)
-	buffer.AppendBytes(this.responseHeader, byte(header.Option), byte(0), byte(0))
-	buffer.AppendBytes(byte(header.Command))
-	buffer.AppendUint16(header.Port.Value())
+	buffer := make([]byte, 0, 512)
+	buffer = append(buffer, Version)
+	buffer = append(buffer, this.requestBodyIV...)
+	buffer = append(buffer, this.requestBodyKey...)
+	buffer = append(buffer, this.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
+	buffer = header.Port.Bytes(buffer)
 
 	switch {
 	case header.Address.IsIPv4():
-		buffer.AppendBytes(AddrTypeIPv4)
-		buffer.Append(header.Address.IP())
+		buffer = append(buffer, AddrTypeIPv4)
+		buffer = append(buffer, header.Address.IP()...)
 	case header.Address.IsIPv6():
-		buffer.AppendBytes(AddrTypeIPv6)
-		buffer.Append(header.Address.IP())
+		buffer = append(buffer, AddrTypeIPv6)
+		buffer = append(buffer, header.Address.IP()...)
 	case header.Address.IsDomain():
-		buffer.AppendBytes(AddrTypeDomain, byte(len(header.Address.Domain())))
-		buffer.Append([]byte(header.Address.Domain()))
+		buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
+		buffer = append(buffer, header.Address.Domain()...)
 	}
 
 	fnv1a := fnv.New32a()
-	fnv1a.Write(buffer.Value)
+	fnv1a.Write(buffer)
 
-	fnvHash := fnv1a.Sum32()
-	buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
+	buffer = fnv1a.Sum(buffer)
 
 	timestampHash := md5.New()
 	timestampHash.Write(hashTimestamp(timestamp))
 	iv := timestampHash.Sum(nil)
 	account := header.User.Account.(*protocol.VMessAccount)
 	aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
-	aesStream.XORKeyStream(buffer.Value, buffer.Value)
-	writer.Write(buffer.Value)
+	aesStream.XORKeyStream(buffer, buffer)
+	writer.Write(buffer)
 
 	return
 }