remove the slice pool management

This commit is contained in:
Eric Coissac
2024-09-24 16:31:30 +02:00
parent 2b4a633c30
commit 241f2286f2
17 changed files with 23 additions and 84 deletions

View File

@ -64,6 +64,7 @@ type BioSequence struct {
qualities []byte // The quality scores of the sequence.
feature []byte
paired *BioSequence // A pointer to the paired sequence
revcomp *BioSequence // A pointer to the reverse complemented sequence
annotations Annotation
annot_lock *sync.Mutex
}
@ -78,7 +79,8 @@ func NewEmptyBioSequence(preallocate int) *BioSequence {
seq := []byte(nil)
if preallocate > 0 {
seq = GetSlice(preallocate)
// seq = GetSlice(preallocate)
seq = make([]byte, 0, preallocate)
}
return &BioSequence{
@ -89,6 +91,7 @@ func NewEmptyBioSequence(preallocate int) *BioSequence {
qualities: nil,
feature: nil,
paired: nil,
revcomp: nil,
annotations: nil,
annot_lock: &sync.Mutex{},
}
@ -426,9 +429,6 @@ func (s *BioSequence) SetFeatures(feature []byte) {
// Parameters:
// - sequence: a byte slice representing the sequence to be set.
func (s *BioSequence) SetSequence(sequence []byte) {
if s.sequence != nil {
RecycleSlice(&s.sequence)
}
s.sequence = obiutils.InPlaceToLower(CopySlice(sequence))
}

View File

@ -1,8 +1,6 @@
package obiseq
import (
"sync"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
@ -14,13 +12,6 @@ import (
// a memory pool of BioSequenceSlice is managed to limit allocations.
type BioSequenceSlice []*BioSequence
var _BioSequenceSlicePool = sync.Pool{
New: func() interface{} {
bs := make(BioSequenceSlice, 0, 10)
return &bs
},
}
// NewBioSequenceSlice returns a new BioSequenceSlice with the specified size.
//
// The size parameter is optional. If provided, the returned slice will be
@ -28,13 +19,14 @@ var _BioSequenceSlicePool = sync.Pool{
//
// Returns a pointer to the newly created BioSequenceSlice.
func NewBioSequenceSlice(size ...int) *BioSequenceSlice {
slice := _BioSequenceSlicePool.Get().(*BioSequenceSlice)
capacity := 0
if len(size) > 0 {
s := size[0]
slice = slice.EnsureCapacity(s)
(*slice) = (*slice)[0:s]
capacity = size[0]
}
return slice
slice := make(BioSequenceSlice, capacity)
return &slice
}
// MakeBioSequenceSlice creates a new BioSequenceSlice with the specified size(s).
@ -48,34 +40,6 @@ func MakeBioSequenceSlice(size ...int) BioSequenceSlice {
return *NewBioSequenceSlice(size...)
}
// Recycle cleans up the BioSequenceSlice by recycling its elements and resetting its length.
//
// If including_seq is true, each element of the BioSequenceSlice is recycled using the Recycle method,
// and then set to nil. If including_seq is false, each element is simply set to nil.
//
// The function does not return anything.
func (s *BioSequenceSlice) Recycle(including_seq bool) {
if s == nil {
log.Panicln("Trying too recycle a nil pointer")
}
// Code added to potentially limit memory leaks
if including_seq {
for i := range *s {
(*s)[i].Recycle()
(*s)[i] = nil
}
} else {
for i := range *s {
(*s)[i] = nil
}
}
*s = (*s)[:0]
_BioSequenceSlicePool.Put(s)
}
// EnsureCapacity ensures that the BioSequenceSlice has a minimum capacity
//
// It takes an integer `capacity` as a parameter, which represents the desired minimum capacity of the BioSequenceSlice.

View File

@ -295,6 +295,5 @@ func (sequences BioSequenceSlice) Merge(na string, statsOn StatsOnDescriptions)
}
}
sequences.Recycle(false)
return seq
}

View File

@ -43,12 +43,21 @@ func nucComplement(n byte) byte {
// The function returns the reverse complemented BioSequence.
func (sequence *BioSequence) ReverseComplement(inplace bool) *BioSequence {
original := (*BioSequence)(nil)
if sequence == nil {
return nil
}
if sequence.revcomp != nil {
return sequence.revcomp
}
if !inplace {
sequence = sequence.Copy()
original = sequence
sequence.revcomp = sequence.Copy()
sequence = sequence.revcomp
sequence.revcomp = original
}
s := sequence.sequence