mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-10 07:20:39 +03:00
just use a sync.Pool as the bytestore to not overcomplicate things, the allocations from interface{} casting don't seem to actually hurt in practice right now
This commit is contained in:
parent
cbbb61b019
commit
144c823bee
@ -22,27 +22,16 @@ func UnlockThread() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This is used to buffer recently used slices of bytes, to prevent allocations in the hot loops.
|
// This is used to buffer recently used slices of bytes, to prevent allocations in the hot loops.
|
||||||
var byteStoreMutex sync.Mutex
|
var byteStore = sync.Pool{New: func() interface{} { return []byte(nil) }}
|
||||||
var byteStore [][]byte
|
|
||||||
|
|
||||||
// Gets an empty slice from the byte store.
|
// Gets an empty slice from the byte store.
|
||||||
func GetBytes() []byte {
|
func GetBytes() []byte {
|
||||||
byteStoreMutex.Lock()
|
return byteStore.Get().([]byte)[:0]
|
||||||
defer byteStoreMutex.Unlock()
|
|
||||||
if len(byteStore) > 0 {
|
|
||||||
var bs []byte
|
|
||||||
bs, byteStore = byteStore[len(byteStore)-1][:0], byteStore[:len(byteStore)-1]
|
|
||||||
return bs
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Puts a slice in the store.
|
// Puts a slice in the store.
|
||||||
func PutBytes(bs []byte) {
|
func PutBytes(bs []byte) {
|
||||||
byteStoreMutex.Lock()
|
byteStore.Put(bs)
|
||||||
defer byteStoreMutex.Unlock()
|
|
||||||
byteStore = append(byteStore, bs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a workaround to go's broken timer implementation
|
// This is a workaround to go's broken timer implementation
|
||||||
|
Loading…
Reference in New Issue
Block a user