Bladeren bron

reduce memory usage in log

v2ray 9 jaren geleden
bovenliggende
commit
5390e8efff
4 gewijzigde bestanden met toevoegingen van 29 en 10 verwijderingen
  1. 9 0
      common/alloc/buffer.go
  2. 5 1
      common/log/access.go
  3. 12 7
      common/log/log.go
  4. 3 2
      common/log/log_writer.go

+ 9 - 0
common/alloc/buffer.go

@@ -49,6 +49,11 @@ func (b *Buffer) Append(data []byte) *Buffer {
 	return b
 }
 
+func (b *Buffer) AppendString(s string) *Buffer {
+	b.Value = append(b.Value, s...)
+	return b
+}
+
 // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
 // no more than 16 bytes.
 func (b *Buffer) Prepend(data []byte) *Buffer {
@@ -131,6 +136,10 @@ func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
 	return nBytes, err
 }
 
+func (b *Buffer) String() string {
+	return string(b.Value)
+}
+
 // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
 func NewSmallBuffer() *Buffer {
 	return smallPool.Allocate()

+ 5 - 1
common/log/access.go

@@ -1,6 +1,7 @@
 package log
 
 import (
+	"github.com/v2ray/v2ray-core/common/alloc"
 	"github.com/v2ray/v2ray-core/common/serial"
 )
 
@@ -30,7 +31,10 @@ func (this *accessLog) Release() {
 }
 
 func (this *accessLog) String() string {
-	return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String()
+	b := alloc.NewSmallBuffer().Clear()
+	defer b.Release()
+
+	return b.AppendString(this.From.String()).AppendString(" ").AppendString(string(this.Status)).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String()
 }
 
 // InitAccessLogger initializes the access logger to write into the give file.

+ 12 - 7
common/log/log.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 
 	"github.com/v2ray/v2ray-core/common"
+	"github.com/v2ray/v2ray-core/common/alloc"
 	"github.com/v2ray/v2ray-core/common/serial"
 )
 
@@ -33,22 +34,26 @@ func (this *errorLog) Release() {
 }
 
 func (this *errorLog) String() string {
-	data := ""
+	b := alloc.NewSmallBuffer().Clear()
+	defer b.Release()
+
+	b.AppendString(this.prefix)
+
 	for _, value := range this.values {
 		switch typedVal := value.(type) {
 		case string:
-			data += typedVal
+			b.AppendString(typedVal)
 		case *string:
-			data += *typedVal
+			b.AppendString(*typedVal)
 		case serial.String:
-			data += typedVal.String()
+			b.AppendString(typedVal.String())
 		case error:
-			data += typedVal.Error()
+			b.AppendString(typedVal.Error())
 		default:
-			data += fmt.Sprintf("%v", value)
+			b.AppendString(fmt.Sprint(value))
 		}
 	}
-	return this.prefix + data
+	return b.String()
 }
 
 var (

+ 3 - 2
common/log/log_writer.go

@@ -19,8 +19,8 @@ type logWriter interface {
 type noOpLogWriter struct {
 }
 
-func (this *noOpLogWriter) Log(LogEntry) {
-	// Swallow
+func (this *noOpLogWriter) Log(entry LogEntry) {
+	entry.Release()
 }
 
 type stdOutLogWriter struct {
@@ -48,6 +48,7 @@ func (this *fileLogWriter) Log(log LogEntry) {
 	select {
 	case this.queue <- log:
 	default:
+		log.Release()
 		// We don't expect this to happen, but don't want to block main thread as well.
 	}
 }