Files
obitools4/pkg/obiseq/pool.go

120 lines
3.0 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiseq
import (
"sync"
2022-01-16 00:21:42 +01:00
log "github.com/sirupsen/logrus"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
2022-01-13 23:27:39 +01:00
)
2022-01-16 00:21:42 +01:00
var _BioSequenceByteSlicePool = sync.Pool{
2022-01-13 23:27:39 +01:00
New: func() interface{} {
2022-01-16 00:21:42 +01:00
bs := make([]byte, 0, 300)
2022-01-13 23:27:39 +01:00
return &bs
},
}
// RecycleSlice recycles a byte slice by clearing its contents and returning it
// to a pool if it is small enough.
//
// Parameters: - s: a pointer to a byte slice that will be recycled.
//
// This function first checks if the input slice is not nil and has a non-zero
// capacity. If so, it clears the contents of the slice by setting its length to
// 0. Then, it checks if the capacity of the slice is less than or equal to
// 1024. If it is, the function puts the slice into a pool for reuse. If the
// capacity is 0 or greater than 1024, the function does nothing. If the input
// slice is nil or has a zero capacity, the function logs a panic message.
2022-02-18 22:53:09 +01:00
func RecycleSlice(s *[]byte) {
if s != nil && cap(*s) > 0 {
*s = (*s)[:0]
if cap(*s) == 0 {
log.Panicln("trying to store a NIL slice in the pool", s == nil, *s == nil, cap(*s))
}
if cap(*s) <= 1024 {
_BioSequenceByteSlicePool.Put(s)
}
}
2022-01-16 00:21:42 +01:00
}
// GetSlice returns a byte slice with the specified capacity.
//
// The function first checks if the capacity is less than or equal to 1024. If it is,
// it retrieves a byte slice from the _BioSequenceByteSlicePool. If the retrieved
// slice is nil, has a nil underlying array, or has a capacity less than the
// specified capacity, a new byte slice is created with the specified capacity.
// If the capacity is greater than 1024, a new byte slice is created with the
// specified capacity.
//
// The function returns the byte slice.
//
// Parameters:
// - capacity: the desired capacity of the byte slice.
//
// Return type:
// - []byte: the byte slice with the specified capacity.
func GetSlice(capacity int) []byte {
p := (*[]byte)(nil)
if capacity <= 1024 {
p = _BioSequenceByteSlicePool.Get().(*[]byte)
}
2022-01-16 00:21:42 +01:00
if p == nil || *p == nil || cap(*p) < capacity {
return make([]byte, 0, capacity)
2022-01-16 00:21:42 +01:00
}
s := *p
2022-01-16 00:21:42 +01:00
if cap(s) < capacity {
log.Panicln("Bizarre... j'aurai pourtant cru")
}
2022-01-16 00:21:42 +01:00
return s
}
func CopySlice(src []byte) []byte {
sl := GetSlice(len(src))
sl = sl[0:len(src)]
2022-10-12 23:01:47 +02:00
copy(sl, src)
return sl
}
2022-01-16 00:21:42 +01:00
var BioSequenceAnnotationPool = sync.Pool{
New: func() interface{} {
bs := make(Annotation, 5)
2022-01-16 00:21:42 +01:00
return &bs
},
2022-01-13 23:27:39 +01:00
}
2022-02-18 22:53:09 +01:00
func RecycleAnnotation(a *Annotation) {
2022-02-01 17:31:28 +01:00
if a != nil {
2022-02-18 22:53:09 +01:00
for k := range *a {
delete(*a, k)
2022-02-01 17:31:28 +01:00
}
BioSequenceAnnotationPool.Put(a)
2022-01-16 00:21:42 +01:00
}
2022-01-13 23:27:39 +01:00
}
// GetAnnotation returns an Annotation from the BioSequenceAnnotationPool.
//
// It takes as argument O or 1 Annotation annotation object.
// If an annotation object is passed, it is copied into the new Annotation.
//
// It returns an Annotation.
2022-01-16 00:21:42 +01:00
func GetAnnotation(values ...Annotation) Annotation {
a := Annotation(nil)
2022-01-16 00:21:42 +01:00
for a == nil {
a = *(BioSequenceAnnotationPool.Get().(*Annotation))
2022-01-16 00:21:42 +01:00
}
2022-02-18 22:53:09 +01:00
if len(values) > 0 {
obiutils.MustFillMap(a, values[0])
2022-02-18 22:53:09 +01:00
}
return a
2022-02-18 22:53:09 +01:00
}