diff --git a/pkg/obiapat/pcr.go b/pkg/obiapat/pcr.go index 5a30c53..989fa2e 100644 --- a/pkg/obiapat/pcr.go +++ b/pkg/obiapat/pcr.go @@ -529,7 +529,6 @@ func PCRSliceWorker(options ...WithOption) obiseq.SeqSliceWorker { opt := MakeOptions(options) worker := func(sequences obiseq.BioSequenceSlice) (obiseq.BioSequenceSlice, error) { result := _PCRSlice(sequences, opt) - sequences.Recycle(true) return result, nil } diff --git a/pkg/obichunk/chunks.go b/pkg/obichunk/chunks.go index 95915c0..b4700f3 100644 --- a/pkg/obichunk/chunks.go +++ b/pkg/obichunk/chunks.go @@ -49,7 +49,6 @@ func ISequenceChunk(iterator obiiter.IBioSequence, b := data.Get() source = b.Source() *chunk = append(*chunk, b.Slice()...) - b.Recycle(false) } lock.Lock() diff --git a/pkg/obichunk/subchunks.go b/pkg/obichunk/subchunks.go index 0d72cd7..4a798f6 100644 --- a/pkg/obichunk/subchunks.go +++ b/pkg/obichunk/subchunks.go @@ -107,8 +107,6 @@ func ISequenceSubChunk(iterator obiiter.IBioSequence, batch.Slice()[i] = nil } - batch.Recycle(false) - _By(func(p1, p2 *sSS) bool { return p1.code < p2.code }).Sort(ordered) diff --git a/pkg/obichunk/unique.go b/pkg/obichunk/unique.go index 621e204..7d971c6 100644 --- a/pkg/obichunk/unique.go +++ b/pkg/obichunk/unique.go @@ -95,10 +95,7 @@ func IUniqueSequence(iterator obiiter.IBioSequence, if icat < 0 || len(batch.Slice()) == 1 { // No more sub classification of sequence or only a single sequence - if opts.NoSingleton() && len(batch.Slice()) == 1 && batch.Slice()[0].Count() == 1 { - // We remove singleton from output - batch.Recycle(true) - } else { + if !(opts.NoSingleton() && len(batch.Slice()) == 1 && batch.Slice()[0].Count() == 1) { iUnique.Push(batch.Reorder(nextOrder())) } } else { diff --git a/pkg/obiiter/batch.go b/pkg/obiiter/batch.go index 5c9bdc1..94db95e 100644 --- a/pkg/obiiter/batch.go +++ b/pkg/obiiter/batch.go @@ -108,14 +108,3 @@ func (batch BioSequenceBatch) Pop0() *obiseq.BioSequence { func (batch BioSequenceBatch) IsNil() bool { return batch.slice == nil } - -// Recycle cleans up the BioSequenceBatch by recycling its elements and resetting its slice. -// -// If including_seq is true, each element of the BioSequenceBatch's slice is recycled using the Recycle method, -// and then set to nil. If including_seq is false, each element is simply set to nil. -// -// This function does not return anything. -func (batch BioSequenceBatch) Recycle(including_seq bool) { - batch.slice.Recycle(including_seq) - batch.slice = nil -} diff --git a/pkg/obiiter/batchiterator.go b/pkg/obiiter/batchiterator.go index f2e5df7..4a49403 100644 --- a/pkg/obiiter/batchiterator.go +++ b/pkg/obiiter/batchiterator.go @@ -459,7 +459,6 @@ func (iterator IBioSequence) Rebatch(size int) IBioSequence { } i += to_push } - seqs.Recycle(false) } log.Debug("End of the rebatch loop") if len(buffer) > 0 { @@ -521,7 +520,6 @@ func (iterator IBioSequence) Recycle() { o := batch.Order() log.Debugln("Recycling batch #", o) recycled += batch.Len() - batch.Recycle(true) log.Debugln("Batch #", o, " recycled") } log.Debugf("End of the recycling of %d Bioseq objects", recycled) @@ -529,8 +527,7 @@ func (iterator IBioSequence) Recycle() { func (iterator IBioSequence) Consume() { for iterator.Next() { - batch := iterator.Get() - batch.Recycle(false) + iterator.Get() } } @@ -548,7 +545,6 @@ func (iterator IBioSequence) Count(recycle bool) (int, int, int) { reads += seq.Count() nucleotides += seq.Len() } - batch.Recycle(recycle) } log.Debugf("End of the counting of %d Bioseq objects", variants) return variants, reads, nucleotides @@ -602,7 +598,6 @@ func (iterator IBioSequence) DivideOn(predicate obiseq.SequencePredicate, falseSlice = obiseq.MakeBioSequenceSlice() } } - seqs.Recycle(false) } if len(trueSlice) > 0 { @@ -749,7 +744,6 @@ func (iterator IBioSequence) Load() (string, obiseq.BioSequenceSlice) { } log.Debugf("append %d sequences", b.Len()) chunk = append(chunk, b.Slice()...) - b.Recycle(false) } return source, chunk diff --git a/pkg/obiiter/distribute.go b/pkg/obiiter/distribute.go index 4c61d58..ee2b080 100644 --- a/pkg/obiiter/distribute.go +++ b/pkg/obiiter/distribute.go @@ -93,7 +93,6 @@ func (iterator IBioSequence) Distribute(class *obiseq.BioSequenceClassifier, siz slices[key] = &s } } - seqs.Recycle(false) } for key, slice := range slices { diff --git a/pkg/obiiter/fragment.go b/pkg/obiiter/fragment.go index 2c09013..3c448ba 100644 --- a/pkg/obiiter/fragment.go +++ b/pkg/obiiter/fragment.go @@ -55,7 +55,6 @@ func IFragments(minsize, length, overlap, size, nworkers int) Pipeable { } } // End of the slice loop newiter.Push(MakeBioSequenceBatch(source, sl.Order(), news)) - sl.Recycle(false) } // End of the iterator loop // if len(news) > 0 { diff --git a/pkg/obilua/lua.go b/pkg/obilua/lua.go index 833751d..08a71cf 100644 --- a/pkg/obilua/lua.go +++ b/pkg/obilua/lua.go @@ -228,7 +228,6 @@ func LuaProcessor(iterator obiiter.IBioSequence, name, program string, breakOnEr } newIter.Push(obiiter.MakeBioSequenceBatch(seqs.Source(), seqs.Order(), ns)) - seqs.Recycle(false) } newIter.Done() diff --git a/pkg/obiseq/biosequence.go b/pkg/obiseq/biosequence.go index bb0533e..101e333 100644 --- a/pkg/obiseq/biosequence.go +++ b/pkg/obiseq/biosequence.go @@ -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)) } diff --git a/pkg/obiseq/biosequenceslice.go b/pkg/obiseq/biosequenceslice.go index 5e56ed5..24993ec 100644 --- a/pkg/obiseq/biosequenceslice.go +++ b/pkg/obiseq/biosequenceslice.go @@ -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. diff --git a/pkg/obiseq/merge.go b/pkg/obiseq/merge.go index 7af3daf..98827ed 100644 --- a/pkg/obiseq/merge.go +++ b/pkg/obiseq/merge.go @@ -295,6 +295,5 @@ func (sequences BioSequenceSlice) Merge(na string, statsOn StatsOnDescriptions) } } - sequences.Recycle(false) return seq } diff --git a/pkg/obiseq/revcomp.go b/pkg/obiseq/revcomp.go index d9d1391..837459c 100644 --- a/pkg/obiseq/revcomp.go +++ b/pkg/obiseq/revcomp.go @@ -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 diff --git a/pkg/obitools/obiconsensus/obiconsensus.go b/pkg/obitools/obiconsensus/obiconsensus.go index 127bb75..aab05ba 100644 --- a/pkg/obitools/obiconsensus/obiconsensus.go +++ b/pkg/obitools/obiconsensus/obiconsensus.go @@ -317,8 +317,6 @@ func MinionDenoise(graph *obigraph.Graph[*obiseq.BioSequence, Mutation], } - pack.Recycle(false) - } else { clean = obiseq.NewBioSequence(v.Id(), v.Sequence(), v.Definition()) clean.SetAttribute("obiconsensus_consensus", false) @@ -403,7 +401,6 @@ func MinionClusterDenoise(graph *obigraph.Graph[*obiseq.BioSequence, Mutation], clean = (*graph.Vertices)[i].Copy() clean.SetAttribute("obiconsensus_consensus", false) } - pack.Recycle(false) clean.SetAttribute(sample_key, graph.Name) @@ -436,7 +433,6 @@ func CLIOBIMinion(itertator obiiter.IBioSequence) obiiter.IBioSequence { log.Infof("Sequence dataset of %d sequeences loaded\n", len(db)) samples := SeqBySamples(db, CLISampleAttribute()) - db.Recycle(false) log.Infof("Dataset composed of %d samples\n", len(samples)) diff --git a/pkg/obitools/obimatrix/obimatrix.go b/pkg/obitools/obimatrix/obimatrix.go index 176e680..04b30c6 100644 --- a/pkg/obitools/obimatrix/obimatrix.go +++ b/pkg/obitools/obimatrix/obimatrix.go @@ -107,7 +107,6 @@ func IMatrix(iterator obiiter.IBioSequence) *MatrixData { for _, seq := range batch.Slice() { summary.Update(seq, mapAttribute) } - batch.Recycle(true) } waiter.Done() } diff --git a/pkg/obitools/obisummary/obisummary.go b/pkg/obitools/obisummary/obisummary.go index 454f40b..cc5ecdf 100644 --- a/pkg/obitools/obisummary/obisummary.go +++ b/pkg/obitools/obisummary/obisummary.go @@ -172,7 +172,6 @@ func ISummary(iterator obiiter.IBioSequence, summarise []string) map[string]inte for _, seq := range batch.Slice() { summary.Update(seq) } - batch.Recycle(true) } waiter.Done() }