refactor(misc): enhance performance on bytes pool, entrypoint, access log and route context handling

- Introduced benchmark tests for Entrypoint and ReverseProxy to evaluate performance.
- Updated Entrypoint's ServeHTTP method to improve route context management.
- Added new test file for entrypoint benchmarks and refined existing tests for route handling.
This commit is contained in:
yusing
2025-09-14 00:03:27 +08:00
parent e0d25e475c
commit f31b1b5ed3
11 changed files with 623 additions and 33 deletions

View File

@@ -41,6 +41,28 @@ type BytesPoolWithMemory struct {
pool chan weakBuf
}
type sliceInternal struct {
ptr unsafe.Pointer
len int
cap int
}
func sliceStruct(b *[]byte) *sliceInternal {
return (*sliceInternal)(unsafe.Pointer(b))
}
func underlyingPtr(b []byte) unsafe.Pointer {
return sliceStruct(&b).ptr
}
func setCap(b *[]byte, cap int) {
sliceStruct(b).cap = cap
}
func setLen(b *[]byte, len int) {
sliceStruct(b).len = len
}
const (
kb = 1024
mb = 1024 * kb
@@ -88,7 +110,7 @@ func (p *BytesPool) Get() []byte {
addReused(cap(bPtr))
return bPtr
default:
return make([]byte, 0)
return make([]byte, 0, p.initSize)
}
}
}
@@ -113,10 +135,6 @@ func (p *BytesPoolWithMemory) Get() []byte {
}
func (p *BytesPool) GetSized(size int) []byte {
if size <= SizedPoolThreshold {
addNonPooled(size)
return make([]byte, size)
}
for {
select {
case bWeak := <-p.sizedPool:
@@ -125,10 +143,26 @@ func (p *BytesPool) GetSized(size int) []byte {
continue
}
capB := cap(bPtr)
if capB >= size {
remainingSize := capB - size
if remainingSize == 0 {
addReused(capB)
return (bPtr)[:size]
return bPtr[:size]
}
if remainingSize > 0 { // capB > size (buffer larger than requested)
addReused(size)
p.Put(bPtr[size:capB])
// return the first part and limit the capacity to the requested size
ret := bPtr[:size]
setLen(&ret, size)
setCap(&ret, size)
return ret
}
// size is not enough
select {
case p.sizedPool <- bWeak:
default:
@@ -147,11 +181,10 @@ func (p *BytesPool) Put(b []byte) {
return
}
b = b[:0]
w := makeWeak(&b)
if size <= SizedPoolThreshold {
p.put(w, p.unsizedPool)
if size >= SizedPoolThreshold {
p.put(makeWeak(&b), p.sizedPool)
} else {
p.put(w, p.sizedPool)
p.put(makeWeak(&b), p.unsizedPool)
}
}