mirror of
https://github.com/Gouryella/drip.git
synced 2026-03-04 12:55:53 +00:00
perf(core): Optimizes performance configuration and resource management
- Removed the manual performance optimization configuration in main.go and replaced it with a new tuning module. - Add patterned GC tuning in server.go and tunnel_runner.go - Updated yamux configuration to a unified optimized configuration to improve throughput. - Implement connection pool preheating function to eliminate cold start delay. - Optimize session selection using a min-heap, reducing the time complexity from O(n) to O(log n). - Add a bufio.Reader pool and a buffer pool to reduce memory allocation. - Implement a fragmented lock manager to improve performance under high concurrency. - Adjust heartbeat and timeout configurations to suit high-throughput scenarios BREAKING CHANGE: Manual GC tuning configuration has been removed; automatic tuning mode is now used.
This commit is contained in:
@@ -3,15 +3,17 @@ package pool
|
||||
import "sync"
|
||||
|
||||
const (
|
||||
SizeSmall = 4 * 1024 // 4KB
|
||||
SizeMedium = 32 * 1024 // 32KB
|
||||
SizeLarge = 256 * 1024 // 256KB
|
||||
SizeSmall = 4 * 1024 // 4KB - HTTP headers, small messages
|
||||
SizeMedium = 32 * 1024 // 32KB - HTTP request/response bodies
|
||||
SizeLarge = 256 * 1024 // 256KB - Data pipe, file transfers
|
||||
SizeXLarge = 1024 * 1024 // 1MB - Large file transfers, bulk data
|
||||
)
|
||||
|
||||
type BufferPool struct {
|
||||
small sync.Pool
|
||||
medium sync.Pool
|
||||
large sync.Pool
|
||||
xlarge sync.Pool
|
||||
}
|
||||
|
||||
func NewBufferPool() *BufferPool {
|
||||
@@ -34,6 +36,12 @@ func NewBufferPool() *BufferPool {
|
||||
return &b
|
||||
},
|
||||
},
|
||||
xlarge: sync.Pool{
|
||||
New: func() interface{} {
|
||||
b := make([]byte, SizeXLarge)
|
||||
return &b
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +51,10 @@ func (p *BufferPool) Get(size int) *[]byte {
|
||||
return p.small.Get().(*[]byte)
|
||||
case size <= SizeMedium:
|
||||
return p.medium.Get().(*[]byte)
|
||||
default:
|
||||
case size <= SizeLarge:
|
||||
return p.large.Get().(*[]byte)
|
||||
default:
|
||||
return p.xlarge.Get().(*[]byte)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +73,24 @@ func (p *BufferPool) Put(buf *[]byte) {
|
||||
p.medium.Put(buf)
|
||||
case SizeLarge:
|
||||
p.large.Put(buf)
|
||||
case SizeXLarge:
|
||||
p.xlarge.Put(buf)
|
||||
}
|
||||
// Note: buffers with non-standard sizes are not pooled (let GC handle them)
|
||||
}
|
||||
|
||||
// GetXLarge returns a 1MB buffer for bulk data transfers
|
||||
func (p *BufferPool) GetXLarge() *[]byte {
|
||||
return p.xlarge.Get().(*[]byte)
|
||||
}
|
||||
|
||||
// PutXLarge returns a 1MB buffer to the pool
|
||||
func (p *BufferPool) PutXLarge(buf *[]byte) {
|
||||
if buf == nil || cap(*buf) != SizeXLarge {
|
||||
return
|
||||
}
|
||||
*buf = (*buf)[:cap(*buf)]
|
||||
p.xlarge.Put(buf)
|
||||
}
|
||||
|
||||
var globalBufferPool = NewBufferPool()
|
||||
@@ -75,3 +102,13 @@ func GetBuffer(size int) *[]byte {
|
||||
func PutBuffer(buf *[]byte) {
|
||||
globalBufferPool.Put(buf)
|
||||
}
|
||||
|
||||
// GetXLargeBuffer returns a 1MB buffer from the global pool
|
||||
func GetXLargeBuffer() *[]byte {
|
||||
return globalBufferPool.GetXLarge()
|
||||
}
|
||||
|
||||
// PutXLargeBuffer returns a 1MB buffer to the global pool
|
||||
func PutXLargeBuffer(buf *[]byte) {
|
||||
globalBufferPool.PutXLarge(buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user