yggdrasil-go/src/yggdrasil/util.go

50 lines
1.3 KiB
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
package yggdrasil
// These are misc. utility functions that didn't really fit anywhere else
import "runtime"
2018-01-04 22:37:51 +00:00
// A wrapper around runtime.Gosched() so it doesn't need to be imported elsewhere.
2017-12-29 04:16:20 +00:00
func util_yield() {
2018-01-04 22:37:51 +00:00
runtime.Gosched()
2017-12-29 04:16:20 +00:00
}
// A wrapper around runtime.LockOSThread() so it doesn't need to be imported elsewhere.
2017-12-29 04:16:20 +00:00
func util_lockthread() {
2018-01-04 22:37:51 +00:00
runtime.LockOSThread()
2017-12-29 04:16:20 +00:00
}
// A wrapper around runtime.UnlockOSThread() so it doesn't need to be imported elsewhere.
2017-12-29 04:16:20 +00:00
func util_unlockthread() {
2018-01-04 22:37:51 +00:00
runtime.UnlockOSThread()
2017-12-29 04:16:20 +00:00
}
// This is used to buffer recently used slices of bytes, to prevent allocations in the hot loops.
// It's used like a sync.Pool, but with a fixed size and typechecked without type casts to/from interface{} (which were making the profiles look ugly).
2017-12-29 04:16:20 +00:00
var byteStore chan []byte
// Initializes the byteStore
2017-12-29 04:16:20 +00:00
func util_initByteStore() {
2018-01-04 22:37:51 +00:00
if byteStore == nil {
byteStore = make(chan []byte, 32)
}
2017-12-29 04:16:20 +00:00
}
// Gets an empty slice from the byte store, if one is available, or else returns a new nil slice.
2017-12-29 04:16:20 +00:00
func util_getBytes() []byte {
2018-01-04 22:37:51 +00:00
select {
case bs := <-byteStore:
return bs[:0]
default:
return nil
}
2017-12-29 04:16:20 +00:00
}
// Puts a slice in the store, if there's room, or else returns and lets the slice get collected.
2017-12-29 04:16:20 +00:00
func util_putBytes(bs []byte) {
2018-01-04 22:37:51 +00:00
select {
case byteStore <- bs:
default:
}
2017-12-29 04:16:20 +00:00
}