mirror of
https://github.com/nadoo/glider.git
synced 2025-04-11 14:53:15 +08:00
This commit is contained in:
parent
bd40b07388
commit
8e81e09a8f
@ -38,10 +38,10 @@ archives:
|
|||||||
builds:
|
builds:
|
||||||
- default
|
- default
|
||||||
wrap_in_directory: true
|
wrap_in_directory: true
|
||||||
format: tar.gz
|
formats: tar.gz
|
||||||
format_overrides:
|
format_overrides:
|
||||||
- goos: windows
|
- goos: windows
|
||||||
format: zip
|
formats: zip
|
||||||
files:
|
files:
|
||||||
- LICENSE
|
- LICENSE
|
||||||
- README.md
|
- README.md
|
||||||
|
@ -3,6 +3,7 @@ package pool
|
|||||||
import (
|
import (
|
||||||
"math/bits"
|
"math/bits"
|
||||||
"sync"
|
"sync"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -17,11 +18,12 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
for i := 0; i < num; i++ {
|
for i := range num {
|
||||||
size := 1 << i
|
size := 1 << i
|
||||||
sizes[i] = size
|
sizes[i] = size
|
||||||
pools[i].New = func() any {
|
pools[i].New = func() any {
|
||||||
return make([]byte, size)
|
buf := make([]byte, size)
|
||||||
|
return unsafe.SliceData(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -30,11 +32,10 @@ func init() {
|
|||||||
// otherwise, this function will call make([]byte, size) directly.
|
// otherwise, this function will call make([]byte, size) directly.
|
||||||
func GetBuffer(size int) []byte {
|
func GetBuffer(size int) []byte {
|
||||||
if size >= 1 && size <= maxsize {
|
if size >= 1 && size <= maxsize {
|
||||||
i := bits.Len32(uint32(size)) - 1
|
i := bits.Len32(uint32(size - 1))
|
||||||
if sizes[i] < size {
|
if p := pools[i].Get().(*byte); p != nil {
|
||||||
i += 1
|
return unsafe.Slice(p, 1<<i)[:size]
|
||||||
}
|
}
|
||||||
return pools[i].Get().([]byte)[:size]
|
|
||||||
}
|
}
|
||||||
return make([]byte, size)
|
return make([]byte, size)
|
||||||
}
|
}
|
||||||
@ -42,9 +43,9 @@ func GetBuffer(size int) []byte {
|
|||||||
// PutBuffer puts a buffer into pool.
|
// PutBuffer puts a buffer into pool.
|
||||||
func PutBuffer(buf []byte) {
|
func PutBuffer(buf []byte) {
|
||||||
if size := cap(buf); size >= 1 && size <= maxsize {
|
if size := cap(buf); size >= 1 && size <= maxsize {
|
||||||
i := bits.Len32(uint32(size)) - 1
|
i := bits.Len32(uint32(size - 1))
|
||||||
if sizes[i] == size {
|
if sizes[i] == size {
|
||||||
pools[i].Put(buf)
|
pools[i].Put(unsafe.SliceData(buf))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user